From 28df119372858fda6384ed01928798553514a439 Mon Sep 17 00:00:00 2001 From: Martin Ficzel Date: Mon, 8 Jun 2020 11:36:37 +0200 Subject: [PATCH 1/3] FEATURE: Add apiClientOptions to configuration The options are used for the Guzzle HTTP Client as is documented here: http://docs.guzzlephp.org/en/6.5/request-options.html Use this to configure custom certificates, timeouts or to disable cert verification --- Classes/AssetSource/PixxioAssetSource.php | 14 +++++++++++++- Classes/Service/PixxioClient.php | 17 ++++++++++++----- Classes/Service/PixxioServiceFactory.php | 6 ++++-- Configuration/Settings.yaml | 5 +++++ README.md | 15 +++++++++++++++ 5 files changed, 49 insertions(+), 8 deletions(-) diff --git a/Classes/AssetSource/PixxioAssetSource.php b/Classes/AssetSource/PixxioAssetSource.php index 0aa1384..e73af4c 100644 --- a/Classes/AssetSource/PixxioAssetSource.php +++ b/Classes/AssetSource/PixxioAssetSource.php @@ -67,6 +67,11 @@ class PixxioAssetSource implements AssetSourceInterface */ private $apiKey; + /** + * @var array + */ + private $apiClientOptions = []; + /** * @var string */ @@ -117,6 +122,12 @@ public function __construct(string $assetSourceIdentifier, array $assetSourceOpt } $this->apiKey = $optionValue; break; + case 'apiClientOptions': + if (!is_array($optionValue) || empty($optionValue)) { + throw new \InvalidArgumentException(sprintf('Invalid api client options specified for Pixx.io asset source %s', $assetSourceIdentifier), 1591605348); + } + $this->apiClientOptions = $optionValue; + break; case 'sharedRefreshToken': if (!is_string($optionValue) || empty($optionValue)) { throw new \InvalidArgumentException(sprintf('Invalid shared refresh token specified for Pixx.io asset source %s', $assetSourceIdentifier), 1528806843); @@ -257,7 +268,8 @@ public function getPixxioClient(): PixxioClient $this->pixxioClient = $this->pixxioServiceFactory->createForAccount( $account->getAccountIdentifier(), $this->apiEndpointUri, - $this->apiKey + $this->apiKey, + $this->apiClientOptions ); $this->pixxioClient->authenticate($clientSecret->getRefreshToken()); diff --git a/Classes/Service/PixxioClient.php b/Classes/Service/PixxioClient.php index 5731e05..2c39c41 100644 --- a/Classes/Service/PixxioClient.php +++ b/Classes/Service/PixxioClient.php @@ -43,6 +43,11 @@ final class PixxioClient */ private $apiKey; + /** + * @var array + */ + private $apiClientOptions; + /** * @var string */ @@ -65,12 +70,14 @@ final class PixxioClient /** * @param string $apiEndpointUri * @param string $apiKey + * @param array $apiClientOptions */ - public function __construct(string $apiEndpointUri, string $apiKey) + public function __construct(string $apiEndpointUri, string $apiKey, array $apiClientOptions) { $this->apiEndpointUri = $apiEndpointUri; $this->apiKey = $apiKey; - $this->guzzleClient = new Client(); + $this->apiClientOptions = $apiClientOptions; + $this->guzzleClient = new Client($this->apiClientOptions); $this->imageOptions = [ (object)[ 'width' => 400, @@ -136,7 +143,7 @@ public function getFile(string $id): ResponseInterface 'options=' . \GuzzleHttp\json_encode($options) ); - $client = new Client(); + $client = new Client($this->apiClientOptions); try { return $client->request('GET', $uri); } catch (GuzzleException $e) { @@ -164,7 +171,7 @@ public function updateFile(string $id, array $metadata): ResponseInterface 'accessToken=' . $this->accessToken ); - $client = new Client(); + $client = new Client($this->apiClientOptions); try { return $client->request( 'PUT', @@ -219,7 +226,7 @@ public function search(string $queryExpression, array $formatTypes, array $fileT 'options=' . \GuzzleHttp\json_encode($options) ); - $client = new Client(); + $client = new Client($this->apiClientOptions); try { return $client->request('GET', $uri); } catch (GuzzleException $e) { diff --git a/Classes/Service/PixxioServiceFactory.php b/Classes/Service/PixxioServiceFactory.php index 19ff5e6..12cddd9 100644 --- a/Classes/Service/PixxioServiceFactory.php +++ b/Classes/Service/PixxioServiceFactory.php @@ -42,13 +42,15 @@ class PixxioServiceFactory * @param string $accountIdentifier * @param string $apiEndpointUri * @param string $apiKey + * @param array $apiClientOptions * @return PixxioClient */ - public function createForAccount(string $accountIdentifier, string $apiEndpointUri, string $apiKey) + public function createForAccount(string $accountIdentifier, string $apiEndpointUri, string $apiKey, array $apiClientOptions) { $client = new PixxioClient( $apiEndpointUri, - $apiKey + $apiKey, + $apiClientOptions ); return $client; } diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index 3032503..a8f6012 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -19,6 +19,11 @@ Neos: # Please get in touch with Pixx.io support in order to get this key. apiKey: '' + # Options for the Guzzle HTTP Client as specified here + # see http://docs.guzzlephp.org/en/6.5/request-options.html + # Use this to configure custom certificates or to disable cert verification + apiClientOptions: {} + # A pixx.io user refresh token which is shared across all editors in # this Neos installation. # sharedRefreshToken: '' diff --git a/README.md b/README.md index ea92dcf..e3a8939 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,21 @@ Neos: usePixxioThumbnailAsOriginal: true ``` +Sometimes the API Client needs additional configuration for the tls connection +like custom timeouts or certificates. +See: http://docs.guzzlephp.org/en/6.5/request-options.html + +```yaml +Neos: + Media: + assetSources: + 'flownative-pixxio': + assetSource: 'Flownative\Pixxio\AssetSource\PixxioAssetSource' + assetSourceOptions: + apiClientOptions: + 'verify': '/path/to/cert.pem' +``` + ## Run database migrations ```bash ./flow doctrine:migrate From 5ea5798c4ce4ded635a3551f488eeb3c3be2b6c3 Mon Sep 17 00:00:00 2001 From: Martin Ficzel Date: Mon, 8 Jun 2020 12:41:05 +0200 Subject: [PATCH 2/3] TASK: Support empty apiClientOptions --- Classes/AssetSource/PixxioAssetSource.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/AssetSource/PixxioAssetSource.php b/Classes/AssetSource/PixxioAssetSource.php index e73af4c..c110fed 100644 --- a/Classes/AssetSource/PixxioAssetSource.php +++ b/Classes/AssetSource/PixxioAssetSource.php @@ -123,7 +123,7 @@ public function __construct(string $assetSourceIdentifier, array $assetSourceOpt $this->apiKey = $optionValue; break; case 'apiClientOptions': - if (!is_array($optionValue) || empty($optionValue)) { + if (!is_array($optionValue)) { throw new \InvalidArgumentException(sprintf('Invalid api client options specified for Pixx.io asset source %s', $assetSourceIdentifier), 1591605348); } $this->apiClientOptions = $optionValue; From 9ae2c6f59abefc7c699d55f98f0a40c6f5031f4f Mon Sep 17 00:00:00 2001 From: Martin Ficzel Date: Tue, 9 Jun 2020 17:57:58 +0200 Subject: [PATCH 3/3] TASK: Import asset via guzzlehttp instead of fopen to apply assetSourceOptions there aswell --- Classes/AssetSource/PixxioAssetProxy.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Classes/AssetSource/PixxioAssetProxy.php b/Classes/AssetSource/PixxioAssetProxy.php index 3fc27c7..738dd6f 100644 --- a/Classes/AssetSource/PixxioAssetProxy.php +++ b/Classes/AssetSource/PixxioAssetProxy.php @@ -15,6 +15,8 @@ use Behat\Transliterator\Transliterator; use Exception; +use Flownative\Pixxio\Exception\ConnectionException; +use GuzzleHttp\Client; use Neos\Flow\Http\Uri; use Neos\Media\Domain\Model\AssetSource\AssetProxy\AssetProxyInterface; use Neos\Media\Domain\Model\AssetSource\AssetProxy\HasRemoteOriginalInterface; @@ -287,7 +289,17 @@ public function getPreviewUri(): ?UriInterface */ public function getImportStream() { - return fopen($this->originalUri, 'rb'); + $client = new Client($this->assetSource->getAssetSourceOptions()['apiClientOptions'] ?? []); + try { + $response = $client->request('GET', $this->originalUri); + if ($response->getStatusCode() === 200) { + return $response->getBody()->detach(); + } else { + return false; + } + } catch (GuzzleException $e) { + throw new ConnectionException('Retrieving file failed: ' . $e->getMessage(), 1542808207); + } } /**