Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
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;

/**
* Grant type for authorization code
*/
Expand Down Expand Up @@ -215,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);
}

/**
Expand Down Expand Up @@ -286,6 +300,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' => [
Expand Down Expand Up @@ -324,6 +340,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' => [
Expand Down Expand Up @@ -556,6 +574,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);
Expand Down Expand Up @@ -626,7 +646,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']);
Expand Down Expand Up @@ -664,4 +686,28 @@ protected function prepareOptions(array $params, $method)
}
return $options;
}

/**
* @param mixed $connect_timeout
*
* @return self
*/
public function setConnectTimeout(float $connect_timeout)
{
$this->connect_timeout = $connect_timeout;

return $this;
}

/**
* @param mixed $timeout
*
* @return self
*/
public function setTimeout(float $timeout)
{
$this->timeout = $timeout;

return $this;
}
}