Skip to content

Commit

Permalink
Timeout support
Browse files Browse the repository at this point in the history
  • Loading branch information
furqansiddiqui committed Dec 22, 2019
1 parent ecdb48f commit fa73573
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class HttpClient
{
public const VERSION = "0.4.4";
public const VERSION = "0.4.5";
public const REQUEST_METHODS = ["GET", "POST", "PUT", "DELETE"];

// HTTP version
Expand Down
28 changes: 28 additions & 0 deletions src/JSON_RPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
namespace HttpClient;

use HttpClient\Exception\JSON_RPC_Exception;
use HttpClient\Exception\RequestException;

/**
* Class JSON_RPC
* @package HttpClient
* @property-read string $specification
* @property-read string $version
* @property-read int|null $timeOut
* @property-read int|null $connectTimeout
*/
class JSON_RPC
{
Expand All @@ -26,6 +29,10 @@ class JSON_RPC
private $auth;
/** @var null|SSL */
private $ssl;
/** @var int|null */
private $timeOut;
/** @var int|null */
private $connectTimeout;

/**
* JSON_RPC constructor.
Expand All @@ -51,11 +58,32 @@ public function __get(string $prop)
case "specification":
case "version":
return $this->_spec;
case "timeOut":
case "connectTimeout":
return $this->$prop;
}

return null;
}

/**
* @param int|null $timeOut
* @param int|null $connectTimeout
* @return $this
* @throws JSON_RPC_Exception
*/
public function setTimeout(?int $timeOut = null, ?int $connectTimeout = null): self
{
$this->timeOut = $timeOut > 0 ? $timeOut : null;
$this->connectTimeout = $connectTimeout > 0 ? $connectTimeout : null;

if ($this->connectTimeout > $this->timeOut) {
throw new JSON_RPC_Exception('connectTimeout value cannot exceed timeOut');
}

return $this;
}

/**
* @return string
*/
Expand Down
3 changes: 3 additions & 0 deletions src/JSON_RPC/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ public function send(): Response
}
}

// Timeouts?
$req->setTimeout($this->_client->timeOut, $this->_client->connectTimeout);

// Set Authentication and SSL/TLS config
call_user_func_array([$this->_client, "prepare_req_objs"], [$req]);

Expand Down
36 changes: 36 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class Request
private $auth;
/** @var null|string */
private $userAgent;
/** @var int|null */
private $timeOut;
/** @var int|null */
private $connectTimeout;

/**
* Request constructor.
Expand Down Expand Up @@ -98,6 +102,29 @@ public function __call(string $method, $arguments)
throw new RequestException('Cannot call inaccessible method');
}

/**
* @param int|null $timeOut
* @param int|null $connectTimeout
* @return $this
* @throws RequestException
*/
public function setTimeout(?int $timeOut = null, ?int $connectTimeout = null): self
{
if ($timeOut > 0) {
$this->timeOut = $timeOut;
}

if ($connectTimeout > 0) {
$this->connectTimeout = $connectTimeout;
}

if ($connectTimeout > $timeOut) {
throw new RequestException('connectTimeout value cannot exceed timeOut');
}

return $this;
}

/**
* @param string $url
* @return Request
Expand Down Expand Up @@ -268,6 +295,15 @@ public function send(): HttpClientResponse
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
}

// Timeouts
if ($this->timeOut) {
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeOut);
}

if ($this->connectTimeout) {
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
}

// Finalise request
$responseHeaders = [];
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Expand Down

0 comments on commit fa73573

Please sign in to comment.