Skip to content

Commit da593ec

Browse files
authored
[INT-1483] [Bug] PHP wrapper isn't compatible with latest version of Laravel (#14)
* [INT-1483] [Bug] PHP wrapper isn't compatible with latest version of Laravel Removed bottleneck library due to conflicts with frameworks * Prepare for v1.0.4
1 parent 60f8ebf commit da593ec

File tree

4 files changed

+46
-90
lines changed

4 files changed

+46
-90
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.0.4] - 2023-10-05
10+
### Fixed
11+
- Removed library that causes conflicts with Laravel framework
12+
913
## [1.0.3] - 2023-06-22
1014
### Fixed
1115
- Fixed issue when getting data from workers and tasks
@@ -23,8 +27,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2327
### Added
2428
- Initial release on packagist
2529

26-
[Unreleased]: https://github.com/onfleet/php-onfleet/compare/v1.0.3...HEAD
30+
[Unreleased]: https://github.com/onfleet/php-onfleet/compare/v1.0.4...HEAD
2731
[1.0.0]: https://github.com/onfleet/php-onfleet/releases/tag/v1.0.0
2832
[1.0.1]: https://github.com/onfleet/php-onfleet/compare/v1.0.0...v1.0.1
2933
[1.0.2]: https://github.com/onfleet/php-onfleet/compare/v1.0.1...v1.0.2
3034
[1.0.3]: https://github.com/onfleet/php-onfleet/compare/v1.0.2...v1.0.3
35+
[1.0.3]: https://github.com/onfleet/php-onfleet/compare/v1.0.3...v1.0.4

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
"test": "./vendor/bin/phpunit tests"
2727
},
2828
"require": {
29-
"php": ">=7.4",
30-
"bandwidth-throttle/token-bucket": "^2.0"
29+
"php": ">=7.4"
3130
},
3231
"require-dev": {
3332
"phpunit/phpunit": "^9.5"

src/CurlClient.php

Lines changed: 39 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,10 @@
22

33
namespace Onfleet;
44

5-
use bandwidthThrottle\tokenBucket\Rate;
6-
use bandwidthThrottle\tokenBucket\storage\StorageException;
7-
use bandwidthThrottle\tokenBucket\TokenBucket;
8-
use bandwidthThrottle\tokenBucket\storage\FileStorage;
9-
105
require_once("Constants.php");
116

127
class CurlClient
138
{
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-
}
269

2710
/**
2811
* Authentication checker
@@ -59,81 +42,51 @@ public function execute(
5942
array $params = [],
6043
int $timeOut = 0
6144
): 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);
7947

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);
8353

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+
}
9757

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+
}
10261

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+
}
11967

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+
}
12571

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.");
13775
}
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;
13891
}
13992
}

src/onfleet-api.bucket

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)