Skip to content

Commit

Permalink
feat: add retry after policy to api exception
Browse files Browse the repository at this point in the history
This commit solves task inter-860
  • Loading branch information
Orkuncakilkaya committed Aug 28, 2024
1 parent 2a8be2e commit 64e0510
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Api/FingerprintApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
);
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
);
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;
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;
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 @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
);
Expand Down

0 comments on commit 64e0510

Please sign in to comment.