1 | <?php |
||
9 | class Request |
||
10 | { |
||
11 | const API_CALL_LIMIT_HEADER = 'http_x_shopify_shop_api_call_limit'; |
||
12 | const METHOD_POST = 'POST'; |
||
13 | const METHOD_GET = 'GET'; |
||
14 | const METHOD_PUT = 'PUT'; |
||
15 | const METHOD_DELETE = 'DELETE'; |
||
16 | |||
17 | /** |
||
18 | * @var ClientInterface |
||
19 | */ |
||
20 | protected $httpClient; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $responseKey; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | private $rateLimit; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | private $callsMade; |
||
36 | |||
37 | /** |
||
38 | * @var float |
||
39 | */ |
||
40 | private static $callCycle = 0.5; // avg. 2 calls a second |
||
41 | |||
42 | /** |
||
43 | * @var float |
||
44 | */ |
||
45 | private $rateLimitThreshold = 0.8; |
||
46 | |||
47 | /** |
||
48 | * @var int |
||
49 | */ |
||
50 | private $rateLimitReached = 0; |
||
51 | |||
52 | /** |
||
53 | * Request constructor. |
||
54 | * @param ClientInterface $client |
||
55 | */ |
||
56 | 5 | public function __construct(ClientInterface $client) |
|
60 | |||
61 | /** |
||
62 | * @param string $method |
||
63 | * @param string $endpoint |
||
64 | * @param array $parameters |
||
65 | * @return bool |
||
66 | * @throws ShopifyException |
||
67 | */ |
||
68 | 130 | public function request(string $method, string $endpoint, array $parameters = []) |
|
91 | |||
92 | /** |
||
93 | * @param string $responseKey |
||
94 | * @return Request |
||
95 | */ |
||
96 | 130 | public function setResponseKey($responseKey) |
|
100 | |||
101 | /** |
||
102 | * @param callable $function |
||
103 | * @return mixed |
||
104 | */ |
||
105 | 16 | public static function throttle(callable $function) |
|
119 | |||
120 | /** |
||
121 | * @return int |
||
122 | */ |
||
123 | 1 | public function getRateLimitReached(): int |
|
127 | |||
128 | 130 | private function handleRateLimit() |
|
137 | |||
138 | /** |
||
139 | * @param array $header |
||
140 | */ |
||
141 | 127 | private function setRateLimit(array $header) |
|
151 | |||
152 | /** |
||
153 | * @return bool |
||
154 | */ |
||
155 | 121 | public function isRateLimitReached(): bool |
|
159 | |||
160 | /** |
||
161 | * @return float|int |
||
162 | */ |
||
163 | 121 | public function getCallLimit() |
|
167 | } |
||
168 |