From 64e05100a2c20c8d1f8e5f9719ee5292c59761c2 Mon Sep 17 00:00:00 2001 From: Orkun Date: Wed, 28 Aug 2024 14:44:44 +0300 Subject: [PATCH] feat: add retry after policy to api exception This commit solves task inter-860 --- src/Api/FingerprintApi.php | 28 ++++++++++++++++++++++++++++ src/ApiException.php | 11 +++++++++++ template/ApiException.mustache | 11 +++++++++++ template/api.mustache | 16 ++++++++++++++++ 4 files changed, 66 insertions(+) diff --git a/src/Api/FingerprintApi.php b/src/Api/FingerprintApi.php index e2413418..60ae6b72 100644 --- a/src/Api/FingerprintApi.php +++ b/src/Api/FingerprintApi.php @@ -149,6 +149,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')); + } + } + throw $e; } } @@ -219,6 +226,13 @@ function ($e) { break; } + if (429 === $e->getCode()) { + $e->setRetryAfter(1); + if ($response->hasHeader('retry-after')) { + $e->setRetryAfter((int) $response->getHeader('retry-after')); + } + } + throw $e; } ); @@ -308,6 +322,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')); + } + } + throw $e; } } @@ -383,6 +404,13 @@ function ($e) { break; } + if (429 === $e->getCode()) { + $e->setRetryAfter(1); + if ($response->hasHeader('retry-after')) { + $e->setRetryAfter((int) $response->getHeader('retry-after')); + } + } + throw $e; } ); diff --git a/src/ApiException.php b/src/ApiException.php index a31ebdff..be7536f6 100644 --- a/src/ApiException.php +++ b/src/ApiException.php @@ -44,6 +44,7 @@ class ApiException extends \Exception { protected ResponseInterface $responseObject; protected ModelInterface $errorDetails; + protected ?int $retryAfter = null; public function __construct(?string $message = '', ?int $code = 0) { @@ -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; + } } diff --git a/template/ApiException.mustache b/template/ApiException.mustache index fe6fd54d..f6a323b7 100644 --- a/template/ApiException.mustache +++ b/template/ApiException.mustache @@ -34,6 +34,7 @@ class ApiException extends Exception { protected ResponseInterface $responseObject; protected ModelInterface $errorDetails; + protected ?int $retryAfter = null; public function __construct(?string $message = "", ?int $code = 0) { @@ -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; + } } \ No newline at end of file diff --git a/template/api.mustache b/template/api.mustache index 9a631e5f..3a3f0cde 100644 --- a/template/api.mustache +++ b/template/api.mustache @@ -137,6 +137,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')); + } + } + throw $e; } } @@ -203,6 +211,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')); + } + } + throw $e; } );