-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathUpdateApiLimits.php
More file actions
116 lines (103 loc) · 3.31 KB
/
UpdateApiLimits.php
File metadata and controls
116 lines (103 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
declare(strict_types=1);
namespace Gnikyt\BasicShopifyAPI\Middleware;
use Gnikyt\BasicShopifyAPI\BasicShopifyAPI;
use Gnikyt\BasicShopifyAPI\Traits\IsResponseType;
use Gnikyt\BasicShopifyAPI\Traits\ResponseTransform;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Update API limits for REST and GraphQL calls.
*/
class UpdateApiLimits extends AbstractMiddleware
{
use IsResponseType;
use ResponseTransform;
/**
* Run.
*
* @param callable $handler
*
* @return callable
*/
public function __invoke(callable $handler): callable
{
$self = $this;
return function (RequestInterface $request, array $options) use ($self, $handler) {
$promise = $handler($request, $options);
return $promise->then(
function (ResponseInterface $response) use ($self) {
if ($self->isRestResponse($response)) {
$self->updateRestLimits($response);
} else {
$self->updateGraphCosts($response);
}
return $response;
}
);
};
}
/**
* Update the GraphQL costs.
*
* @param ResponseInterface $response
*
* @return void
*/
protected function updateGraphCosts(ResponseInterface $response): void
{
// Get the GraphQL client
$client = $this->api->getGraphClient();
$body = $this->toResponse($response->getBody());
if (!isset($body['extensions']) || !isset($body['extensions']['cost'])) {
// Non-existant, exit
return;
}
// Update the costs
$cost = $body['extensions']['cost'];
$client->getLimitStore()->push(
[
'left' => (int)
$cost['throttleStatus']['currentlyAvailable'],
'made' => (int)
($cost['throttleStatus']['maximumAvailable'] - $cost['throttleStatus']['currentlyAvailable']),
'limit' => (int)
$cost['throttleStatus']['maximumAvailable'],
'restoreRate' => (int)
$cost['throttleStatus']['restoreRate'],
'requestedCost' => (int)
$cost['requestedQueryCost'],
'actualCost' => (int)
$cost['actualQueryCost'],
],
$this->api->getSession()
);
}
/**
* Updates the REST API call limits from Shopify headers.
*
* @param ResponseInterface $response
*
* @return void
*/
protected function updateRestLimits(ResponseInterface $response): void
{
// Grab the API call limit header returned from Shopify
$header = $response->getHeader(BasicShopifyAPI::HEADER_REST_API_LIMITS);
if (!$header) {
// Non-existant, exit
return;
}
// Update the limits
$calls = explode('/', $header[0]);
$client = $this->api->getRestClient();
$client->getLimitStore()->push(
[
'left' => (int) $calls[1] - (int) $calls[0],
'made' => (int) $calls[0],
'limit' => (int) $calls[1],
],
$this->api->getSession()
);
}
}