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

Posibilty to respect rate limits given by bol.com 429 response #35

Merged
merged 1 commit into from
Jan 2, 2024
Merged
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
56 changes: 55 additions & 1 deletion src/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\ConnectException as GuzzleConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Picqer\BolRetailerV10\Exception\RateLimitException;
use Picqer\BolRetailerV10\Exception\ServerException;
use Picqer\BolRetailerV10\Model\AbstractModel;
Expand All @@ -15,6 +18,7 @@
use Picqer\BolRetailerV10\Exception\UnauthorizedException;
use Picqer\BolRetailerV10\Model\Authentication\TokenResponse;
use Picqer\BolRetailerV10\Model\Authentication\TokenRequest;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class BaseClient
Expand All @@ -29,6 +33,11 @@ class BaseClient
*/
protected $isDemoMode = false;

/**
* @var bool Whether to respect and wait for the rate limits.
*/
protected $respectRateLimits = true;

/** @var HttpClient|null */
protected $http = null;

Expand All @@ -43,7 +52,16 @@ class BaseClient
*/
public function __construct()
{
$this->setHttp(new HttpClient());
$handlerStack = HandlerStack::create();

$handlerStack->push(Middleware::retry(
$this->retryDecider(),
$this->retryDelay()
));

$this->setHttp(new HttpClient([
'handler' => $handlerStack,
]));
}

/**
Expand Down Expand Up @@ -76,6 +94,16 @@ public function setDemoMode(bool $enabled): void
$this->isDemoMode = $enabled;
}

/**
* Configure whether to respect and wait for the rate limits.
*
* @param bool $enabled Set to `true` to respect and wait for rate limits, `false` otherwise.
*/
public function respectRateLimits(bool $enabled): void
{
$this->respectRateLimits = $enabled;
}

/**
* Sets a callback which is called at the start of a request when an access token is set, but expired. This callback
* may attempt to refresh the access token. If the token is valid after the callback, the request will continue,
Expand Down Expand Up @@ -572,4 +600,30 @@ protected function printResponse(ResponseInterface $response)
echo "\n";
echo $response->getBody() . "\n";
}

private function retryDecider()
{
return function (
$retries,
RequestInterface $request,
?ResponseInterface $response = null,
?RequestException $exception = null
) {
return $this->respectRateLimits
&& $response
&& $response->getStatusCode() === 429
&& (int)$response->getHeaderLine('Retry-After');
};
}

private function retryDelay()
{
return function (
$retries,
?ResponseInterface $response = null,
RequestInterface $request
) {
return (int)$response->getHeaderLine('Retry-After') * 1000;
};
}
}
Loading