Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
lauren12292 committed Mar 31, 2016
2 parents 7f440c4 + 9b87190 commit 128580a
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ script: phpunit --bootstrap ./vendor/autoload.php ./tests/rosette/api/ApiTest.ph
notifications:
slack:
rooms:
- secure: 4FRaTAAiYyeUvgw2RhmblgbNiJO4wmd34OBgWcwURjP9oVmFfSwR9r1LNCdUGxrPOghexSY2DjXIuvIrfTfi/xYbhHb3Kw7PEAyB8IuBMlKtY4NSFou62S2VhYpxyg58T+C7P2zi0eDnDE06pwTCoGPaimxMZQY91yQ0yPYDPVXbwe5SjEgamzlwGBxlS/0A6w1iCPHg27/iO2hXtdW3oLS2I0F/Q8Q95RBkX9hpg6yqHlTV7jRbSqvQ9OFBqk/tXMHQvhoPDGgCgQDuykJuaAYx7g9d0YL0eEYYOh9B/TJ/kNOwdRFBu5kuQ2/nFS5Z0S3Y3UIhdYjUmm9gSMnwIbYnrW22EqDJLoT9Zi3Gv7Prg/8/fSkWsof7BJTMSuXUqO1AxDGKIxFv9uSF1daZoY+AC1ooU1xDu1nNvWVYPlkwEdDxxmHpFkGT3ESTZYccPovQl8Z5K0I1BBAVdJKDzm07lE6VHbxkKcvK6gG0TN3uLxnSlQtjkfJ+aVMq1kxeVsB9lEsKs9oezsKzzbftMm525aXPg+OAv+31CUFWxvT/p4ps8Q+AV6aZpoPHkpK8VryyNirUeZ/m4m4ebDHhD9vcN+JqE9gzshT+0U3g19SvLiUMQtbuZ2BUvrq2hh2LEGs03AFZaNg9AEUVA1PQRhV5NILyoS/lbiBYJPT39Sg=
# - secure: 4FRaTAAiYyeUvgw2RhmblgbNiJO4wmd34OBgWcwURjP9oVmFfSwR9r1LNCdUGxrPOghexSY2DjXIuvIrfTfi/xYbhHb3Kw7PEA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TjfoMcJ3ISjGNHRmUwcCH9avcaHqMLPLHht0z+8inVlDZeTcN+Wi8tk1YXwuQYUkTW46iFmoZNGdygRjTu43h9hCqdG7vcoKrR8oDbMT8X/sfs+z/UaBbuYBr9dT8rrdKJoa9115PaYpCs529DLrw1iGmzr5CkAXDElc1y2F352nwVS+dX7/OIQz1/dbT1ozWQ9Blx6ez5XN84=
# - 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=
on_success: always
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rosette/api",
"version": "1.0.2",
"version": "1.0.3",
"description": "Rosette API PHP client SDK",
"license": "Apache",
"keywords": [
Expand Down
4 changes: 2 additions & 2 deletions examples/name_translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
$params = new NameTranslationParameters();
$params->set('name', $name_translation_data);
$params->set('targetLanguage', 'eng');
$params->set ('targetScript', 'Latn');
$params->set ('targetScheme', 'IC');
$params->set('targetScript', 'Latn');
$params->set('targetScheme', 'IC');
$params->set('sourceLanguageOfOrigin', 'ara');
$params->set('sourceLanguageOfUse', 'ara');

Expand Down
1 change: 0 additions & 1 deletion examples/sentiment.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@
}

fclose($temp); // clean up the temp file

74 changes: 40 additions & 34 deletions source/rosette/api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ private function finishResult($resultObject, $action)
: failed to communicate with Api: ' . $msg,
is_numeric($serverCode) ? $serverCode : RosetteException::$BAD_REQUEST_FORMAT
);

}
}

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

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

/**
* function headersToArray
*
* Converts the http response header string to an associative array
*
* @param $headers
*
* @returns associative array of headers
*/
public function headersToArray($headers)
{
$head = array();
foreach ($headers as $k=>$v) {
$t = explode(':', $v, 2);
if (isset($t[1])) {
$head[ trim($t[0]) ] = trim($t[1]);
} else {
if (strlen(trim($v)) > 0) {
$head[] = $v;
}
if (preg_match("#HTTP/[0-9\.]+\s+([0-9]+)#", $v, $out)) {
$head['response_code'] = intval($out[1]);
}
}
}
return $head;
}

/**
* function makeRequest.
*
Expand Down Expand Up @@ -394,7 +419,7 @@ private function makeRequest($url, $headers, $data, $method)
}

foreach ($data as $v) {
$data = array_filter($data, function ($v) {
$data = array_filter($data, function ($v) {
if ($v !== null || $v !== "") {
return $v;
}
Expand Down Expand Up @@ -422,21 +447,23 @@ private function makeRequest($url, $headers, $data, $method)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$resCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->setResponseCode($resCode);
if ($response === false) {
echo curl_errno($ch);
echo curl_error($ch);
}
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = explode(PHP_EOL, substr($response, 0, $header_size));
$body = substr($response, $header_size);

curl_close($ch);
$response = explode(PHP_EOL, $response);
$this->setResponseCode($resCode);

if (array_key_exists(9, $response)) {
if (strlen($response[9]) > 3 && mb_strpos($response[9], "\x1f" . "\x8b" . "\x08", 0) === 0) {
// a gzipped string starts with ID1(\x1f) ID2(\x8b) CM(\x08)
// http://www.gzip.org/zlib/rfc-gzip.html#member-format
$response = gzinflate(substr($response, 10, -8));
}
if (strlen($body) > 3 && mb_strpos($body, "\x1f" . "\x8b" . "\x08", 0) === 0) {
// a gzipped string starts with ID1(\x1f) ID2(\x8b) CM(\x08)
// http://www.gzip.org/zlib/rfc-gzip.html#member-format
$body = gzinflate(substr($body, 10, -8));
}
$response = [ 'headers' => $this->headersToArray($header) ];
$response = array_merge($response, json_decode($body, true));
if ($this->getResponseCode() < 500) {
return $response;
}
Expand Down Expand Up @@ -482,27 +509,6 @@ public function getResponseStatusCode($header_str)
}
}

/**
* Creates the header string that is acceptable to file_get_contents.
*
* @param $headers
*
* @return string
*/
private function headersAsString($headers)
{
return implode(
"\r\n",
array_map(
function ($k, $v) {
return "$k: $v";
},
array_keys($headers),
array_values($headers)
)
);
}

/**
* Standard GET helper.
*
Expand Down
10 changes: 9 additions & 1 deletion source/rosette/api/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Class that represents a Name.
*/
class Name
class Name extends RosetteParamsSetBase
{
/**
* Textual form of the name.
Expand Down Expand Up @@ -69,4 +69,12 @@ public function __construct($text, $entityType = null, $language = null, $script
$this->language = $language;
$this->script = $script;
}

/**
* required validate function
*/
public function validate()
{
// nothing to validate
}
}
101 changes: 50 additions & 51 deletions tests/rosette/api/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,20 @@

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

return $mock_response;
}

function curl_getinfo($ch)
Expand Down Expand Up @@ -71,9 +83,6 @@ public static function setupBeforeClass()
*/
private function getMockedResponse($filename)
{
//$response = json_decode(\file_get_contents(self::$responseDir . $filename . '.json'), true);

//return $response;
return curl_exec($ch = null);
}

Expand All @@ -86,7 +95,6 @@ private function getMockedResponse($filename)
*/
private function getMockedResponseCode($filename)
{
//return intval(\file_get_contents(self::$responseDir . $filename . '.status'));
return curl_getinfo($ch = null);
}

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

/**
* @group posts
* @expectedException \rosette\api\RosetteException
*/
public function testCheckVersion()
{
$this->userKey = 'checkVersion';
$api = $this->setUpApi($this->userKey);
$api->checkVersion('http://rosette.basistech.com');
$result = $api->checkVersion('http://rosette.basistech.com');
$this->assertTrue($result);
}

/**
* @group gets
*/
public function testInfo()
{
$expected = $this->getMockedResponse('info');
$expected = "Rosette API";
$this->userKey = 'info';
$api = $this->setUpApi($this->userKey);
$result = $api->info();
$this->assertSame($expected, $result[0]);
$this->assertSame($expected, $result["name"]);
}

/**
* @group gets
*/
public function testPing()
{
$expected = $this->getMockedResponse('ping');
$expected = "Rosette API";
$this->userKey = 'ping';
$api = $this->setUpApi($this->userKey);
$result = $api->ping();
$this->assertSame($expected, $result[0]);
$this->assertSame($expected, $result["name"]);
}

/**
Expand Down Expand Up @@ -198,7 +206,7 @@ public function testEndpoints($filename, $endpoint)
$api->skipVersionCheck(); // need to set it so it doesn't call the mocked info()
$api->setDebug(true);
$input = $this->getRequestData($this->userKey);
$expected = $this->getMockedResponse($this->userKey);
$expected = "Rosette API";
if ($endpoint === 'name-similarity') {
$sourceName = new Name(
$input['name1']['text'],
Expand Down Expand Up @@ -232,43 +240,34 @@ public function testEndpoints($filename, $endpoint)
// If it does not throw an exception, check that it was not supposed to and if so check that it
// returns the correct thing.
// If it throws an exception, check that it was supposed to and if so pass otherwise fail test.
//try {
$result = '';
if ($endpoint === 'categories') {
$result = $api->categories($params);
}
if ($endpoint === 'entities') {
$result = $api->entities($params);
}
if ($endpoint === 'entities_linked') {
$result = $api->entities($params, true);
}
if ($endpoint === 'language') {
$result = $api->language($params);
}
if ($endpoint === 'name-similarity') {
$result = $api->nameSimilarity($params);
}
if ($endpoint === 'morphology_complete') {
$result = $api->morphology($params);
}
if ($endpoint === 'sentiment') {
$result = $api->sentiment($params);
}
if ($endpoint === 'name-translation') {
$result = $api->nameTranslation($params);
}
if ($endpoint === 'relationships') {
$result = $api->relationships($params);
}
// If there is a "code" key, it means an exception should be thrown
//if (!array_key_exists('code', $expected)) {
$this->assertEquals($expected, $result[0]);

//}
//} catch (RosetteException $exception) {
// $this->assertSame('unsupportedLanguage', $expected['code']);
//}
$result = '';
if ($endpoint === 'categories') {
$result = $api->categories($params);
}
if ($endpoint === 'entities') {
$result = $api->entities($params);
}
if ($endpoint === 'entities_linked') {
$result = $api->entities($params, true);
}
if ($endpoint === 'language') {
$result = $api->language($params);
}
if ($endpoint === 'name-similarity') {
$result = $api->nameSimilarity($params);
}
if ($endpoint === 'morphology_complete') {
$result = $api->morphology($params);
}
if ($endpoint === 'sentiment') {
$result = $api->sentiment($params);
}
if ($endpoint === 'name-translation') {
$result = $api->nameTranslation($params);
}
if ($endpoint === 'relationships') {
$result = $api->relationships($params);
}
$this->assertEquals($expected, $result["name"]);
}

}

0 comments on commit 128580a

Please sign in to comment.