Skip to content

Commit

Permalink
Merge branch 'craft-4' of https://github.com/verbb/consume into craft-5
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.json
  • Loading branch information
engram-design committed Mar 4, 2024
2 parents 47af9bb + 28a256e commit e6cdb46
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 1 deletion.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion docs/providers/all-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**.
Expand Down Expand Up @@ -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.
36 changes: 36 additions & 0 deletions src/clients/oauth/Zoho.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use verbb\consume\base\OAuthClient;

use craft\helpers\App;

use verbb\auth\Auth;
use verbb\auth\providers\Zoho as ZohoProvider;

Expand All @@ -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;
}
}
18 changes: 18 additions & 0 deletions src/events/FetchEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace verbb\consume\events;

use craft\events\CancelableEvent;

class FetchEvent extends CancelableEvent
{
// Properties
// =========================================================================

public array|string $clientOpts;
public string $method;
public string $uri;
public array $options;

public mixed $response = null;

}
29 changes: 29 additions & 0 deletions src/services/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use verbb\consume\Consume;
use verbb\consume\base\OAuthClient;
use verbb\consume\events\FetchEvent;
use verbb\consume\models\Settings;

use Craft;
Expand All @@ -18,20 +19,40 @@
use Exception;
use Throwable;

use yii\base\Event;
use yii\caching\TagDependency;

use GuzzleHttp\Exception\RequestException;

use verbb\auth\helpers\UrlHelper as AuthUrlHelper;

use Symfony\Component\Serializer\Encoder\CsvEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;

class Service extends Component
{
// Constants
// =========================================================================
public const EVENT_BEFORE_FETCH_DATA = 'beforeFetchData';

// Public Methods
// =========================================================================

public function fetchData(array|string $clientOpts, string $method = 'GET', string $uri = '', array $options = []): mixed
{
$event = new FetchEvent([
'clientOpts' => $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
Expand Down Expand Up @@ -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, '/');
Expand Down Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions src/templates/clients/oauth/_types/zoho.html
Original file line number Diff line number Diff line change
@@ -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'),
}) }}

0 comments on commit e6cdb46

Please sign in to comment.