|
31 | 31 | class Client |
32 | 32 | { |
33 | 33 |
|
| 34 | + /** |
| 35 | + * Float describing the number of seconds to wait while trying to connect to a server |
| 36 | + * @var float |
| 37 | + */ |
| 38 | + private $connect_timeout = 30; |
| 39 | + |
| 40 | + /** |
| 41 | + * Float describing the total timeout of the request in seconds |
| 42 | + * @var float |
| 43 | + */ |
| 44 | + private $timeout = 60; |
| 45 | + |
34 | 46 | /** |
35 | 47 | * Grant type for authorization code |
36 | 48 | */ |
@@ -215,10 +227,12 @@ public function setOAuthApiRoot($oAuthApiRoot) |
215 | 227 | * @param string $clientId |
216 | 228 | * @param string $clientSecret |
217 | 229 | */ |
218 | | - public function __construct($clientId = '', $clientSecret = '') |
| 230 | + public function __construct($clientId = '', $clientSecret = '', ?float $connect_timeout = null, ?float $timeout = null) |
219 | 231 | { |
220 | 232 | !empty($clientId) && $this->setClientId($clientId); |
221 | 233 | !empty($clientSecret) && $this->setClientSecret($clientSecret); |
| 234 | + !is_null($connect_timeout) && $this->setConnectTimeout($connect_timeout); |
| 235 | + !is_null($timeout) && $this->setTimeout($timeout); |
222 | 236 | } |
223 | 237 |
|
224 | 238 | /** |
@@ -286,6 +300,8 @@ public function getAccessToken($code = '') |
286 | 300 | $headers['Connection'] = 'Keep-Alive'; |
287 | 301 | $guzzle = new GuzzleClient([ |
288 | 302 | 'headers' => $headers, |
| 303 | + 'connect_timeout' => $this->connect_timeout, |
| 304 | + 'timeout' => $this->timeout, |
289 | 305 | ]); |
290 | 306 | try { |
291 | 307 | $response = $guzzle->post($uri, ['form_params' => [ |
@@ -324,6 +340,8 @@ public function renewTokenFromRefreshToken($refreshToken = '') |
324 | 340 | $headers['Connection'] = 'Keep-Alive'; |
325 | 341 | $guzzle = new GuzzleClient([ |
326 | 342 | 'headers' => $headers, |
| 343 | + 'connect_timeout' => $this->connect_timeout, |
| 344 | + 'timeout' => $this->timeout, |
327 | 345 | ]); |
328 | 346 | try { |
329 | 347 | $response = $guzzle->post($uri, ['form_params' => [ |
@@ -556,6 +574,8 @@ public function api($endpoint, array $params = [], $method = Method::GET) |
556 | 574 | $guzzle = new GuzzleClient([ |
557 | 575 | 'base_uri' => $this->getApiRoot(), |
558 | 576 | 'headers' => $headers, |
| 577 | + 'connect_timeout' => $this->connect_timeout, |
| 578 | + 'timeout' => $this->timeout, |
559 | 579 | ]); |
560 | 580 | if (!empty($params) && Method::GET === $method) { |
561 | 581 | $endpoint .= '?' . build_query($params); |
@@ -626,7 +646,9 @@ public function upload($path) |
626 | 646 | $headers['Authorization'] = 'Bearer ' . $this->accessToken->getToken(); |
627 | 647 | } |
628 | 648 | $guzzle = new GuzzleClient([ |
629 | | - 'base_uri' => $this->getApiRoot() |
| 649 | + 'base_uri' => $this->getApiRoot(), |
| 650 | + 'connect_timeout' => $this->connect_timeout, |
| 651 | + 'timeout' => $this->timeout, |
630 | 652 | ]); |
631 | 653 | $fileinfo = pathinfo($path); |
632 | 654 | $filename = preg_replace('/\W+/', '_', $fileinfo['filename']); |
@@ -664,4 +686,28 @@ protected function prepareOptions(array $params, $method) |
664 | 686 | } |
665 | 687 | return $options; |
666 | 688 | } |
| 689 | + |
| 690 | + /** |
| 691 | + * @param mixed $connect_timeout |
| 692 | + * |
| 693 | + * @return self |
| 694 | + */ |
| 695 | + public function setConnectTimeout(float $connect_timeout) |
| 696 | + { |
| 697 | + $this->connect_timeout = $connect_timeout; |
| 698 | + |
| 699 | + return $this; |
| 700 | + } |
| 701 | + |
| 702 | + /** |
| 703 | + * @param mixed $timeout |
| 704 | + * |
| 705 | + * @return self |
| 706 | + */ |
| 707 | + public function setTimeout(float $timeout) |
| 708 | + { |
| 709 | + $this->timeout = $timeout; |
| 710 | + |
| 711 | + return $this; |
| 712 | + } |
667 | 713 | } |
0 commit comments