Skip to content

Commit a5f6aae

Browse files
authored
Merge pull request #7 from postplanner/feature/timeouts
Add timeouts
2 parents 32e519d + 3a76455 commit a5f6aae

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

src/Client.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@
3131
class Client
3232
{
3333

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+
3446
/**
3547
* Grant type for authorization code
3648
*/
@@ -215,10 +227,12 @@ public function setOAuthApiRoot($oAuthApiRoot)
215227
* @param string $clientId
216228
* @param string $clientSecret
217229
*/
218-
public function __construct($clientId = '', $clientSecret = '')
230+
public function __construct($clientId = '', $clientSecret = '', ?float $connect_timeout = null, ?float $timeout = null)
219231
{
220232
!empty($clientId) && $this->setClientId($clientId);
221233
!empty($clientSecret) && $this->setClientSecret($clientSecret);
234+
!is_null($connect_timeout) && $this->setConnectTimeout($connect_timeout);
235+
!is_null($timeout) && $this->setTimeout($timeout);
222236
}
223237

224238
/**
@@ -286,6 +300,8 @@ public function getAccessToken($code = '')
286300
$headers['Connection'] = 'Keep-Alive';
287301
$guzzle = new GuzzleClient([
288302
'headers' => $headers,
303+
'connect_timeout' => $this->connect_timeout,
304+
'timeout' => $this->timeout,
289305
]);
290306
try {
291307
$response = $guzzle->post($uri, ['form_params' => [
@@ -324,6 +340,8 @@ public function renewTokenFromRefreshToken($refreshToken = '')
324340
$headers['Connection'] = 'Keep-Alive';
325341
$guzzle = new GuzzleClient([
326342
'headers' => $headers,
343+
'connect_timeout' => $this->connect_timeout,
344+
'timeout' => $this->timeout,
327345
]);
328346
try {
329347
$response = $guzzle->post($uri, ['form_params' => [
@@ -556,6 +574,8 @@ public function api($endpoint, array $params = [], $method = Method::GET)
556574
$guzzle = new GuzzleClient([
557575
'base_uri' => $this->getApiRoot(),
558576
'headers' => $headers,
577+
'connect_timeout' => $this->connect_timeout,
578+
'timeout' => $this->timeout,
559579
]);
560580
if (!empty($params) && Method::GET === $method) {
561581
$endpoint .= '?' . build_query($params);
@@ -626,7 +646,9 @@ public function upload($path)
626646
$headers['Authorization'] = 'Bearer ' . $this->accessToken->getToken();
627647
}
628648
$guzzle = new GuzzleClient([
629-
'base_uri' => $this->getApiRoot()
649+
'base_uri' => $this->getApiRoot(),
650+
'connect_timeout' => $this->connect_timeout,
651+
'timeout' => $this->timeout,
630652
]);
631653
$fileinfo = pathinfo($path);
632654
$filename = preg_replace('/\W+/', '_', $fileinfo['filename']);
@@ -664,4 +686,28 @@ protected function prepareOptions(array $params, $method)
664686
}
665687
return $options;
666688
}
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+
}
667713
}

0 commit comments

Comments
 (0)