Skip to content

Commit f7141d4

Browse files
authored
Merge pull request #49 from rosette-api/RCB-454_concurrency
Updated to use the concurrency returned from Rosette API
2 parents fa33194 + 1c98e95 commit f7141d4

File tree

4 files changed

+76
-40
lines changed

4 files changed

+76
-40
lines changed

source/rosette/api/Api.php

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,6 @@ class Api
9696
*/
9797
private $ms_between_retries;
9898

99-
/**
100-
* request override - for testing
101-
*
102-
* @RosetteRequest
103-
*/
104-
private $mock_request;
105-
10699
/**
107100
* internal options array
108101
* @var array
@@ -115,6 +108,11 @@ class Api
115108
*/
116109
private $customHeaders;
117110

111+
/**
112+
* internal Request object
113+
* @var array
114+
*/
115+
private $request;
118116

119117
/**
120118
* Create an L{API} object.
@@ -143,7 +141,7 @@ public function __construct($user_key, $service_url = 'https://api.rosette.com/
143141
$this->subUrl = null;
144142
$this->max_retries = 5;
145143
$this->ms_between_retries = 500000;
146-
$this->mock_request = null;
144+
$this->request = new RosetteRequest();
147145
$this->options = array();
148146
}
149147

@@ -154,7 +152,7 @@ public function __construct($user_key, $service_url = 'https://api.rosette.com/
154152
*/
155153
public function setMockRequest($requestObject)
156154
{
157-
$this->mock_request = $requestObject;
155+
$this->request = $requestObject;
158156
}
159157

160158
/**
@@ -368,6 +366,17 @@ public function clearCustomHeaders()
368366
}
369367

370368

369+
/**
370+
* Returns the max connections (concurrency)
371+
*
372+
* @return int
373+
*/
374+
public function getMaxConnections()
375+
{
376+
return $this->request->getMaxConnections();
377+
}
378+
379+
371380
/**
372381
* Replaces a header item with a new one
373382
*/
@@ -448,25 +457,25 @@ private function callEndpoint($parameters, $subUrl)
448457
*/
449458
private function makeRequest($url, $headers, $data, $method)
450459
{
451-
$request = $this->mock_request != null ? $this->mock_request : new RosetteRequest();
452460
for ($retries = 0; $retries < $this->max_retries; $retries++) {
453-
if ($request->makeRequest($url, $headers, $data, $method) === false) {
454-
throw new RosetteException($request->getResponseError);
461+
if ($this->request->makeRequest($url, $headers, $data, $method) === false) {
462+
throw new RosetteException($this->request->getResponseError);
455463
} else {
456-
$this->setResponseCode($request->getResponseCode());
464+
$this->setResponseCode($this->request->getResponseCode());
457465
if ($this->getResponseCode() === 429) {
466+
print('429 RETRY');
458467
usleep($this->ms_between_retries);
459468
continue;
460469
} elseif ($this->getResponseCode() !== 200) {
461-
throw new RosetteException($request->getResponse()['message'], $this->getResponseCode());
470+
throw new RosetteException($this->request->getResponse()['message'], $this->getResponseCode());
462471
}
463-
return $request->getResponse();
472+
return $this->request->getResponse();
464473
}
465474
}
466475
if ($this->getResponseCode() !== 200) {
467-
throw new RosetteException($request->getResponse()['message'], $this->getResponseCode());
476+
throw new RosetteException($this->request->getResponse()['message'], $this->getResponseCode());
468477
} else {
469-
return $request->getResponse();
478+
return $this->request->getResponse();
470479
}
471480
}
472481

@@ -607,24 +616,25 @@ public function morphology($params, $facet = null)
607616
return $this->callEndpoint($params, 'morphology/' . $facet);
608617
}
609618

610-
/* Calls the entities endpoint.
611-
*
612-
* @param $params
613-
* @param $resolve_entities
614-
*
615-
* @return mixed
616-
*
617-
* @throws RosetteException
618-
*/
619-
public function entities($params, $resolve_entities = false)
620-
{
619+
/**
620+
* Calls the entities endpoint.
621+
*
622+
* @param $params
623+
* @param $resolve_entities
624+
*
625+
* @return mixed
626+
*
627+
* @throws RosetteException
628+
*/
629+
public function entities($params, $resolve_entities = false)
630+
{
621631
if ($resolve_entities == true) {
622632
error_reporting(E_DEPRECATED);
623633
return $this->callEndpoint($params, 'entities/linked');
624634
} else {
625635
return $this->callEndpoint($params, 'entities');
626636
}
627-
}
637+
}
628638

629639

630640
/**

source/rosette/api/RosetteRequest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ class RosetteRequest
6666
* @var bool
6767
*/
6868
private $initialized;
69+
/**
70+
* Maximum connections
71+
*
72+
* @var int
73+
*/
74+
private $max_connections;
6975

7076
/**
7177
* class constructor
@@ -74,6 +80,7 @@ class RosetteRequest
7480
public function __construct()
7581
{
7682
$this->initialized = false;
83+
$this->max_connections = 1;
7784
}
7885

7986
/**
@@ -116,6 +123,7 @@ public function makeRequest($url, $headers, $data, $method)
116123
curl_setopt($this->curl_handle, CURLOPT_HTTPGET, true);
117124
}
118125

126+
curl_setopt($this->curl_handle, CURLOPT_MAXCONNECTS, $this->max_connections);
119127
curl_setopt($this->curl_handle, CURLOPT_HEADER, 1);
120128
curl_setopt($this->curl_handle, CURLOPT_RETURNTRANSFER, true);
121129
$this->response = curl_exec($this->curl_handle);
@@ -195,6 +203,14 @@ public function getResponse()
195203
{
196204
if ($this->response !== false) {
197205
$response = [ 'headers' => $this->headersToArray() ];
206+
207+
if (array_key_exists('X-RosetteAPI-Concurrency', $response['headers'])) {
208+
$concurrency = $response['headers']['X-RosetteAPI-Concurrency'];
209+
if ($concurrency != $this->max_connections) {
210+
$this->max_connections = $concurrency;
211+
}
212+
}
213+
198214
$responseBody = $this->getResponseBody();
199215
if (empty($responseBody)) {
200216
$response = array_merge($response, [ 'body' => 'empty' ]);
@@ -208,6 +224,15 @@ public function getResponse()
208224
return $response;
209225
}
210226

227+
/**
228+
* Gets the maximum number of connections allowed
229+
*
230+
* @return int
231+
*/
232+
public function getMaxConnections()
233+
{
234+
return $this->max_connections;
235+
}
211236

212237
/**
213238
* function headersToArray

spec/rosette/api/ApiSpec.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
namespace spec\rosette\api;
3+
34
use PhpSpec\ObjectBehavior;
45
use Prophecy\Argument;
6+
57
class ApiSpec extends ObjectBehavior
68
{
79
public function let()
@@ -51,6 +53,10 @@ public function it_sets_gets_debug()
5153
$this->setDebug($debug);
5254
$this->getDebug()->shouldBe($debug);
5355
}
56+
public function it_gets_max_connections()
57+
{
58+
$this->getMaxConnections()->shouldBe(1);
59+
}
5460
public function it_can_ping($request)
5561
{
5662
$request->beADoubleOf('rosette\api\RosetteRequest');
@@ -230,4 +236,4 @@ public function it_fails_with_409_response($params, $request)
230236
$this->setMockRequest($request);
231237
$this->shouldThrow('rosette\api\RosetteException')->duringRelationships($params);
232238
}
233-
}
239+
}

spec/rosette/api/RosetteRequestSpec.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
<?php
2-
32
namespace spec\rosette\api;
43

54
use PhpSpec\ObjectBehavior;
65
use Prophecy\Argument;
76

87
class RosetteRequestSpec extends ObjectBehavior
98
{
10-
public function let()
11-
{
12-
$headers = array("X-RosetteAPI-Key: user_key",
13-
"Content-Type: application/json",
14-
"Accept: application/json",
15-
"Accept-Encoding: gzip",);
16-
$this->beConstructedWith('https://api.rosette.com/rest/v1', null, $headers, 'GET');
17-
}
18-
199
public function it_is_initializable()
2010
{
2111
$this->shouldHaveType('\rosette\api\RosetteRequest');
@@ -35,4 +25,9 @@ public function it_returns_a_response()
3525
{
3626
$this->getResponse()->shouldBeArray();
3727
}
28+
29+
public function it_gets_max_connections()
30+
{
31+
$this->getMaxConnections()->shouldBe(1);
32+
}
3833
}

0 commit comments

Comments
 (0)