Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to override service description. Add options to retry decider #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 19 additions & 5 deletions src/ShopifyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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']);
}
Expand Down Expand Up @@ -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';
}
}