Skip to content
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
15 changes: 14 additions & 1 deletion src/Client/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function sendRequest(string $httpMethod, $uri, array $headers = [], $body
{
$request = $this->prepareRequest($httpMethod, $uri, $headers, $body);

$response = $this->httpClient->sendRequest($request);
$response = $this->retry($this->httpClient->sendRequest($request), $request);

return $this->httpExceptionHandler->transformResponseToException($request, $response);
}
Expand All @@ -88,4 +88,17 @@ public function sendAsync(

return $this->httpClient->sendAsyncRequest($request);
}

private function retry(ResponseInterface $response, RequestInterface $request, int $retryDelay = 2): ResponseInterface
{
if ($this->options->canRetry() && HttpClient::HTTP_TOO_MANY_REQUESTS === $response->getStatusCode()) {
$retry = 0;
while ($this->options->getMaxRetry() > $retry && HttpClient::HTTP_TOO_MANY_REQUESTS === $response->getStatusCode()) {
usleep($retryDelay * 1000);
$response = $this->httpClient->sendRequest($request);
$retry++;
}
}
return $response;
}
}
16 changes: 15 additions & 1 deletion src/Client/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class Options
{
private function __construct(
private array $options
private array $options,
) {
}

Expand All @@ -19,8 +19,12 @@ public static function fromArray(array $options): self

$resolver->setDefaults([
'headers' => [],
'retry' => false,
'max-retry' => 5,
]);
$resolver->setAllowedTypes('headers', 'string[]');
$resolver->setAllowedTypes('retry', 'bool');
$resolver->setAllowedTypes('max-retry', 'int');

$options = $resolver->resolve($options);

Expand All @@ -39,4 +43,14 @@ public function getHeaders(): array
{
return $this->options['headers'];
}

public function getMaxRetry(): int
{
return $this->options['max-retry'];
}

public function canRetry(): bool
{
return $this->options['retry'];
}
}
50 changes: 50 additions & 0 deletions tests/Api/AkeneoPimClientBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Akeneo\Pim\ApiClient\tests\Api;

use Akeneo\Pim\ApiClient\AkeneoPimClientBuilder;
use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
use Akeneo\Pim\ApiClient\Api\ProductApi;
use donatj\MockWebServer\Response;
use donatj\MockWebServer\ResponseStack;
use ECSPrefix202306\Symfony\Component\VarDumper\VarDumper;
use PHPUnit\Framework\Assert;

final class AkeneoPimClientBuilderTest extends ApiTestCase
{
public function test_retry_http_request()
{
$this->server->setResponseOfPath(
'/' . ProductApi::PRODUCTS_URI,
new ResponseStack(
new Response('', [], 429),
new Response('', [], 429),
new Response('', [], 429),
new Response('', [], 429),
new Response('', [], 201),
)
);

$api = $this->createClientByPassword()->getProductApi();
$response = $api->create('new_shoes', []);

Assert::assertSame(201, $response);
}

protected function createClientByPassword(): AkeneoPimClientInterface
{
$clientBuilder = new AkeneoPimClientBuilder($this->server->getServerRoot(), [
'retry' => true,
'max-retry' => 5,
]);

return $clientBuilder->buildAuthenticatedByPassword(
'client_id',
'secret',
'username',
'password'
);
}
}