|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Github\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use ApiClients\Client\Github\RateLimitState; |
|
6
|
|
|
use ApiClients\Foundation\Middleware\DefaultPriorityTrait; |
|
7
|
|
|
use ApiClients\Foundation\Middleware\ErrorTrait; |
|
8
|
|
|
use ApiClients\Foundation\Middleware\MiddlewareInterface; |
|
9
|
|
|
use ApiClients\Foundation\Middleware\PreTrait; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
use React\Promise\CancellablePromiseInterface; |
|
12
|
|
|
use function React\Promise\resolve; |
|
13
|
|
|
|
|
14
|
|
|
final class RateLimitStateMiddleware implements MiddlewareInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use DefaultPriorityTrait; |
|
17
|
|
|
use PreTrait; |
|
18
|
|
|
use ErrorTrait; |
|
19
|
|
|
|
|
20
|
|
|
const HEADERS = [ |
|
21
|
|
|
'X-RateLimit-Limit' => 'setLimit', |
|
22
|
|
|
'X-RateLimit-Remaining' => 'setRemaining', |
|
23
|
|
|
'X-RateLimit-Reset' => 'setReset', |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var RateLimitState |
|
28
|
|
|
*/ |
|
29
|
|
|
private $state; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* RateLimitStatusMiddleware constructor. |
|
33
|
|
|
* @param RateLimitState $state |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function __construct(RateLimitState $state) |
|
36
|
|
|
{ |
|
37
|
1 |
|
$this->state = $state; |
|
38
|
1 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param ResponseInterface $response |
|
42
|
|
|
* @param array $options |
|
43
|
|
|
* @return CancellablePromiseInterface |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function post(ResponseInterface $response, array $options = []): CancellablePromiseInterface |
|
46
|
|
|
{ |
|
47
|
1 |
|
foreach (self::HEADERS as $header => $method) { |
|
48
|
1 |
|
if (!$response->hasHeader($header)) { |
|
49
|
1 |
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
$this->state->$method((int)$response->getHeaderLine($header)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
return resolve($response); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|