Skip to content

Commit

Permalink
Merge pull request #8 from sitegeist/feature/addApiClientOptionsConfi…
Browse files Browse the repository at this point in the history
…guration

FEATURE: Add apiClientOptions to configuration
  • Loading branch information
robertlemke committed Dec 4, 2020
2 parents 07869c1 + 9ae2c6f commit b79be00
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 9 deletions.
14 changes: 13 additions & 1 deletion Classes/AssetSource/PixxioAssetProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down
14 changes: 13 additions & 1 deletion Classes/AssetSource/PixxioAssetSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class PixxioAssetSource implements AssetSourceInterface
*/
private $apiKey;

/**
* @var array
*/
private $apiClientOptions = [];

/**
* @var string
*/
Expand Down Expand Up @@ -117,6 +122,12 @@ public function __construct(string $assetSourceIdentifier, array $assetSourceOpt
}
$this->apiKey = $optionValue;
break;
case 'apiClientOptions':
if (!is_array($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);
Expand Down Expand Up @@ -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());
Expand Down
17 changes: 12 additions & 5 deletions Classes/Service/PixxioClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ final class PixxioClient
*/
private $apiKey;

/**
* @var array
*/
private $apiClientOptions;

/**
* @var string
*/
Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions Classes/Service/PixxioServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b79be00

Please sign in to comment.