From 3da3d7ef844d715214b5d30cfb9394fccc8ecc73 Mon Sep 17 00:00:00 2001 From: Stiliyan Ivanov Date: Tue, 23 Feb 2021 23:19:02 +0200 Subject: [PATCH 1/3] Add timeouts Signed-off-by: Stiliyan Ivanov --- src/Client.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 95f815c..c218c42 100644 --- a/src/Client.php +++ b/src/Client.php @@ -31,6 +31,16 @@ class Client { + /** + * Float describing the number of seconds to wait while trying to connect to a server + */ + private $connect_timeout = 30; + + /** + * Float describing the total timeout of the request in seconds + */ + private $timeout = 60; + /** * Grant type for authorization code */ @@ -286,6 +296,8 @@ public function getAccessToken($code = '') $headers['Connection'] = 'Keep-Alive'; $guzzle = new GuzzleClient([ 'headers' => $headers, + 'connect_timeout' => $this->connect_timeout, + 'timeout' => $this->timeout, ]); try { $response = $guzzle->post($uri, ['form_params' => [ @@ -324,6 +336,8 @@ public function renewTokenFromRefreshToken($refreshToken = '') $headers['Connection'] = 'Keep-Alive'; $guzzle = new GuzzleClient([ 'headers' => $headers, + 'connect_timeout' => $this->connect_timeout, + 'timeout' => $this->timeout, ]); try { $response = $guzzle->post($uri, ['form_params' => [ @@ -556,6 +570,8 @@ public function api($endpoint, array $params = [], $method = Method::GET) $guzzle = new GuzzleClient([ 'base_uri' => $this->getApiRoot(), 'headers' => $headers, + 'connect_timeout' => $this->connect_timeout, + 'timeout' => $this->timeout, ]); if (!empty($params) && Method::GET === $method) { $endpoint .= '?' . build_query($params); @@ -626,7 +642,9 @@ public function upload($path) $headers['Authorization'] = 'Bearer ' . $this->accessToken->getToken(); } $guzzle = new GuzzleClient([ - 'base_uri' => $this->getApiRoot() + 'base_uri' => $this->getApiRoot(), + 'connect_timeout' => $this->connect_timeout, + 'timeout' => $this->timeout, ]); $fileinfo = pathinfo($path); $filename = preg_replace('/\W+/', '_', $fileinfo['filename']); From 191439aa97e472f66fd0bde150e9b5413bdd80bc Mon Sep 17 00:00:00 2001 From: Stiliyan Ivanov Date: Wed, 24 Feb 2021 00:39:10 +0200 Subject: [PATCH 2/3] Add setters Signed-off-by: Stiliyan Ivanov --- src/Client.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Client.php b/src/Client.php index c218c42..1e9bcc5 100644 --- a/src/Client.php +++ b/src/Client.php @@ -682,4 +682,28 @@ protected function prepareOptions(array $params, $method) } return $options; } + + /** + * @param mixed $connect_timeout + * + * @return self + */ + public function setConnectTimeout($connect_timeout) + { + $this->connect_timeout = $connect_timeout; + + return $this; + } + + /** + * @param mixed $timeout + * + * @return self + */ + public function setTimeout($timeout) + { + $this->timeout = $timeout; + + return $this; + } } From 3a76455ac08795eceb48c0a5d37549bbb39acf42 Mon Sep 17 00:00:00 2001 From: Stiliyan Ivanov Date: Wed, 24 Feb 2021 01:17:28 +0200 Subject: [PATCH 3/3] Set timeouts in construct Signed-off-by: Stiliyan Ivanov --- src/Client.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Client.php b/src/Client.php index 1e9bcc5..8124ef4 100644 --- a/src/Client.php +++ b/src/Client.php @@ -33,11 +33,13 @@ class Client /** * Float describing the number of seconds to wait while trying to connect to a server + * @var float */ private $connect_timeout = 30; /** * Float describing the total timeout of the request in seconds + * @var float */ private $timeout = 60; @@ -225,10 +227,12 @@ public function setOAuthApiRoot($oAuthApiRoot) * @param string $clientId * @param string $clientSecret */ - public function __construct($clientId = '', $clientSecret = '') + public function __construct($clientId = '', $clientSecret = '', ?float $connect_timeout = null, ?float $timeout = null) { !empty($clientId) && $this->setClientId($clientId); !empty($clientSecret) && $this->setClientSecret($clientSecret); + !is_null($connect_timeout) && $this->setConnectTimeout($connect_timeout); + !is_null($timeout) && $this->setTimeout($timeout); } /** @@ -688,7 +692,7 @@ protected function prepareOptions(array $params, $method) * * @return self */ - public function setConnectTimeout($connect_timeout) + public function setConnectTimeout(float $connect_timeout) { $this->connect_timeout = $connect_timeout; @@ -700,7 +704,7 @@ public function setConnectTimeout($connect_timeout) * * @return self */ - public function setTimeout($timeout) + public function setTimeout(float $timeout) { $this->timeout = $timeout;