This repository has been archived by the owner on Aug 19, 2023. It is now read-only.
forked from bunq/sdk_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added response handler to retry 429. bunq#109
- Loading branch information
Showing
5 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,48 @@ | ||||||||||||||||||||||||||||||||||
<?php declare(strict_types=1); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
namespace bunq\Http\Handler; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
use bunq\Http\ApiClient; | ||||||||||||||||||||||||||||||||||
use phpDocumentor\Reflection\Types\This; | ||||||||||||||||||||||||||||||||||
use bunq\Http\RequestRetryer; | ||||||||||||||||||||||||||||||||||
use Psr\Http\Message\RequestInterface; | ||||||||||||||||||||||||||||||||||
use Psr\Http\Message\ResponseInterface; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||
* Class ResponseHandlerRateLimit | ||||||||||||||||||||||||||||||||||
* @package bunq\Http\Handler | ||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||
class ResponseHandlerRateLimit extends ResponseHandlerBase | ||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||
* @var RequestRetryert | ||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||
private $retryer; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||
* ResponseHandlerRateLimit constructor. | ||||||||||||||||||||||||||||||||||
* @param ApiClient $client | ||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||
public function __construct(RequestRetryer $retryer) | ||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||
$this->retryer = $retryer; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||
* @param ResponseInterface $response | ||||||||||||||||||||||||||||||||||
* @param RequestInterface $request | ||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||
* @return ResponseInterface | ||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||
public function execute(ResponseInterface $response, RequestInterface $request): ResponseInterface | ||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||
if ($response->getStatusCode() === 429) { | ||||||||||||||||||||||||||||||||||
usleep(1500); | ||||||||||||||||||||||||||||||||||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
OGKevin
Author
Owner
|
public function execute(ResponseInterface $response, RequestInterface $request): ResponseInterface | |
{ | |
if ($response->getStatusCode() === 429) { | |
$this->incRetryCounter($request->getUri()->__toString()); | |
if ($this->retryMap[$request->getUri()->__toString()] > 2) { | |
// let the error handler further down the stack handle the 429 error | |
return $response; | |
} | |
usleep(1100 * $this->retryMap[$request->getUri()->__toString()]); | |
return $this->retryer->retryRequest($request); | |
} | |
$this->retryMap[$request->getUri()->__toString()] = 0; | |
return $response; | |
} |
This comment has been minimized.
This comment has been minimized.
Sorry, something went wrong.
OGKevin
Mar 17, 2019
Author
Owner
and to answer your question, no, bunq does not say how long you should wait or any of that. They only return a plain and simple 429 with an inaccurate estimate of what the rate limit is. E.g. 1 call per 3 sec etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace bunq\Http; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* Interface RequestRertier | ||
* @package bunq\Http | ||
*/ | ||
interface RequestRetryer | ||
{ | ||
/** | ||
* @param RequestInterface $request | ||
* @return ResponseInterface | ||
*/ | ||
public function retryRequest(RequestInterface $request): ResponseInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php declare(strict_types=1); | ||
|
||
|
||
namespace bunq\test\Http; | ||
|
||
use bunq\Http\RequestRetryer; | ||
use GuzzleHttp\Psr7\Response; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* Class RequesRertyerForTest | ||
* @package bunq\test\Http | ||
*/ | ||
class RequestRertyerForTest implements RequestRetryer | ||
{ | ||
/** | ||
* @param RequestInterface $request | ||
* @return ResponseInterface | ||
*/ | ||
public function retryRequest(RequestInterface $request): ResponseInterface | ||
{ | ||
return new Response(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace bunq\test\Http; | ||
|
||
use bunq\Http\Handler\ResponseHandlerRateLimit; | ||
use bunq\test\BunqSdkTestBase; | ||
use GuzzleHttp\Psr7\Request; | ||
use GuzzleHttp\Psr7\Response; | ||
|
||
/** | ||
* Class ResponseHandlerRateLimitTest | ||
* @package bunq\test\Http | ||
*/ | ||
class ResponseHandlerRateLimitTest extends BunqSdkTestBase | ||
{ | ||
/** | ||
*/ | ||
public function testExecute() | ||
{ | ||
$sut = new ResponseHandlerRateLimit(new RequestRertyerForTest()); | ||
|
||
$response = $sut->execute(new Response(429), new Request('GET', 'https://whatthecommit.com/index.txt')); | ||
|
||
static::assertEquals(200, $response->getStatusCode()); | ||
} | ||
|
||
/** | ||
*/ | ||
public function testActualRequest() | ||
{ | ||
static::markTestSkipped('how do we want to test this'); | ||
} | ||
} |
Does the Exception / response not indicate a time to "wait"?
It not: the current way of "static" backing of seems not right. Usually it is dynamic: 1 second, 2 seconds, 4 seconds, 16 seconds, etc.