Skip to content

Commit 8425215

Browse files
author
Chris Park
committed
Merge branch 'develop'
2 parents 925a481 + 1e7673f commit 8425215

File tree

7 files changed

+136
-48
lines changed

7 files changed

+136
-48
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rosette/api",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "Rosette API PHP client SDK",
55
"license": "Apache",
66
"keywords": [

docker/Dockerfile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ RUN apt-get -y update && apt-get install -y \
1414

1515
RUN LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php5-5.6
1616
RUN apt-get -y update && apt-get install -y \
17-
php5-dev \
18-
php5-cli \
19-
php5-curl \
20-
libapache2-mod-php5
17+
php-dev \
18+
php-cli \
19+
php-curl \
20+
libapache2-mod-php
2121

2222

23-
RUN echo "memory_limit=-1" >> /etc/php5/cli/php.ini && \
24-
echo "date.timezone=America/New_York" >> /etc/php5/cli/php.ini
23+
RUN echo "memory_limit=-1" >> /etc/php/7.0/cli/php.ini && \
24+
echo "date.timezone=America/New_York" >> /etc/php/7.0/cli/php.ini
2525

2626
RUN wget -O /usr/local/bin/composer https://getcomposer.org/composer.phar && \
2727
chmod +x /usr/local/bin/composer

examples/text_embedding.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* Example code to call Rosette API to get text vectors from sample text
5+
**/
6+
require_once dirname(__FILE__) . '/vendor/autoload.php';
7+
use rosette\api\Api;
8+
use rosette\api\DocumentParameters;
9+
use rosette\api\RosetteException;
10+
11+
$options = getopt(null, array('key:', 'url::'));
12+
if (!isset($options['key'])) {
13+
echo 'Usage: php ' . __FILE__ . " --key <api_key> --url=<alternate_url>\n";
14+
exit();
15+
}
16+
$embeddings_data = "Cambridge, Massachusetts";
17+
$api = isset($options['url']) ? new Api($options['key'], $options['url']) : new Api($options['key']);
18+
$params = new DocumentParameters();
19+
$content = $embeddings_data;
20+
$params->set('content', $content);
21+
22+
try {
23+
$result = $api->textEmbedding($params, false);
24+
var_dump($result);
25+
} catch (RosetteException $e) {
26+
error_log($e);
27+
}

source/rosette/api/Api.php

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Api
4545
*
4646
* @var string
4747
*/
48-
private static $binding_version = '1.2.0';
48+
private static $binding_version = '1.3.0';
4949

5050
/**
5151
* User key (required for Rosette API).
@@ -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
/**
@@ -696,4 +706,18 @@ public function relationships($params)
696706
{
697707
return $this->callEndpoint($params, 'relationships');
698708
}
709+
710+
/**
711+
* Calls the text-embedding endpoint.
712+
*
713+
* @param $params
714+
*
715+
* @return mixed
716+
*
717+
* @throws RosetteException
718+
*/
719+
public function textEmbedding($params)
720+
{
721+
return $this->callEndpoint($params, 'text-embedding');
722+
}
699723
}

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: 18 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');
@@ -197,6 +203,17 @@ public function it_calls_the_relationships_endpoint($params, $request)
197203
$this->setMockRequest($request);
198204
$this->relationships($params)->shouldHaveKeyWithValue('name', 'Rosette API');
199205
}
206+
public function it_calls_the_text_embedding_endpoint($params, $request)
207+
{
208+
$params->beADoubleOf('\rosette\api\DocumentParameters');
209+
$params->contentUri = 'http://some.dummysite.com';
210+
$request->beADoubleOf('rosette\api\RosetteRequest');
211+
$request->makeRequest(Argument::any(), Argument::any(), Argument::any(), Argument::any())->willReturn(true);
212+
$request->getResponseCode()->willReturn(200);
213+
$request->getResponse()->willReturn([ 'name' => 'Rosette API']);
214+
$this->setMockRequest($request);
215+
$this->textEmbedding($params)->shouldHaveKeyWithValue('name', 'Rosette API');
216+
}
200217
public function it_fails_with_non_200_response($params, $request)
201218
{
202219
$params->beADoubleOf('\rosette\api\DocumentParameters');
@@ -230,4 +247,4 @@ public function it_fails_with_409_response($params, $request)
230247
$this->setMockRequest($request);
231248
$this->shouldThrow('rosette\api\RosetteException')->duringRelationships($params);
232249
}
233-
}
250+
}

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)