Skip to content

Commit

Permalink
Merge pull request #104 from fingerprintjs/feature/api-exception-retr…
Browse files Browse the repository at this point in the history
…y-after

feat: add retry after policy to api exception inter-860
  • Loading branch information
Orkuncakilkaya authored Aug 30, 2024
2 parents 159af19 + 1175aab commit 0245bfe
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
56 changes: 56 additions & 0 deletions src/Api/FingerprintApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ public function deleteVisitorData(string $visitor_id): array
break;
}

if (429 === $e->getCode()) {
$e->setRetryAfter(1);
if ($response->hasHeader('retry-after')) {
$e->setRetryAfter((int) $response->getHeader('retry-after')[0]);
}
}

throw $e;
}
}
Expand Down Expand Up @@ -233,6 +240,13 @@ function ($e) {
break;
}

if (429 === $e->getCode()) {
$e->setRetryAfter(1);
if ($response->hasHeader('retry-after')) {
$e->setRetryAfter((int) $response->getHeader('retry-after')[0]);
}
}

throw $e;
}
);
Expand Down Expand Up @@ -318,6 +332,13 @@ public function getEvent(string $request_id): array
break;
}

if (429 === $e->getCode()) {
$e->setRetryAfter(1);
if ($response->hasHeader('retry-after')) {
$e->setRetryAfter((int) $response->getHeader('retry-after')[0]);
}
}

throw $e;
}
}
Expand Down Expand Up @@ -390,6 +411,13 @@ function ($e) {
break;
}

if (429 === $e->getCode()) {
$e->setRetryAfter(1);
if ($response->hasHeader('retry-after')) {
$e->setRetryAfter((int) $response->getHeader('retry-after')[0]);
}
}

throw $e;
}
);
Expand Down Expand Up @@ -480,6 +508,13 @@ public function getVisits(string $visitor_id, ?string $request_id = null, ?strin
break;
}

if (429 === $e->getCode()) {
$e->setRetryAfter(1);
if ($response->hasHeader('retry-after')) {
$e->setRetryAfter((int) $response->getHeader('retry-after')[0]);
}
}

throw $e;
}
}
Expand Down Expand Up @@ -557,6 +592,13 @@ function ($e) {
break;
}

if (429 === $e->getCode()) {
$e->setRetryAfter(1);
if ($response->hasHeader('retry-after')) {
$e->setRetryAfter((int) $response->getHeader('retry-after')[0]);
}
}

throw $e;
}
);
Expand Down Expand Up @@ -648,6 +690,13 @@ public function updateEvent(EventUpdateRequest $body, string $request_id): array
break;
}

if (429 === $e->getCode()) {
$e->setRetryAfter(1);
if ($response->hasHeader('retry-after')) {
$e->setRetryAfter((int) $response->getHeader('retry-after')[0]);
}
}

throw $e;
}
}
Expand Down Expand Up @@ -726,6 +775,13 @@ function ($e) {
break;
}

if (429 === $e->getCode()) {
$e->setRetryAfter(1);
if ($response->hasHeader('retry-after')) {
$e->setRetryAfter((int) $response->getHeader('retry-after')[0]);
}
}

throw $e;
}
);
Expand Down
11 changes: 11 additions & 0 deletions src/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ApiException extends \Exception
{
protected ResponseInterface $responseObject;
protected ?ModelInterface $errorDetails = null;
protected ?int $retryAfter = null;

public function __construct(?string $message = '', ?int $code = 0)
{
Expand Down Expand Up @@ -72,4 +73,14 @@ public function setErrorDetails(ModelInterface $errorDetails): void
{
$this->errorDetails = $errorDetails;
}

public function getRetryAfter(): ?int
{
return $this->retryAfter;
}

public function setRetryAfter(?int $retryAfter): void
{
$this->retryAfter = $retryAfter;
}
}
11 changes: 11 additions & 0 deletions template/ApiException.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ApiException extends Exception
{
protected ResponseInterface $responseObject;
protected ?ModelInterface $errorDetails = null;
protected ?int $retryAfter = null;
public function __construct(?string $message = "", ?int $code = 0)
{
Expand Down Expand Up @@ -63,4 +64,14 @@ class ApiException extends Exception
{
$this->errorDetails = $errorDetails;
}

public function getRetryAfter(): ?int
{
return $this->retryAfter;
}

public function setRetryAfter(?int $retryAfter): void
{
$this->retryAfter = $retryAfter;
}
}
16 changes: 16 additions & 0 deletions template/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ use \GuzzleHttp\Exception\GuzzleException;
{{/dataType}}
{{/responses}}
}

if ($e->getCode() === 429) {
$e->setRetryAfter(1);
if ($response->hasHeader('retry-after')) {
$e->setRetryAfter((int) $response->getHeader('retry-after')[0]);
}
}

throw $e;
}
}
Expand Down Expand Up @@ -206,6 +214,14 @@ use \GuzzleHttp\Exception\GuzzleException;
{{/dataType}}
{{/responses}}
}

if ($e->getCode() === 429) {
$e->setRetryAfter(1);
if ($response->hasHeader('retry-after')) {
$e->setRetryAfter((int) $response->getHeader('retry-after')[0]);
}
}

throw $e;
}
);
Expand Down
8 changes: 7 additions & 1 deletion test/FingerprintApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ protected function getMockResponse(string $mockId, string $method = "GET", ?stri
{
$mock_name = "";
$status = 200;
$headers = [
'Content-Type' => 'application/json',
];

if ($method === "GET" || $method === "DELETE") {
switch ($mockId) {
Expand Down Expand Up @@ -111,6 +114,7 @@ protected function getMockResponse(string $mockId, string $method = "GET", ?stri
case self::MOCK_VISITOR_ID_429_ERROR:
$mock_name = 'get_visits_429_too_many_requests_error.json';
$status = 429;
$headers['retry-after'] = 30;
break;
case self::MOCK_VISITOR_ID_400_ERROR:
$mock_name = "400_error_incorrect_visitor_id.json";
Expand Down Expand Up @@ -157,7 +161,7 @@ protected function getMockResponse(string $mockId, string $method = "GET", ?stri
$contents = json_encode($visits_mock_data);
}

return new Response($status, [], $contents);
return new Response($status, $headers, $contents);
}

public function testGetEvent()
Expand Down Expand Up @@ -389,6 +393,7 @@ public function testGetVisits429Error()
$this->fingerprint_api->getVisits(self::MOCK_VISITOR_ID_429_ERROR);
} catch (ApiException $e) {
$this->assertEquals(TooManyRequestsResponse::class, get_class($e->getErrorDetails()));
$this->assertEquals(30, $e->getRetryAfter());
throw $e;
}
}
Expand Down Expand Up @@ -492,6 +497,7 @@ public function testDeleteVisitorData429Error()
$this->fingerprint_api->deleteVisitorData(self::MOCK_VISITOR_ID_429_ERROR);
} catch (ApiException $e) {
$this->assertEquals(ErrorCommon429Response::class, get_class($e->getErrorDetails()));
$this->assertEquals(30, $e->getRetryAfter());
throw $e;
}
}
Expand Down

0 comments on commit 0245bfe

Please sign in to comment.