Skip to content

Commit

Permalink
Added global setTimeout, and per request timeout functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
daika7ana committed Oct 9, 2024
1 parent c1ed87f commit 793101d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 16 deletions.
12 changes: 12 additions & 0 deletions src/Fancourier/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class Auth {
protected $verifyHost = true;
protected $verifyPeer = true;

protected $timeout = 6;
protected $con_timeout = 3;

protected $gateway = 'login';

public function __construct($clientId, $username, $password, $token = '')
Expand Down Expand Up @@ -65,6 +68,8 @@ private function retrieve_token()
{
$client = new Client();
$client->set_verify($this->verifyHost, $this->verifyPeer);
$client->set_timeout($this->con_timeout, $this->timeout);

$url = Fancourier::API_URL.$this->gateway;

$data = [
Expand Down Expand Up @@ -103,6 +108,13 @@ public function setVerify($host = true, $peer = true)
{
$this->verifyHost = $host;
$this->verifyPeer = $peer;
return $this;
}

public function setTimeout($con_timeout = 3, $timeout = 6)
{
$this->con_timeout = $con_timeout;
$this->timeout = $timeout;
return $this;
}
}
33 changes: 22 additions & 11 deletions src/Fancourier/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Client {
private $verify_host = true;
private $verify_peer = true;

private $timeout = 6;
private $con_timeout = 3;

private $useragent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/119.0';

private $headers = []; // custom headers
Expand All @@ -28,14 +31,14 @@ public function __construct(?string $useragent = null)
/*
* Init curl and set common options. Called by get/post functions
*/
private function init(int $con_timeout = 60, int $timeout = 600)
private function init()
{
$this->curl = curl_init();
// init default data
curl_setopt($this->curl, CURLOPT_USERAGENT, $this->useragent);

curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $con_timeout);
curl_setopt($this->curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->con_timeout);
curl_setopt($this->curl, CURLOPT_TIMEOUT, $this->timeout);

if (!$this->verify_host)
{
Expand Down Expand Up @@ -73,10 +76,10 @@ private function close()
$this->is_delete = false;
}

public function get(string $url, int $con_timeout = 60, int $timeout = 600)//: string|false
public function get(string $url)//: string|false
{
//echo '<div style="font-family:monospace; padding: 5px; border: 1px solid red; margin: 5px">'.$url.'</div>';
$this->init($con_timeout, $timeout);
$this->init();

curl_setopt($this->curl, CURLOPT_URL, $url);
//curl_setopt($this->curl, CURLOPT_HEADER, 0);
Expand Down Expand Up @@ -127,9 +130,9 @@ public static function prepare_file_string(string $data, string $postname, strin
}


public function post(string $url, array $data, int $con_timeout = 60, int $timeout = 600)
public function post(string $url, array $data)
{
$this->init($con_timeout, $timeout);
$this->init();

curl_setopt($this->curl, CURLOPT_URL, $url);
//curl_setopt($this->curl, CURLOPT_HEADER, 0);
Expand Down Expand Up @@ -160,9 +163,9 @@ public function post(string $url, array $data, int $con_timeout = 60, int $timeo
}

// curl doesn't like multilevel arrays in CURLOPT_POSTFIELDS, so we have to manually build the data with http_build_query
public function post_ma(string $url, array $data, int $con_timeout = 60, int $timeout = 600)
public function post_ma(string $url, array $data)
{
$this->init($con_timeout, $timeout);
$this->init();

$datastr = http_build_query($data, '', '&');
$datastr = str_replace(["%5B", "%5D"], ["[", "]"], $datastr);
Expand Down Expand Up @@ -200,10 +203,10 @@ public function post_ma(string $url, array $data, int $con_timeout = 60, int $ti
return false;
}

public function post_json(string $url, array $data, int $con_timeout = 60, int $timeout = 600)//: string|false
public function post_json(string $url, array $data)//: string|false
{
$this->headers_add('Content-Type', 'application/json'); // add content-type to headers
$this->init($con_timeout, $timeout);
$this->init();

curl_setopt($this->curl, CURLOPT_URL, $url);

Expand Down Expand Up @@ -320,6 +323,14 @@ public function set_verify($host = true, $peer = true)
return $this;
}

/* if you need a custom request timeout */
public function set_timeout($con_timeout = 3, $timeout = 6)
{
$this->con_timeout = $con_timeout;
$this->timeout = $timeout;
return $this;
}

public function __destruct()
{

Expand Down
18 changes: 18 additions & 0 deletions src/Fancourier/Fancourier.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class Fancourier
protected $verifyHost = true;
protected $verifyPeer = true;

protected $conTimeout = 3;
protected $timeout = 6;

public function __construct($clientId, $username, $password, $bearer_token = '')
{
$this->auth = new Auth($clientId, $username, $password, $bearer_token);
Expand All @@ -65,6 +68,20 @@ public function setVerify($verifyHost = true, $verifyPeer = true)
$this->auth->setVerify($verifyHost, $verifyPeer);
return $this;
}

/**
* Use this if you need to set a custom request timeout
* @param int $conTimeout
* @param int $timeout
*/
public function setTimeout($conTimeout = 3, $timeout = 6)
{
$this->conTimeout = $conTimeout;
$this->timeout = $timeout;
$this->auth->setTimeout($conTimeout, $timeout);
return $this;
}

/**
* @param CreateAwb $request
* @return \Fancourier\Response\CreateAwb
Expand Down Expand Up @@ -310,6 +327,7 @@ protected function send(RequestInterface $request)
return $request
->authenticate($this->auth)
->setVerify($this->verifyHost, $this->verifyPeer)
->setTimeout($this->conTimeout, $this->timeout)
->send();
}

Expand Down
29 changes: 24 additions & 5 deletions src/Fancourier/Request/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ abstract class AbstractRequest implements RequestInterface
/** @var Client */
protected $client;

protected $clientOverrides = [
'verify' => false,
'timeout' => false
];

/** @var Generic */
protected $response;

Expand All @@ -41,11 +46,25 @@ public function authenticate(Auth $auth)
return $this;
}

public function setVerify($verifyHost = true, $verifyPeer = true)
{
$this->client->set_verify($verifyHost, $verifyPeer);
return $this;
}
public function setVerify($verifyHost = true, $verifyPeer = true)
{
if ($this->clientOverrides['verify'] !== true) {
$this->client->set_verify($verifyHost, $verifyPeer);
$this->clientOverrides['verify'] = true;
}

return $this;
}

public function setTimeout($conTimeout = 3, $timeout = 6)
{
if ($this->clientOverrides['timeout'] !== true) {
$this->client->set_timeout($conTimeout, $timeout);
$this->clientOverrides['timeout'] = true;
}

return $this;
}

/**
* @return Generic
Expand Down
1 change: 1 addition & 0 deletions src/Fancourier/Request/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface RequestInterface
{
public function authenticate(Auth $auth);
public function setVerify($verifyHost, $verifyPeer);
public function setTimeout($conTimeout, $timeout);
public function send();
public function pack();
}

0 comments on commit 793101d

Please sign in to comment.