diff --git a/CHANGELOG.md b/CHANGELOG.md index b531216..a1d3966 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## 1.0.9 - 2024-02-08 + +### Added +- Add `includeErrorResponse` as a request option to allow API errors to be returned in an `error` property. +- Add `Service::EVENT_BEFORE_FETCH_DATA` event to handle before data is fetched. (thanks @markhuot). + +### Fixed +- Fix Zoho not working for multiple data centers. + ## 1.0.8 - 2023-12-18 ### Fixed diff --git a/docs/providers/all-providers.md b/docs/providers/all-providers.md index 2c3e051..44713df 100644 --- a/docs/providers/all-providers.md +++ b/docs/providers/all-providers.md @@ -430,7 +430,7 @@ Follow the below steps to connect to the Facebook API. 1. Select **None** as the **App Type**, and fill in the rest of the details. 1. Once created, in the left-hand sidebar, click the **Add Product** button and select **Facebook Login**. 1. Select **Web** as the type and your website address into **Site URL**. -1. Navigate to **Facebook Login** section in the left-hand siderbar, click **Settings**. +1. Navigate to **Facebook Login** section in the left-hand sidebar, click **Settings**. 1. For the **Valid OAuth Redirect URIs** setting, enter the value from the **Redirect URI** field in Consume. 1. Click the **Save Changes** button. 1. Navigate to **App Review** → **Requests**. @@ -1284,3 +1284,4 @@ Follow the below steps to connect to the Zoho API. 1. In the **Authorized Redirect URIs** field, enter the value from the **Redirect URI** field in Consume. 1. Copy the **Client ID** from Zoho and paste in the **Client ID** field in Consume. 1. Copy the **Client Secret** from Zoho and paste in the **Client Secret** field in Consume. +1. Be sure to also change your **Data Center** value to match the data center your Zoho account is on. diff --git a/src/clients/oauth/Zoho.php b/src/clients/oauth/Zoho.php index 67d2e69..dd21058 100644 --- a/src/clients/oauth/Zoho.php +++ b/src/clients/oauth/Zoho.php @@ -3,6 +3,8 @@ use verbb\consume\base\OAuthClient; +use craft\helpers\App; + use verbb\auth\Auth; use verbb\auth\providers\Zoho as ZohoProvider; @@ -21,5 +23,39 @@ public static function getOAuthProviderClass(): string // ========================================================================= public static string $providerHandle = 'zoho'; + public ?string $dataCenter = 'US'; + + + // Public Methods + // ========================================================================= + + public function getDataCenter(): ?string + { + return App::parseEnv($this->dataCenter); + } + + public function defineRules(): array + { + $rules = parent::defineRules(); + $rules[] = [['dataCenter'], 'required']; + return $rules; + } + + public function getOAuthProviderConfig(): array + { + $config = parent::getOAuthProviderConfig(); + $config['dc'] = $this->getDataCenter(); + + return $config; + } + + public function getAuthorizationUrlOptions(): array + { + $options = parent::getAuthorizationUrlOptions(); + $options['access_type'] = 'offline'; + $options['prompt'] = 'consent'; + + return $options; + } } \ No newline at end of file diff --git a/src/events/FetchEvent.php b/src/events/FetchEvent.php new file mode 100644 index 0000000..c22b347 --- /dev/null +++ b/src/events/FetchEvent.php @@ -0,0 +1,18 @@ + $clientOpts, + 'method' => $method, + 'uri' => $uri, + 'options' => $options, + ]); + + Event::trigger(self::class, self::EVENT_BEFORE_FETCH_DATA, $event); + + if (! $event->isValid) { + return $event->response; + } + $settings = Consume::$plugin->getSettings(); // Allow overriding cache/duration in templates @@ -86,6 +107,9 @@ public function fetchRawData(array|string $clientOpts, string $method = '', stri $format = ArrayHelper::remove($clientOpts, 'format', 'json'); $handle = ArrayHelper::remove($clientOpts, 'handle'); + // See if we want to include any errors in the response, rather than return `null` + $includeErrorResponse = ArrayHelper::remove($options, 'includeErrorResponse', false); + // Otherwise, we're probably passing in Guzzle settings for an in-template call // Normalise the Base URI and URI $uri = ltrim($uri, '/'); @@ -136,6 +160,11 @@ public function fetchRawData(array|string $clientOpts, string $method = '', stri 'line' => $e->getLine(), 'trace' => $e->getTraceAsString(), ]); + + // Check if we want to return any errors rather than just return `null` + if ($e instanceof RequestException && $includeErrorResponse) { + return ['error' => $this->_parseResponse($format, $e->getResponse())]; + } } return null; diff --git a/src/templates/clients/oauth/_types/zoho.html b/src/templates/clients/oauth/_types/zoho.html index 069f300..38df686 100644 --- a/src/templates/clients/oauth/_types/zoho.html +++ b/src/templates/clients/oauth/_types/zoho.html @@ -1 +1,23 @@ +{% import '_includes/forms' as forms %} +{% import 'verbb-base/_macros' as macros %} + {% include 'consume/clients/oauth/_oauth' %} + +{{ forms.selectField({ + label: 'Data Center' | t('consume'), + instructions: 'Select your {provider} Data Center here.' | t('consume', { provider: client.providerName }), + name: 'dataCenter', + required: true, + suggestEnvVars: true, + value: client.dataCenter ?? '', + options: [ + { label: 'US' | t('consume'), value: 'US' }, + { label: 'AU' | t('consume'), value: 'AU' }, + { label: 'EU' | t('consume'), value: 'EU' }, + { label: 'IN' | t('consume'), value: 'IN' }, + { label: 'CN' | t('consume'), value: 'CN' }, + { label: 'JP' | t('consume'), value: 'JP' }, + ], + warning: macros.configWarning("clients.#{client.handle}.dataCenter", 'consume'), + errors: client.getErrors('dataCenter'), +}) }} \ No newline at end of file