From b12d36d06926f293fab473191ef1591ea8f39372 Mon Sep 17 00:00:00 2001 From: Oleksii Savchuk Date: Sun, 10 Oct 2021 16:48:43 +0300 Subject: [PATCH] Enable to override service description. Add options to retry decider --- composer.json | 7 ++++--- src/ShopifyClient.php | 24 +++++++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 68c619b..c92dbec 100644 --- a/composer.json +++ b/composer.json @@ -16,11 +16,12 @@ } ], "require": { - "php": "~7.0", + "php": "^7.0 || ^8.0", "guzzlehttp/guzzle-services": "^1.0", - "psr/container": "^1.0", + "psr/container": "^2.0", "psr/http-message": "^1.0", - "laminas/laminas-diactoros": "^1.3 || ^2.0" + "laminas/laminas-diactoros": "^1.3 || ^2.0", + "ext-json": "*" }, "require-dev": { "phpunit/phpunit": "^6.0", diff --git a/src/ShopifyClient.php b/src/ShopifyClient.php index 2ceb984..0186440 100644 --- a/src/ShopifyClient.php +++ b/src/ShopifyClient.php @@ -410,7 +410,10 @@ class ShopifyClient public function __construct(array $connectionOptions, GuzzleClient $guzzleClient = null, array $guzzleMiddleware = []) { $this->validateConnectionOptions($connectionOptions); - $this->connectionOptions = $connectionOptions; + $this->connectionOptions = array_merge([ + "use_retry_decider" => true, + "max_retries" => 5, + ], $connectionOptions); $this->guzzleClient = $guzzleClient ?? $this->createDefaultClient($guzzleMiddleware); } @@ -514,7 +517,7 @@ public function wrapRequestData(CommandInterface $command): RequestInterface if (($method === 'post' || $method === 'put') && $rootKey !== null) { $newBody = [$rootKey => json_decode($request->getBody()->getContents(), true)]; - $request = $request->withBody(Psr7\stream_for(json_encode($newBody))); + $request = $request->withBody(Psr7\Utils::streamFor(json_encode($newBody))); } return $request; @@ -533,7 +536,8 @@ public function wrapRequestData(CommandInterface $command): RequestInterface public function retryDecider(int $retries, RequestInterface $request, ResponseInterface $response = null, ClientExceptionInterface $exception = null): bool { // Limit the number of retries to 5 - if ($retries >= 5) { + $max_retries = $this->connectionOptions["max_retries"]; + if ($retries >= $max_retries) { return false; } @@ -595,14 +599,16 @@ private function createDefaultClient(array $guzzleMiddleware = []): GuzzleClient $baseUri = 'https://' . str_replace('.myshopify.com', '', $this->connectionOptions['shop']) . '.myshopify.com'; $handlerStack = HandlerStack::create(new CurlHandler()); - $handlerStack->push(Middleware::retry([$this, 'retryDecider'], [$this, 'retryDelay'])); + if ($this->connectionOptions["use_retry_decider"]) { + $handlerStack->push($this->getRetryDecider()); + } foreach ($guzzleMiddleware as $curMiddleware) { $handlerStack->push($curMiddleware); } $httpClient = new Client(['base_uri' => $baseUri, 'handler' => $handlerStack]); - $description = new Description(require __DIR__ . '/ServiceDescription/Shopify-v1.php'); + $description = new Description($this->getServiceDescription()); return new GuzzleClient($httpClient, $description, [$this, 'wrapRequestData']); } @@ -691,4 +697,12 @@ private function getRequestAuthorizationArguments(): array ]; } } + + public function getRetryDecider() { + return Middleware::retry([$this, 'retryDecider'], [$this, 'retryDelay']); + } + + public function getServiceDescription() : array { + return require __DIR__ . '/ServiceDescription/Shopify-v1.php'; + } }