Skip to content

Commit 128580a

Browse files
committed
Merge branch 'develop'
2 parents 7f440c4 + 9b87190 commit 128580a

File tree

7 files changed

+103
-91
lines changed

7 files changed

+103
-91
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ script: phpunit --bootstrap ./vendor/autoload.php ./tests/rosette/api/ApiTest.ph
1515
notifications:
1616
slack:
1717
rooms:
18-
- secure: 4FRaTAAiYyeUvgw2RhmblgbNiJO4wmd34OBgWcwURjP9oVmFfSwR9r1LNCdUGxrPOghexSY2DjXIuvIrfTfi/xYbhHb3Kw7PEAyB8IuBMlKtY4NSFou62S2VhYpxyg58T+C7P2zi0eDnDE06pwTCoGPaimxMZQY91yQ0yPYDPVXbwe5SjEgamzlwGBxlS/0A6w1iCPHg27/iO2hXtdW3oLS2I0F/Q8Q95RBkX9hpg6yqHlTV7jRbSqvQ9OFBqk/tXMHQvhoPDGgCgQDuykJuaAYx7g9d0YL0eEYYOh9B/TJ/kNOwdRFBu5kuQ2/nFS5Z0S3Y3UIhdYjUmm9gSMnwIbYnrW22EqDJLoT9Zi3Gv7Prg/8/fSkWsof7BJTMSuXUqO1AxDGKIxFv9uSF1daZoY+AC1ooU1xDu1nNvWVYPlkwEdDxxmHpFkGT3ESTZYccPovQl8Z5K0I1BBAVdJKDzm07lE6VHbxkKcvK6gG0TN3uLxnSlQtjkfJ+aVMq1kxeVsB9lEsKs9oezsKzzbftMm525aXPg+OAv+31CUFWxvT/p4ps8Q+AV6aZpoPHkpK8VryyNirUeZ/m4m4ebDHhD9vcN+JqE9gzshT+0U3g19SvLiUMQtbuZ2BUvrq2hh2LEGs03AFZaNg9AEUVA1PQRhV5NILyoS/lbiBYJPT39Sg=
18+
# - secure: 4FRaTAAiYyeUvgw2RhmblgbNiJO4wmd34OBgWcwURjP9oVmFfSwR9r1LNCdUGxrPOghexSY2DjXIuvIrfTfi/xYbhHb3Kw7PEAyB8IuBMlKtY4NSFou62S2VhY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cCH9avcaHqMLPLHht0z+8inVlDZeTcN+Wi8tk1YXwuQYUkTW46iFmoZNGdygRjTu43h9hCqdG7vcoKrR8oDbMT8X/sfs+z/UaBbuYBr9dT8rrdKJoa9115PaYpCs529DLrw1iGmzr5CkAXDElc1y2F352nwVS+dX7/OIQz1/dbT1ozWQ9Blx6ez5XN84=
2020
# - secure: g/eOC2QCn7oUW234u/P1kmh9OASO9t5g6iC+DflNdnedHnpw8IEG9CsLAnNynOY0KvC/2/Q6BAIFpT/qO8BPdaC2eQnd9LDurOXExxvjKZgTujhqgzijJGzVPSL8//hafj0Ec2+iJZFo/DYMPqUpYDsiKxX53+SMz9/GN/PKVKsI7bzuaOm04xXktU0A76pVfaG0N/qFSVHI64SShACoUXRpTzyUbmksRgzk7FB162R+TH1soPsR3vPh8c2SSWC9msrDc4iljcBhSFTdjL68z+srjDPYMSoOcFXXFNP+dmL+Q0veL/E4e40e7CWIU1O3grOcEcCkaSoZVSpGduNnCst8h6MpgauPtrgwHk4zGMoSl+L6al+nFo/3h2dXeebrQ0tY/hRfZi4Q8xwqG9083TBqi71fTpoFZ7sNtrY4Kdtl5Oa2CFUo7lVn1JB9qQwSa0eai3Whv0RyRqqQe77aDUj0dfD2R3Q61rX5OF/f++W0XtNwHQubzmj8HD/cFneShIQsbl9KgYXoR6HiXcbBiNdmmZjSrzkPYh7vlsujYrz6Cg2msKybWJ/FfOz0tS1cjlCtiCMOTExN6tEF8YCp9l+s+5RLe61pgFPy4Snr9pEjuTS5DYliTyZMY2ZZC1clBQtgE8E2qCG1QSzqnqqiGxj2K4zmLPpB4y1XpW8e3yk=
2121
on_success: always

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.0.2",
3+
"version": "1.0.3",
44
"description": "Rosette API PHP client SDK",
55
"license": "Apache",
66
"keywords": [

examples/name_translation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
$params = new NameTranslationParameters();
1919
$params->set('name', $name_translation_data);
2020
$params->set('targetLanguage', 'eng');
21-
$params->set ('targetScript', 'Latn');
22-
$params->set ('targetScheme', 'IC');
21+
$params->set('targetScript', 'Latn');
22+
$params->set('targetScheme', 'IC');
2323
$params->set('sourceLanguageOfOrigin', 'ara');
2424
$params->set('sourceLanguageOfUse', 'ara');
2525

examples/sentiment.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,3 @@
3030
}
3131

3232
fclose($temp); // clean up the temp file
33-

source/rosette/api/Api.php

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ private function finishResult($resultObject, $action)
259259
: failed to communicate with Api: ' . $msg,
260260
is_numeric($serverCode) ? $serverCode : RosetteException::$BAD_REQUEST_FORMAT
261261
);
262-
263262
}
264263
}
265264

@@ -342,8 +341,6 @@ public function checkVersion($url, $versionToCheck = null)
342341
$versionToCheck = self::$binding_version;
343342
}
344343
$resultObject = $this->postHttp($url . "info?clientVersion=$versionToCheck", $this->headers, null);
345-
$resultObject = array_pop((array_slice($resultObject, -1)));
346-
$resultObject = (array) json_decode($resultObject);
347344

348345
if (array_key_exists('versionChecked', $resultObject) && $resultObject['versionChecked'] === true) {
349346
$this->version_checked = true;
@@ -358,6 +355,34 @@ public function checkVersion($url, $versionToCheck = null)
358355
return $this->version_checked;
359356
}
360357

358+
/**
359+
* function headersToArray
360+
*
361+
* Converts the http response header string to an associative array
362+
*
363+
* @param $headers
364+
*
365+
* @returns associative array of headers
366+
*/
367+
public function headersToArray($headers)
368+
{
369+
$head = array();
370+
foreach ($headers as $k=>$v) {
371+
$t = explode(':', $v, 2);
372+
if (isset($t[1])) {
373+
$head[ trim($t[0]) ] = trim($t[1]);
374+
} else {
375+
if (strlen(trim($v)) > 0) {
376+
$head[] = $v;
377+
}
378+
if (preg_match("#HTTP/[0-9\.]+\s+([0-9]+)#", $v, $out)) {
379+
$head['response_code'] = intval($out[1]);
380+
}
381+
}
382+
}
383+
return $head;
384+
}
385+
361386
/**
362387
* function makeRequest.
363388
*
@@ -394,7 +419,7 @@ private function makeRequest($url, $headers, $data, $method)
394419
}
395420

396421
foreach ($data as $v) {
397-
$data = array_filter($data, function ($v) {
422+
$data = array_filter($data, function ($v) {
398423
if ($v !== null || $v !== "") {
399424
return $v;
400425
}
@@ -422,21 +447,23 @@ private function makeRequest($url, $headers, $data, $method)
422447
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
423448
$response = curl_exec($ch);
424449
$resCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
450+
$this->setResponseCode($resCode);
425451
if ($response === false) {
426452
echo curl_errno($ch);
427-
echo curl_error($ch);
428453
}
454+
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
455+
$header = explode(PHP_EOL, substr($response, 0, $header_size));
456+
$body = substr($response, $header_size);
457+
429458
curl_close($ch);
430-
$response = explode(PHP_EOL, $response);
431-
$this->setResponseCode($resCode);
432459

433-
if (array_key_exists(9, $response)) {
434-
if (strlen($response[9]) > 3 && mb_strpos($response[9], "\x1f" . "\x8b" . "\x08", 0) === 0) {
435-
// a gzipped string starts with ID1(\x1f) ID2(\x8b) CM(\x08)
436-
// http://www.gzip.org/zlib/rfc-gzip.html#member-format
437-
$response = gzinflate(substr($response, 10, -8));
438-
}
460+
if (strlen($body) > 3 && mb_strpos($body, "\x1f" . "\x8b" . "\x08", 0) === 0) {
461+
// a gzipped string starts with ID1(\x1f) ID2(\x8b) CM(\x08)
462+
// http://www.gzip.org/zlib/rfc-gzip.html#member-format
463+
$body = gzinflate(substr($body, 10, -8));
439464
}
465+
$response = [ 'headers' => $this->headersToArray($header) ];
466+
$response = array_merge($response, json_decode($body, true));
440467
if ($this->getResponseCode() < 500) {
441468
return $response;
442469
}
@@ -482,27 +509,6 @@ public function getResponseStatusCode($header_str)
482509
}
483510
}
484511

485-
/**
486-
* Creates the header string that is acceptable to file_get_contents.
487-
*
488-
* @param $headers
489-
*
490-
* @return string
491-
*/
492-
private function headersAsString($headers)
493-
{
494-
return implode(
495-
"\r\n",
496-
array_map(
497-
function ($k, $v) {
498-
return "$k: $v";
499-
},
500-
array_keys($headers),
501-
array_values($headers)
502-
)
503-
);
504-
}
505-
506512
/**
507513
* Standard GET helper.
508514
*

source/rosette/api/Name.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Class that represents a Name.
2020
*/
21-
class Name
21+
class Name extends RosetteParamsSetBase
2222
{
2323
/**
2424
* Textual form of the name.
@@ -69,4 +69,12 @@ public function __construct($text, $entityType = null, $language = null, $script
6969
$this->language = $language;
7070
$this->script = $script;
7171
}
72+
73+
/**
74+
* required validate function
75+
*/
76+
public function validate()
77+
{
78+
// nothing to validate
79+
}
7280
}

tests/rosette/api/ApiTest.php

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,20 @@
2929

3030
function curl_exec($ch)
3131
{
32-
$data = array ('response'=>'{"content": "Mocked response content"}');
33-
return '{"content": "Mocked response content"}';
32+
// mock response
33+
// Note: The X's are set to a length to make the header = 200, which is necessary to force a correct
34+
// boundary between the header and body.
35+
$mock_response = 'HTTP/1.1 200 OK'.PHP_EOL;
36+
$mock_response .= 'Content-Type: application/json'.PHP_EOL;
37+
$mock_response .= 'Date: Thu, 31 Mar 2016 13:12:00 GMT'.PHP_EOL;
38+
$mock_response .= 'Server: openresty/1.9.7.3'.PHP_EOL;
39+
$mock_response .= 'X-RosetteAPI-Request-Id: XXXXXXXXXXXXXXXXXXXXXXXX'.PHP_EOL;
40+
$mock_response .= 'Content-Length: 95'.PHP_EOL;
41+
$mock_response .= 'Connection: keep-alive'.PHP_EOL;
42+
$mock_response .= PHP_EOL;
43+
$mock_response .= '{"name":"Rosette API","version":"0.10.3","buildNumber":"","buildTime":"","versionChecked":true}';
44+
45+
return $mock_response;
3446
}
3547

3648
function curl_getinfo($ch)
@@ -71,9 +83,6 @@ public static function setupBeforeClass()
7183
*/
7284
private function getMockedResponse($filename)
7385
{
74-
//$response = json_decode(\file_get_contents(self::$responseDir . $filename . '.json'), true);
75-
76-
//return $response;
7786
return curl_exec($ch = null);
7887
}
7988

@@ -86,7 +95,6 @@ private function getMockedResponse($filename)
8695
*/
8796
private function getMockedResponseCode($filename)
8897
{
89-
//return intval(\file_get_contents(self::$responseDir . $filename . '.status'));
9098
return curl_getinfo($ch = null);
9199
}
92100

@@ -110,37 +118,37 @@ private function setUpApi($userKey)
110118

111119
/**
112120
* @group posts
113-
* @expectedException \rosette\api\RosetteException
114121
*/
115122
public function testCheckVersion()
116123
{
117124
$this->userKey = 'checkVersion';
118125
$api = $this->setUpApi($this->userKey);
119-
$api->checkVersion('http://rosette.basistech.com');
126+
$result = $api->checkVersion('http://rosette.basistech.com');
127+
$this->assertTrue($result);
120128
}
121129

122130
/**
123131
* @group gets
124132
*/
125133
public function testInfo()
126134
{
127-
$expected = $this->getMockedResponse('info');
135+
$expected = "Rosette API";
128136
$this->userKey = 'info';
129137
$api = $this->setUpApi($this->userKey);
130138
$result = $api->info();
131-
$this->assertSame($expected, $result[0]);
139+
$this->assertSame($expected, $result["name"]);
132140
}
133141

134142
/**
135143
* @group gets
136144
*/
137145
public function testPing()
138146
{
139-
$expected = $this->getMockedResponse('ping');
147+
$expected = "Rosette API";
140148
$this->userKey = 'ping';
141149
$api = $this->setUpApi($this->userKey);
142150
$result = $api->ping();
143-
$this->assertSame($expected, $result[0]);
151+
$this->assertSame($expected, $result["name"]);
144152
}
145153

146154
/**
@@ -198,7 +206,7 @@ public function testEndpoints($filename, $endpoint)
198206
$api->skipVersionCheck(); // need to set it so it doesn't call the mocked info()
199207
$api->setDebug(true);
200208
$input = $this->getRequestData($this->userKey);
201-
$expected = $this->getMockedResponse($this->userKey);
209+
$expected = "Rosette API";
202210
if ($endpoint === 'name-similarity') {
203211
$sourceName = new Name(
204212
$input['name1']['text'],
@@ -232,43 +240,34 @@ public function testEndpoints($filename, $endpoint)
232240
// If it does not throw an exception, check that it was not supposed to and if so check that it
233241
// returns the correct thing.
234242
// If it throws an exception, check that it was supposed to and if so pass otherwise fail test.
235-
//try {
236-
$result = '';
237-
if ($endpoint === 'categories') {
238-
$result = $api->categories($params);
239-
}
240-
if ($endpoint === 'entities') {
241-
$result = $api->entities($params);
242-
}
243-
if ($endpoint === 'entities_linked') {
244-
$result = $api->entities($params, true);
245-
}
246-
if ($endpoint === 'language') {
247-
$result = $api->language($params);
248-
}
249-
if ($endpoint === 'name-similarity') {
250-
$result = $api->nameSimilarity($params);
251-
}
252-
if ($endpoint === 'morphology_complete') {
253-
$result = $api->morphology($params);
254-
}
255-
if ($endpoint === 'sentiment') {
256-
$result = $api->sentiment($params);
257-
}
258-
if ($endpoint === 'name-translation') {
259-
$result = $api->nameTranslation($params);
260-
}
261-
if ($endpoint === 'relationships') {
262-
$result = $api->relationships($params);
263-
}
264-
// If there is a "code" key, it means an exception should be thrown
265-
//if (!array_key_exists('code', $expected)) {
266-
$this->assertEquals($expected, $result[0]);
267-
268-
//}
269-
//} catch (RosetteException $exception) {
270-
// $this->assertSame('unsupportedLanguage', $expected['code']);
271-
//}
243+
$result = '';
244+
if ($endpoint === 'categories') {
245+
$result = $api->categories($params);
246+
}
247+
if ($endpoint === 'entities') {
248+
$result = $api->entities($params);
249+
}
250+
if ($endpoint === 'entities_linked') {
251+
$result = $api->entities($params, true);
252+
}
253+
if ($endpoint === 'language') {
254+
$result = $api->language($params);
255+
}
256+
if ($endpoint === 'name-similarity') {
257+
$result = $api->nameSimilarity($params);
258+
}
259+
if ($endpoint === 'morphology_complete') {
260+
$result = $api->morphology($params);
261+
}
262+
if ($endpoint === 'sentiment') {
263+
$result = $api->sentiment($params);
264+
}
265+
if ($endpoint === 'name-translation') {
266+
$result = $api->nameTranslation($params);
267+
}
268+
if ($endpoint === 'relationships') {
269+
$result = $api->relationships($params);
270+
}
271+
$this->assertEquals($expected, $result["name"]);
272272
}
273-
274273
}

0 commit comments

Comments
 (0)