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

PLENTY-489-retry-requests-with-exceptions #95

Merged
merged 11 commits into from
May 27, 2024
28 changes: 24 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
use FINDOLOGIC\PlentyMarketsRestExporter\Debug\DummyDebugger;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\AuthorizationException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\CriticalException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\CustomerException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry\CustomerException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\PermissionException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry\EmptyResponseException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry\RetryableException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\ThrottlingException;
use FINDOLOGIC\PlentyMarketsRestExporter\Logger\DummyLogger;
use FINDOLOGIC\PlentyMarketsRestExporter\Request\Request;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request as GuzzleRequest;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\RequestOptions;
Expand Down Expand Up @@ -90,10 +92,28 @@ public function send(Request $request): ResponseInterface
{
$this->handleRateLimit();
$this->handleLogin();

$endpoint = $request->getUri()->__toString();
$request = $request->withUri($this->buildRequestUri($endpoint));
try {
aserstobitov marked this conversation as resolved.
Show resolved Hide resolved
$response = $this->sendRequest($request, $request->getParams());
$this->handleResponse($request, $response);
} catch (RetryableException | RequestException $e) {
if (!$request->isRetryLimitReached()) {
$this->customerLogger->error($e->getMessage());
$request->incrementRetryCounter();
sleep(60);

$this->customerLogger->debug(sprintf(
'Retrying failed request. Attempt number %s.',
(string) $request->getRetryCounter()
));
$request = $request->withUri(new Uri($endpoint));
return $this->send($request);
}

$request = $request->withUri($this->buildRequestUri($request->getUri()->__toString()));
$response = $this->sendRequest($request, $request->getParams());
$this->handleResponse($request, $response);
throw $e;
}

return $response;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/GenerateTokenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Exception;
use FINDOLOGIC\PlentyMarketsRestExporter\Client;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\AuthorizationException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\CustomerException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry\CustomerException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\PermissionException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry\EmptyResponseException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\ThrottlingException;
Expand Down
11 changes: 0 additions & 11 deletions src/Exception/CustomerException.php

This file was deleted.

4 changes: 3 additions & 1 deletion src/Exception/PermissionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace FINDOLOGIC\PlentyMarketsRestExporter\Exception;

class PermissionException extends CustomerException
use Exception;

class PermissionException extends Exception
{
}
11 changes: 11 additions & 0 deletions src/Exception/Retry/CustomerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry;

use FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry\RetryableException;

class CustomerException extends RetryableException
{
}
2 changes: 1 addition & 1 deletion src/Exporter/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use FINDOLOGIC\PlentyMarketsRestExporter\Config;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\AuthorizationException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\CriticalException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\CustomerException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry\CustomerException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\PermissionException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry\EmptyResponseException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\ThrottlingException;
Expand Down
2 changes: 1 addition & 1 deletion src/RegistryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use FINDOLOGIC\PlentyMarketsRestExporter\Definition\PropertyOptionType;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\AuthorizationException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\CriticalException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\CustomerException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry\CustomerException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\PermissionException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry\EmptyResponseException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\ThrottlingException;
Expand Down
20 changes: 20 additions & 0 deletions src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
abstract class Request extends GuzzleRequest
{
protected array $params = [];
private int $retryCounter = 1;

public function __construct(
string $method,
Expand All @@ -28,4 +29,23 @@ public function getParams(): array
{
return $this->params;
}

public function getRetryCounter(): int
{
return $this->retryCounter;
}

public function incrementRetryCounter(): void
{
$this->retryCounter++;
}

public function isRetryLimitReached(): bool
{
if ($this->retryCounter == 5) {
return true;
}

return false;
aserstobitov marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use FINDOLOGIC\Export\Helpers\DataHelper;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\AuthorizationException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\CriticalException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\CustomerException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry\CustomerException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\PermissionException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry\EmptyResponseException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\ThrottlingException;
Expand Down
16 changes: 13 additions & 3 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use FINDOLOGIC\PlentyMarketsRestExporter\Config;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\AuthorizationException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\CriticalException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\CustomerException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry\CustomerException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\PermissionException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry\EmptyResponseException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\ThrottlingException;
Expand Down Expand Up @@ -95,6 +95,7 @@ public function badResponseProvider(): array
'response' => new GuzzleResponse(401, [], 'OOF you are not logged in bro...'),
'expectedException' => AuthorizationException::class,
'expectedExceptionMessage' => 'The REST client is not logged in.',
'expectedCallTimes' => 2
TobiasGraml11 marked this conversation as resolved.
Show resolved Hide resolved
],
'response with status code 403' => [
'request' => new CategoryRequest(1234),
Expand All @@ -104,12 +105,14 @@ public function badResponseProvider(): array
'The REST client does not have access rights for method with URI "%s"',
$requestUri
),
'expectedCallTimes' => 2
],
'response with status code 429' => [
'request' => new CategoryRequest(1234),
'response' => new GuzzleResponse(429, [], 'You have reached your rate limit :('),
'expectedException' => ThrottlingException::class,
'expectedExceptionMessage' => 'Throttling limit reached.',
'expectedCallTimes' => 2
],
'response with status code 200 but empty response' => [
'request' => new CategoryRequest(1234),
Expand All @@ -119,6 +122,7 @@ public function badResponseProvider(): array
'The API for URI "%s" responded with an empty response',
$requestUri
),
'expectedCallTimes' => 6
],
'response with unknown status code 400' => [
'request' => new CategoryRequest(1234),
Expand All @@ -128,6 +132,7 @@ public function badResponseProvider(): array
'Could not reach API method with URI "%s". Status code was 400.',
$requestUri
),
'expectedCallTimes' => 6
],
];
}
Expand All @@ -139,17 +144,22 @@ public function testExceptionsAreThrownIfResponseHasBadStatusCodes(
Request $request,
GuzzleResponse $response,
string $expectedException,
string $expectedExceptionMessage
string $expectedExceptionMessage,
int $expectedCallTimes
): void {
$this->expectException($expectedException);
$this->expectExceptionMessage($expectedExceptionMessage);

$client = $this->getDefaultClient();

$this->guzzleClientMock->expects($this->exactly(2))
$this->guzzleClientMock->expects($this->exactly($expectedCallTimes))
->method('send')
->willReturnOnConsecutiveCalls(
$this->getMockResponse('LoginResponse/response.json'),
$response,
$response,
$response,
$response,
$response
);

Expand Down
2 changes: 1 addition & 1 deletion tests/RegistryServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use FINDOLOGIC\PlentyMarketsRestExporter\Client;
use FINDOLOGIC\PlentyMarketsRestExporter\Config;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\AuthorizationException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\CustomerException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry\CustomerException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\PermissionException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\Retry\EmptyResponseException;
use FINDOLOGIC\PlentyMarketsRestExporter\Exception\ThrottlingException;
Expand Down
Loading