Skip to content

Commit 6d264cf

Browse files
Merge pull request #85 from rosette-api/ws-3224-add-timeout
WS-3224: request timeout configurable
2 parents 2aa04c7 + 4f7c995 commit 6d264cf

3 files changed

Lines changed: 49 additions & 28 deletions

File tree

source/rosette/api/Api.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ class Api
122122
*/
123123
private $ms_between_retries;
124124

125+
/**
126+
* The maximum time in seconds to wait for the response to arrive
127+
*
128+
* @var null|int
129+
*/
130+
private $timeout;
131+
125132
/**
126133
* Create an L{API} object.
127134
*
@@ -147,6 +154,7 @@ public function __construct($user_key, $service_url = 'https://api.rosette.com/r
147154
$this->options = array();
148155
$this->url_params = array();
149156
$this->customHeaders = array();
157+
$this->timeout = null;
150158
}
151159

152160
/**
@@ -194,6 +202,15 @@ public function setMillisecondsBetweenRetries($ms_between_retries)
194202
{
195203
$this->ms_between_retries = $ms_between_retries;
196204
}
205+
/**
206+
* Sets the maximum time in seconds to wait for the response to arrive
207+
*
208+
* @param int $timeout
209+
*/
210+
public function setTimeout($timeout)
211+
{
212+
$this->timeout = $timeout;
213+
}
197214

198215
/**
199216
* Returns response code.
@@ -469,7 +486,7 @@ private function callEndpoint($parameters, $subUrl)
469486
*/
470487
private function makeRequest($url, $headers, $data, $method)
471488
{
472-
if ($this->request->makeRequest($url, $headers, $data, $method, $this->url_params) === false) {
489+
if ($this->request->makeRequest($url, $headers, $data, $method, $this->timeout, $this->url_params) === false) {
473490
throw new RosetteException($this->request->getResponseError());
474491
} else {
475492
$this->setResponseCode($this->request->getResponseCode());

source/rosette/api/RosetteRequest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,22 @@ public function __destruct()
7272
*
7373
* @return bool
7474
*/
75-
public function makeRequest($url, $headers, $data, $method, $url_params = null)
75+
public function makeRequest($url, $headers, $data, $method, $timeout, $url_params = null)
7676
{
7777
// Unfortunately, the 'options' argument for post and get is NOT for
7878
// query parameters (as it is in Python). Hence, the construction.
7979
if (!is_null($url_params)) {
8080
$url = $url . '?' . http_build_query($url_params);
8181
}
82+
if (is_null($timeout)) {
83+
$timeout = 30;
84+
}
85+
$options = ['timeout' => $timeout]; // Set the timeout here
8286
try {
8387
if ($method === 'POST') {
84-
$this->response = Requests::post($url, $headers, $data);
88+
$this->response = Requests::post($url, $headers, $data, $options);
8589
} elseif ($method === 'GET') {
86-
$this->response = Requests::get($url, $headers);
90+
$this->response = Requests::get($url, $headers, $options);
8791
}
8892
return true;
8993
} catch (RequestsException $e) {

0 commit comments

Comments
 (0)