Skip to content

Commit c63571c

Browse files
author
Chris Park
committed
Revised output to associative array format with header element. Cleaned up several files to pass cs-fixer. Amended unit tests to work with the new output.
1 parent 7d0d090 commit c63571c

File tree

4 files changed

+64
-51
lines changed

4 files changed

+64
-51
lines changed

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
*

tests/rosette/api/ApiTest.php

Lines changed: 22 additions & 14 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'],
@@ -260,6 +268,6 @@ public function testEndpoints($filename, $endpoint)
260268
if ($endpoint === 'relationships') {
261269
$result = $api->relationships($params);
262270
}
263-
$this->assertEquals($expected, $result[0]);
271+
$this->assertEquals($expected, $result["name"]);
264272
}
265273
}

0 commit comments

Comments
 (0)