From 3fadc7b60bac384bcefa443e53f30453c3e13b4a Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Thu, 25 Jan 2024 10:20:31 +1100 Subject: [PATCH 1/7] Add `includeErrorResponse` as a request option to allow API errors to be returned in an `error` property --- config.codekit3 | 8 ++++++++ src/services/Service.php | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/config.codekit3 b/config.codekit3 index 337b199..9401991 100644 --- a/config.codekit3 +++ b/config.codekit3 @@ -536,6 +536,14 @@ "oAP" : "\/src\/clients\/oauth\/Facebook-output.php", "oF" : 0 }, + "\/src\/clients\/oauth\/Fedex.php" : { + "cB" : 0, + "ft" : 8192, + "hM" : 0, + "oA" : 1, + "oAP" : "\/src\/clients\/oauth\/Fedex-output.php", + "oF" : 0 + }, "\/src\/clients\/oauth\/Fitbit.php" : { "cB" : 0, "ft" : 8192, diff --git a/src/services/Service.php b/src/services/Service.php index 9b346ac..f9526b2 100644 --- a/src/services/Service.php +++ b/src/services/Service.php @@ -20,6 +20,8 @@ use yii\caching\TagDependency; +use GuzzleHttp\Exception\RequestException; + use verbb\auth\helpers\UrlHelper as AuthUrlHelper; use Symfony\Component\Serializer\Encoder\CsvEncoder; @@ -86,6 +88,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 +141,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; From dd940709791c4925eeb47fb77e68889f7d9d5416 Mon Sep 17 00:00:00 2001 From: markhuot Date: Thu, 25 Jan 2024 12:15:34 -0500 Subject: [PATCH 2/7] add fetch event for test mocking --- src/events/FetchEvent.php | 18 ++++++++++++++++++ src/services/Service.php | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/events/FetchEvent.php 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 @@ -130,6 +149,7 @@ public function fetchRawData(array|string $clientOpts, string $method = '', stri return $this->_parseResponse($format, $response); } catch (Throwable $e) { + \markhuot\craftpest\helpers\test\dd($e); Consume::error('Unable to fetch data: “{message}” {file}:{line}. Trace: “{trace}”.', [ 'message' => $e->getMessage(), 'file' => $e->getFile(), From a27fc13d7a79a359b3b90763d595704bcc851920 Mon Sep 17 00:00:00 2001 From: markhuot Date: Thu, 25 Jan 2024 12:46:42 -0500 Subject: [PATCH 3/7] removing debugging --- src/services/Service.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/services/Service.php b/src/services/Service.php index 78af8be..6cd1768 100644 --- a/src/services/Service.php +++ b/src/services/Service.php @@ -149,7 +149,6 @@ public function fetchRawData(array|string $clientOpts, string $method = '', stri return $this->_parseResponse($format, $response); } catch (Throwable $e) { - \markhuot\craftpest\helpers\test\dd($e); Consume::error('Unable to fetch data: “{message}” {file}:{line}. Trace: “{trace}”.', [ 'message' => $e->getMessage(), 'file' => $e->getFile(), From d6e55253cb4bf34496e1a00d898a657e70a5a1eb Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Thu, 8 Feb 2024 21:29:43 +1100 Subject: [PATCH 4/7] Fix Zoho not working for multiple data centers --- src/clients/oauth/Zoho.php | 36 ++++++++++++++++++++ src/templates/clients/oauth/_types/zoho.html | 22 ++++++++++++ 2 files changed, 58 insertions(+) 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/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 From 9c437cadb803fb2ad3ad55f233e7fde4bc446e1f Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Thu, 8 Feb 2024 21:33:24 +1100 Subject: [PATCH 5/7] version 1.0.9 --- CHANGELOG.md | 9 +++++++++ composer.json | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) 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/composer.json b/composer.json index c95e27e..d9bbc69 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "verbb/consume", "description": "Create HTTP requests in your Twig templates to consume JSON, XML or CSV content.", "type": "craft-plugin", - "version": "1.0.8", + "version": "1.0.9", "keywords": [ "craft", "cms", From 9c623adfc94ae3054a9cd40a674e5f25352e771a Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Thu, 8 Feb 2024 21:36:01 +1100 Subject: [PATCH 6/7] docs --- docs/providers/all-providers.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/providers/all-providers.md b/docs/providers/all-providers.md index 2c3e051..dbc9fe6 100644 --- a/docs/providers/all-providers.md +++ b/docs/providers/all-providers.md @@ -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. From 28a256eda84531e278a4ab5558a0e2036c455753 Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Thu, 8 Feb 2024 22:14:12 +1100 Subject: [PATCH 7/7] docs --- docs/providers/all-providers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/providers/all-providers.md b/docs/providers/all-providers.md index dbc9fe6..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**.