|
2 | 2 |
|
3 | 3 | namespace Onfleet; |
4 | 4 |
|
5 | | -use bandwidthThrottle\tokenBucket\Rate; |
6 | | -use bandwidthThrottle\tokenBucket\storage\StorageException; |
7 | | -use bandwidthThrottle\tokenBucket\TokenBucket; |
8 | | -use bandwidthThrottle\tokenBucket\storage\FileStorage; |
9 | | - |
10 | 5 | require_once("Constants.php"); |
11 | 6 |
|
12 | 7 | class CurlClient |
13 | 8 | { |
14 | | - private TokenBucket $_bucket; |
15 | | - |
16 | | - /** |
17 | | - * @throws StorageException |
18 | | - */ |
19 | | - public function __construct() |
20 | | - { |
21 | | - $storage = new FileStorage(__DIR__ . "/onfleet-api.bucket"); |
22 | | - $rate = new Rate(MAX_CONSUME, Rate::SECOND); |
23 | | - $this->_bucket = new TokenBucket(MAX_CONSUME, $rate, $storage); |
24 | | - $this->_bucket->bootstrap(MAX_CONSUME); |
25 | | - } |
26 | 9 |
|
27 | 10 | /** |
28 | 11 | * Authentication checker |
@@ -59,81 +42,51 @@ public function execute( |
59 | 42 | array $params = [], |
60 | 43 | int $timeOut = 0 |
61 | 44 | ): array { |
62 | | - do { |
63 | | - // Delayed 40 - 200 microseconds all request in order to have some time to limit the request |
64 | | - usleep(rand(MIN_DELAY_TIME, MAX_DELAY_TIME)); |
65 | | - $isValid = $this->_bucket->consume(1); |
66 | | - if (!$isValid) { |
67 | | - sleep(1); |
68 | | - continue; |
69 | | - } |
70 | | - |
71 | | - $this->_client = curl_init(); |
72 | | - curl_setopt($this->_client, CURLOPT_URL, $url); |
73 | | - |
74 | | - // Default configuration |
75 | | - curl_setopt($this->_client, CURLOPT_SSL_VERIFYHOST, 2); |
76 | | - curl_setopt($this->_client, CURLOPT_SSL_VERIFYPEER, 1); |
77 | | - curl_setopt($this->_client, CURLOPT_RETURNTRANSFER, 1); |
78 | | - curl_setopt($this->_client, CURLOPT_HEADER, 1); |
| 45 | + $this->_client = curl_init(); |
| 46 | + curl_setopt($this->_client, CURLOPT_URL, $url); |
79 | 47 |
|
80 | | - if (is_int($timeOut) && $timeOut > 0) { |
81 | | - curl_setopt($this->_client, CURLOPT_TIMEOUT, $timeOut); |
82 | | - } |
| 48 | + // Default configuration |
| 49 | + curl_setopt($this->_client, CURLOPT_SSL_VERIFYHOST, 2); |
| 50 | + curl_setopt($this->_client, CURLOPT_SSL_VERIFYPEER, 1); |
| 51 | + curl_setopt($this->_client, CURLOPT_RETURNTRANSFER, 1); |
| 52 | + curl_setopt($this->_client, CURLOPT_HEADER, 1); |
83 | 53 |
|
84 | | - if ($headers) { |
85 | | - curl_setopt($this->_client, CURLOPT_HTTPHEADER, $headers); |
86 | | - } |
87 | | - |
88 | | - if ($method === 'POST') { |
89 | | - curl_setopt($this->_client, CURLOPT_POST, 1); |
90 | | - } else if (in_array($method, ['PUT', 'PATCH', 'DELETE'])) { |
91 | | - curl_setopt($this->_client, CURLOPT_CUSTOMREQUEST, $method); |
92 | | - } |
93 | | - |
94 | | - if (!empty($params) && in_array($method, ['POST', 'PUT', 'PATCH', 'DELETE'])) { |
95 | | - if ($params) curl_setopt($this->_client, CURLOPT_POSTFIELDS, json_encode($params)); |
96 | | - } |
| 54 | + if (is_int($timeOut) && $timeOut > 0) { |
| 55 | + curl_setopt($this->_client, CURLOPT_TIMEOUT, $timeOut); |
| 56 | + } |
97 | 57 |
|
98 | | - $result = curl_exec($this->_client); |
99 | | - if ($result === false) { |
100 | | - throw new \Exception("Connection couldn't be established."); |
101 | | - } |
| 58 | + if ($headers) { |
| 59 | + curl_setopt($this->_client, CURLOPT_HTTPHEADER, $headers); |
| 60 | + } |
102 | 61 |
|
103 | | - $httpCode = curl_getinfo($this->_client, CURLINFO_HTTP_CODE); |
104 | | - $success = ($httpCode >= 200 && $httpCode < 300); |
105 | | - $header_size = curl_getinfo($this->_client, CURLINFO_HEADER_SIZE); |
106 | | - $stringHeader = substr($result, 0, $header_size); |
107 | | - $this->_checkRatelimitRemaining( |
108 | | - array_map(function ($value) { |
109 | | - return explode(": ", $value); |
110 | | - }, explode("\n", $stringHeader)) |
111 | | - ); |
112 | | - $result = json_decode(substr($result, $header_size, strlen($result)), true); |
113 | | - $response = [ |
114 | | - 'success' => $success, |
115 | | - 'code' => curl_getinfo($this->_client, CURLINFO_HTTP_CODE), |
116 | | - 'data' => $success ? $result : null, |
117 | | - 'error' => !$success ? $result : null, |
118 | | - ]; |
| 62 | + if ($method === 'POST') { |
| 63 | + curl_setopt($this->_client, CURLOPT_POST, 1); |
| 64 | + } else if (in_array($method, ['PUT', 'PATCH', 'DELETE'])) { |
| 65 | + curl_setopt($this->_client, CURLOPT_CUSTOMREQUEST, $method); |
| 66 | + } |
119 | 67 |
|
120 | | - curl_close($this->_client); |
121 | | - return $response; |
122 | | - } while (!$isValid); |
123 | | - return []; |
124 | | - } |
| 68 | + if (!empty($params) && in_array($method, ['POST', 'PUT', 'PATCH', 'DELETE'])) { |
| 69 | + if ($params) curl_setopt($this->_client, CURLOPT_POSTFIELDS, json_encode($params)); |
| 70 | + } |
125 | 71 |
|
126 | | - private function _checkRatelimitRemaining($headers) |
127 | | - { |
128 | | - foreach ($headers as $value) { |
129 | | - if ($value[0] === "x-ratelimit-remaining") { |
130 | | - $remaining = (int) $value[1]; |
131 | | - $available = $this->_bucket->getTokens(); |
132 | | - if ($available > 0 && $available < $remaining) { |
133 | | - $newConsume = $remaining - $available; |
134 | | - $this->_bucket->consume($newConsume); |
135 | | - } |
136 | | - } |
| 72 | + $result = curl_exec($this->_client); |
| 73 | + if ($result === false) { |
| 74 | + throw new \Exception("Connection couldn't be established."); |
137 | 75 | } |
| 76 | + |
| 77 | + $httpCode = curl_getinfo($this->_client, CURLINFO_HTTP_CODE); |
| 78 | + $success = ($httpCode >= 200 && $httpCode < 300); |
| 79 | + $header_size = curl_getinfo($this->_client, CURLINFO_HEADER_SIZE); |
| 80 | + $stringHeader = substr($result, 0, $header_size); |
| 81 | + $result = json_decode(substr($result, $header_size, strlen($result)), true); |
| 82 | + $response = [ |
| 83 | + 'success' => $success, |
| 84 | + 'code' => curl_getinfo($this->_client, CURLINFO_HTTP_CODE), |
| 85 | + 'data' => $success ? $result : null, |
| 86 | + 'error' => !$success ? $result : null, |
| 87 | + ]; |
| 88 | + |
| 89 | + curl_close($this->_client); |
| 90 | + return $response; |
138 | 91 | } |
139 | 92 | } |
0 commit comments