Skip to content

Commit

Permalink
FEATURE: Flow 6.x and 7.x compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
bwaidelich committed Oct 7, 2021
1 parent 4549d41 commit fea8009
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
35 changes: 20 additions & 15 deletions Classes/DataSource/HttpDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
declare(strict_types=1);
namespace Wwwision\ImportService\DataSource;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Uri;
use Wwwision\ImportService\ImportServiceException;
use Wwwision\ImportService\OptionsSchema;
use Wwwision\ImportService\ValueObject\DataRecords;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Http\Client\Browser;
use Neos\Flow\Http\Client\CurlEngineException;
use Neos\Flow\Http\Request;
use Neos\Flow\Http\Uri;

/**
* HTTP Data Source that allows to import records from some HTTP endpoint
Expand All @@ -18,10 +16,9 @@ final class HttpDataSource implements DataSourceInterface
{

/**
* @Flow\Inject
* @var Browser
* @var Client
*/
protected $httpClient;
private $httpClient;

/**
* @var Uri
Expand All @@ -43,14 +40,21 @@ protected function __construct(array $options)
$this->endpoint = new Uri($options['endpoint']);
$this->idAttributeName = $options['idAttributeName'] ?? 'id';
$this->versionAttributeName = $options['versionAttributeName'] ?? null;
$defaultHttpOptions = [
'headers' => [
'Accept' => 'application/json'
]
];
$this->httpClient = new Client($options['httpOptions'] ?? $defaultHttpOptions);
}

public static function getOptionsSchema(): OptionsSchema
{
return OptionsSchema::create()
->requires('endpoint', 'string')
->has('idAttributeName', 'string')
->has('versionAttributeName', 'string');
->has('versionAttributeName', 'string')
->has('httpOptions', 'array');
}

public static function createWithOptions(array $options): DataSourceInterface
Expand All @@ -64,18 +68,19 @@ public static function createWithOptions(array $options): DataSourceInterface
public function load(): DataRecords
{
try {
$request = Request::create($this->endpoint);
/** @noinspection PhpDeprecationInspection */
$request->setHeader('Accept', 'application/json');
$response = $this->httpClient->sendRequest($request);
} /** @noinspection PhpRedundantCatchClauseInspection */ catch (CurlEngineException $exception) {
$response = $this->httpClient->get($this->endpoint);
} catch (GuzzleException $exception) {
throw new ImportServiceException(sprintf('Request failed: %s.', $exception->getMessage()), 15102227415, $exception);
}

if ($response->getStatusCode() !== 200) {
throw new ImportServiceException(sprintf('Unexpected response status code %s on endpoint %s.', $response->getStatusCode(), $this->endpoint), 15102213263);
}
$data = json_decode($response->getBody()->getContents(), true);
try {
$data = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new ImportServiceException(sprintf('Unexpected response body or malformed JSON on endpoint %s.', $this->endpoint), 1633522969);
}

if (!\is_array($data)) {
throw new ImportServiceException(sprintf('Unexpected response body or malformed JSON on endpoint %s.', $this->endpoint), 15203231319);
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
"license": "MIT",
"require": {
"ext-json": "*",
"neos/flow": "^5.2"
"neos/flow": "^6.0 || ^7.0",
"guzzlehttp/guzzle": "^7.0"
},
"require-dev": {
"roave/security-advisories": "dev-master"
"roave/security-advisories": "dev-latest"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit fea8009

Please sign in to comment.