Skip to content

Commit

Permalink
Merge pull request #35 from WoutervanderLoopNL/develop-rate-limits
Browse files Browse the repository at this point in the history
Posibilty to respect rate limits given by bol.com 429 response
  • Loading branch information
casperbakker authored Jan 2, 2024
2 parents efa3ed4 + 754b8d3 commit a9defa4
Showing 1 changed file with 55 additions and 1 deletion.
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;
};
}
}

0 comments on commit a9defa4

Please sign in to comment.