Skip to content

Commit f465cc2

Browse files
committed
updated unit tests to match curl requests and removed fil_get_contents for http
1 parent c73fa84 commit f465cc2

File tree

7 files changed

+169
-134
lines changed

7 files changed

+169
-134
lines changed

docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ RUN chmod 755 /php-dev/run_php.sh
2727
VOLUME ["/source"]
2828

2929
# allow interactive bash inside docker container
30-
CMD ./run_php.sh $API_KEY $FILENAME $ALT_URL $GIT_USERNAME $VERSION; /bin/bash
30+
CMD ./run_php.sh $API_KEY $FILENAME $ALT_URL $GIT_USERNAME $VERSION

docker/run_php.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ else
9090
HELP
9191
fi
9292

93-
: <<'COMMENT'
93+
9494
#Run unit tests
9595
cd /php-dev && ./vendor/bin/phpunit -v --bootstrap ./vendor/autoload.php ./tests/rosette/api/ApiTest.php
9696

@@ -114,4 +114,3 @@ if [ ! -z ${GIT_USERNAME} ] && [ ! -z ${VERSION} ]; then
114114
git commit -a -m "publish php apidocs ${VERSION}"
115115
git push
116116
fi
117-
COMMENT

examples/base64_input.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

examples/name-translation.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
$params->set('targetLanguage', 'eng');
2121
$params->set ('targetScript', 'Latn');
2222
$params->set ('targetScheme', 'IC');
23+
$params->set('sourceLanguageOfOrigin', 'ara');
24+
$params->set('sourceLanguageOfUse', 'ara');
25+
2326

2427
try {
2528
$result = $api->nameTranslation($params);

source/rosette/api/Api.php

Lines changed: 83 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ public function __construct($user_key, $service_url = 'https://api.rosette.com/
154154
$this->subUrl = null;
155155
$this->timeout = 300;
156156

157-
$this->headers = array('X-RosetteAPI-Key' => $user_key,
158-
'Content-Type' => 'application/json',
159-
'Accept' => 'application/json',
160-
'Accept-Encoding' => 'gzip',
161-
'User-Agent' => 'RosetteAPIPHP/' . self::$binding_version, );
157+
$this->headers = array("X-RosetteAPI-Key: $user_key",
158+
"Content-Type: application/json",
159+
"Accept: application/json",
160+
"Accept-Encoding: gzip",
161+
"User-Agent: RosetteAPIPHP/" . self::$binding_version, );
162162
}
163163

164164
/**
@@ -275,36 +275,45 @@ private function finishResult($resultObject, $action)
275275
*/
276276
private function callEndpoint($parameters, $subUrl)
277277
{
278-
//$this->checkVersion($this->service_url);
278+
$this->checkVersion($this->service_url);
279279
$this->subUrl = $subUrl;
280280
$this->useMultiPart = $parameters->useMultiPart;
281+
281282
if($this->useMultiPart){
283+
$content = $parameters->content;
284+
$filename = $parameters->fileName;
285+
286+
$parameters = (array) $parameters;
287+
json_encode($parameters);
288+
json_encode($content, JSON_FORCE_OBJECT);
289+
290+
// create multipart
282291
$clrf = "\r\n";
283292
$multi = '';
284293
$boundary = md5(time());
285294
$multi .= '--' . $boundary . $clrf;
286-
$multi .= 'Content-Type: "application/json"' . "\r\n";
287-
$multi .= 'Content-Disposition: form-data; name="request"' . "\r\n" . "\r\n";
288-
//$multi .= $parameters . $clrf .$clrf;
295+
$multi .= 'Content-Type: application/json' . "\r\n";
296+
$multi .= 'Content-Disposition: mixed; name="request"' . "\r\n" . "\r\n";
297+
$multi .= "{\"language\": \"eng\"}" . "\r\n";
298+
$multi .= $parametersTemp . $clrf .$clrf;
289299
$multi .= '--' . $boundary . "\r\n";
290300
$multi .= 'Content-Type: text/plain' . "\r\n";
291-
$multi .= 'Content-Disposition: "multipart/form-data"; name="content"; filename="' . $parameters->fileName . '"' . "\r\n" . "\r\n";
292-
$multi .= $parameters->content . "\r\n" . "\r\n";
301+
$multi .= 'Content-Disposition: mixed; name="content"; filename="' . $filename . '"' . "\r\n" . "\r\n";
302+
$multi .= $content . "\r\n";
293303
$multi .= '--' . $boundary . '--';
294304

295-
$this->headers = array('X-RosetteAPI-Key' => $this->user_key,
296-
'Content-Type' => 'multipart/form-data',
297-
'Accept' => '*/*',
298-
'Accept-Encoding' => 'gzip',
299-
'User-Agent' => 'RosetteAPIPHP/' . self::$binding_version, );
305+
$this->headers = array("X-RosetteAPI-Key: $this->user_key",
306+
"Content-Type: multipart/mixed",
307+
"Accept: */*",
308+
"Accept-Encoding: gzip",
309+
"User-Agent: RosetteAPIPHP/" . self::$binding_version, );
300310

301311
$url = $this->service_url . $this->subUrl;
302312
if ($this->debug) {
303313
$url .= '?debug=true';
304314
}
305-
$parameters->content = $multi;
306315

307-
$resultObject = $this->postHttp($url, $this->headers, $parameters);
316+
$resultObject = $this->postHttp($url, $this->headers, $multi);
308317
return $this->finishResult($resultObject, 'callEndpoint');
309318

310319
} else {
@@ -334,12 +343,10 @@ public function checkVersion($url, $versionToCheck = null)
334343
$versionToCheck = self::$binding_version;
335344
}
336345
$resultObject = $this->postHttp($url . "info?clientVersion=$versionToCheck", $this->headers, null);
337-
var_dump(json_encode($resultObject[1]));
338-
@$tempJSON = json_encode($resultObject[1]);
339-
$finalJSON = json_decode($tempJSON, true);
340-
var_dump($finalJSON);
346+
$resultObject = array_pop((array_slice($resultObject, -1)));
347+
$resultObject = (array) json_decode($resultObject);
341348

342-
if ($finalJSON['versionChecked'] === true) {
349+
if ($resultObject['versionChecked'] === true) {
343350
$this->version_checked = true;
344351
} else {
345352
throw new RosetteException(
@@ -348,7 +355,7 @@ public function checkVersion($url, $versionToCheck = null)
348355
);
349356
}
350357
}
351-
$this->versionChecked = true;
358+
352359
return $this->version_checked;
353360
}
354361

@@ -358,7 +365,7 @@ public function checkVersion($url, $versionToCheck = null)
358365
* Encapsulates the GET/POST
359366
*
360367
* @param $url
361-
* @param $context
368+
* @param $data
362369
*
363370
* @return string
364371
*
@@ -367,62 +374,69 @@ public function checkVersion($url, $versionToCheck = null)
367374
* @internal param $op : operation
368375
* @internal param $url : target URL
369376
* @internal param $headers : header data
370-
* @internal param data $optional : submission data
377+
* @internal param $data : submission data
378+
* @internal param $method : http method
371379
*/
372-
private function makeRequest($url, $headers, $context, $method)
380+
private function makeRequest($url, $headers, $data, $method)
373381
{
374382
$response = null;
375383
$message = null;
376-
$context = array('content' => $context->content);
377-
//$context = json_encode($context, JSON_FORCE_OBJECT);
378384

379-
$code = 'unknownError';
380-
$http_response_header = null;
381-
//$response = file_get_contents($url, false, $context);
382-
//$url = 'http://10.1.7.116:8181/rest/v1/language';
383-
$ch = curl_init();
384-
curl_setopt($ch, CURLOPT_URL, $url);
385-
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
386-
387-
if($method === 'POST'){
388-
curl_setopt($ch, CURLOPT_POST, true);
389-
curl_setopt($ch, CURLOPT_POSTFIELDS, $context);
390-
} else if($method === 'GET'){
391-
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
385+
// check for multipart and set data accordingly
386+
if($this->useMultiPart === NULL){
387+
$data = (array) $data;
388+
389+
if($data['content'] === ""){
390+
unset($data['content']);
391+
}
392+
393+
if($data['contentUri'] === ""){
394+
unset($data['contentUri']);
392395
}
393396

394-
curl_setopt($ch, CURLOPT_HEADER, 1);
395-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
396-
$response = curl_exec($ch);
397-
$resCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
398-
if($response === false){
399-
echo curl_errno($ch);
400-
echo curl_error($ch);
397+
foreach($data as $v){
398+
$data = array_filter($data, function($v){ if($v !== NULL || $v !== ""){return $v;}});
401399
}
402-
curl_close($ch);
403-
$response = explode(PHP_EOL, $response);
404-
//var_dump($response);
405-
$this->setResponseCode($resCode);
406-
return $response;
407-
408-
/*$response_status = $this->getResponseStatusCode($http_response_header);
409-
$this->setResponseCode($response_status);
410-
if (strlen($response) > 3 && mb_strpos($response, "\x1f" . "\x8b" . "\x08", 0) === 0) {
400+
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
401+
}
402+
403+
$code = 'unknownError';
404+
$http_response_header = null;
405+
406+
// create cURL request
407+
$ch = curl_init();
408+
curl_setopt($ch, CURLOPT_URL, $url);
409+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
410+
411+
if($method === 'POST'){
412+
curl_setopt($ch, CURLOPT_POST, true);
413+
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
414+
} else if($method === 'GET'){
415+
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
416+
}
417+
418+
curl_setopt($ch, CURLOPT_HEADER, 1);
419+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
420+
$response = curl_exec($ch);
421+
$resCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
422+
if($response === false){
423+
echo curl_errno($ch);
424+
echo curl_error($ch);
425+
}
426+
curl_close($ch);
427+
$response = explode(PHP_EOL, $response);
428+
$this->setResponseCode($resCode);
429+
430+
if (strlen($response[9]) > 3 && mb_strpos($response[9], "\x1f" . "\x8b" . "\x08", 0) === 0) {
411431
// a gzipped string starts with ID1(\x1f) ID2(\x8b) CM(\x08)
412432
// http://www.gzip.org/zlib/rfc-gzip.html#member-format
413433
$response = gzinflate(substr($response, 10, -8));
414434
}
415435
if ($this->getResponseCode() < 500) {
416-
if($http_response_header != null){
417-
$response = array($http_response_header, json_decode($response, true));
418-
} else {
419-
$response = json_decode($response, true);
420-
}
421436
return $response;
422-
}*/
423-
/*if ($response !== null) {
437+
}
438+
if ($response !== null) {
424439
try {
425-
$json = json_decode($response, true);
426440
if (array_key_exists('message', $json)) {
427441
$message = $json['message'];
428442
}
@@ -432,12 +446,12 @@ private function makeRequest($url, $headers, $context, $method)
432446
} catch (\Exception $e) {
433447
// pass
434448
}
435-
}*/
449+
}
436450

437-
/*if ($code === 'unknownError') {
451+
if ($code === 'unknownError') {
438452
$message = sprintf('A retryable network operation has not succeeded after %d attempts', $this->numRetries);
439453
}
440-
throw new RosetteException($message . ' [' . $url . ']', $code);*/
454+
throw new RosetteException($message . ' [' . $url . ']', $code);
441455
}
442456

443457
/**

0 commit comments

Comments
 (0)