Skip to content

Commit e8de071

Browse files
author
Chris Park
committed
Bumped composer requirements to 1.0. Upgraded scripts to check for errors
1 parent 0d56fa3 commit e8de071

File tree

5 files changed

+169
-85
lines changed

5 files changed

+169
-85
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": "0.8.0",
3+
"version": "1.0.0",
44
"description": "Rosette API PHP client SDK",
55
"license": "Apache",
66
"keywords": [

docker/run_php.sh

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/bin/bash
22

3+
retcode=0
4+
ping_url="https://api.rosette.com/rest/v1"
5+
6+
#------------------ Functions ----------------------------------------------------
7+
38
#Gets called when the user doesn't provide any args
49
function HELP {
510
echo -e "\nusage: source_file.php --key API_KEY [--url ALT_URL]"
@@ -12,6 +17,58 @@ function HELP {
1217
exit 1
1318
}
1419

20+
#Checks if Rosette API key is valid
21+
function checkAPI {
22+
match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "forbidden")
23+
if [ ! -z $match ]; then
24+
echo -e "\nInvalid Rosette API Key"
25+
exit 1
26+
fi
27+
}
28+
29+
function cleanURL() {
30+
# strip the trailing slash off of the alt_url if necessary
31+
if [ ! -z "${ALT_URL}" ]; then
32+
case ${ALT_URL} in
33+
*/) ALT_URL=${ALT_URL::-1}
34+
echo "Slash detected"
35+
;;
36+
esac
37+
ping_url=${ALT_URL}
38+
fi
39+
}
40+
41+
function validateURL() {
42+
match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "Rosette API")
43+
if [ "${match}" = "" ]; then
44+
echo -e "\n${ping_url} server not responding\n"
45+
exit 1
46+
fi
47+
}
48+
49+
function runExample() {
50+
echo -e "\n---------- ${1} start -------------"
51+
result=""
52+
if [ -z ${ALT_URL} ]; then
53+
result="$(php ${1} --key ${API_KEY} 2>&1 )"
54+
else
55+
result="$(php ${1} --key ${API_KEY} --url=${ALT_URL} 2>&1 )"
56+
fi
57+
echo "${result}"
58+
echo -e "\n---------- ${1} end -------------"
59+
if [[ "${result}" == *"Exception"* ]]; then
60+
echo "Exception found"
61+
retcode=1
62+
elif [[ "$result" == *"processingFailure"* ]]; then
63+
retcode=1
64+
elif [[ "$result" == *"AttributeError"* ]]; then
65+
retcode=1
66+
elif [[ "$result" == *"ImportError"* ]]; then
67+
retcode=1
68+
fi
69+
}
70+
#------------------ Functions End ------------------------------------------------
71+
1572
#Gets API_KEY, FILENAME, ALT_URL, GIT_USERNAME and VERSION if present
1673
while getopts ":API_KEY:FILENAME:ALT_URL" arg; do
1774
case "${arg}" in
@@ -38,34 +95,9 @@ while getopts ":API_KEY:FILENAME:ALT_URL" arg; do
3895
esac
3996
done
4097

41-
ping_url="https://api.rosette.com/rest/v1"
42-
43-
# strip the trailing slash off of the alt_url if necessary
44-
if [ ! -z "${ALT_URL}" ]; then
45-
case ${ALT_URL} in
46-
*/) ALT_URL=${ALT_URL::-1}
47-
echo "Slash detected"
48-
;;
49-
esac
50-
ping_url=${ALT_URL}
51-
fi
52-
53-
#Checks for valid url
54-
match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "Rosette API")
55-
if [ "${match}" = "" ]; then
56-
echo -e "\n${ping_url} server not responding\n"
57-
exit 1
58-
fi
59-
98+
cleanURL
6099

61-
#Checks if Rosette API key is valid
62-
function checkAPI {
63-
match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "forbidden")
64-
if [ ! -z $match ]; then
65-
echo -e "\nInvalid Rosette API Key"
66-
exit 1
67-
fi
68-
}
100+
validateURL
69101

70102
#Copy the mounted content in /source to current WORKDIR
71103
cp -r -n /source/* .
@@ -76,15 +108,11 @@ if [ ! -z ${API_KEY} ]; then
76108
checkAPI
77109
cd /php-dev/examples
78110
if [ ! -z ${FILENAME} ]; then
79-
if [ ! -z ${ALT_URL} ]; then
80-
php ${FILENAME} --key ${API_KEY} --url=${ALT_URL}
81-
else
82-
php ${FILENAME} --key ${API_KEY}
83-
fi
84-
elif [ ! -z ${ALT_URL} ]; then
85-
find -maxdepth 1 -name '*.php' -print -exec php {} --key ${API_KEY} --url=${ALT_URL} \;
111+
runExample ${FILENAME}
86112
else
87-
find -maxdepth 1 -name '*.php' -print -exec php {} --key ${API_KEY} \;
113+
for file in *.php; do
114+
runExample ${file}
115+
done
88116
fi
89117
else
90118
HELP
@@ -95,7 +123,7 @@ fi
95123
cd /php-dev && ./vendor/bin/phpunit -v --bootstrap ./vendor/autoload.php ./tests/rosette/api/ApiTest.php
96124

97125
#Run php-cs-fixer
98-
./vendor/bin/php-cs-fixer fix . --dry-run --diff
126+
./vendor/bin/php-cs-fixer fix . --dry-run --diff --level=psr2
99127

100128
#Generate gh-pages and push them to git account (if git username is provided)
101129
if [ ! -z ${GIT_USERNAME} ] && [ ! -z ${VERSION} ]; then
@@ -114,3 +142,5 @@ if [ ! -z ${GIT_USERNAME} ] && [ ! -z ${VERSION} ]; then
114142
git commit -a -m "publish php apidocs ${VERSION}"
115143
git push
116144
fi
145+
146+
exit ${retcode}

examples/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"rosette/api": "0.8.*"
3+
"rosette/api": "1.0.*"
44
}
55
}

examples/docker/run_php.sh

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/bin/bash
22

3+
retcode=0
4+
ping_url="https://api.rosette.com/rest/v1"
5+
6+
#------------------ Functions ----------------------------------------------------
7+
38
#Gets called when the user doesn't provide any args
49
function HELP {
510
echo -e "\nusage: source_file.php --key API_KEY [--url ALT_URL]"
@@ -10,6 +15,59 @@ function HELP {
1015
exit 1
1116
}
1217

18+
#Checks if Rosette API key is valid
19+
function checkAPI {
20+
match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "forbidden")
21+
if [ ! -z $match ]; then
22+
echo -e "\nInvalid Rosette API Key"
23+
exit 1
24+
fi
25+
}
26+
27+
function cleanURL() {
28+
# strip the trailing slash off of the alt_url if necessary
29+
if [ ! -z "${ALT_URL}" ]; then
30+
case ${ALT_URL} in
31+
*/) ALT_URL=${ALT_URL::-1}
32+
echo "Slash detected"
33+
;;
34+
esac
35+
ping_url=${ALT_URL}
36+
fi
37+
}
38+
39+
function validateURL() {
40+
match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "Rosette API")
41+
if [ "${match}" = "" ]; then
42+
echo -e "\n${ping_url} server not responding\n"
43+
exit 1
44+
fi
45+
}
46+
47+
function runExample() {
48+
echo -e "\n---------- ${1} start -------------"
49+
result=""
50+
if [ -z ${ALT_URL} ]; then
51+
result="$(php ${1} --key ${API_KEY} 2>&1 )"
52+
else
53+
result="$(php ${1} --key ${API_KEY} --url=${ALT_URL} 2>&1 )"
54+
fi
55+
echo "${result}"
56+
echo -e "\n---------- ${1} end -------------"
57+
if [[ "${result}" == *"Exception"* ]]; then
58+
echo "Exception found"
59+
retcode=1
60+
elif [[ "$result" == *"processingFailure"* ]]; then
61+
retcode=1
62+
elif [[ "$result" == *"AttributeError"* ]]; then
63+
retcode=1
64+
elif [[ "$result" == *"ImportError"* ]]; then
65+
retcode=1
66+
fi
67+
}
68+
#------------------ Functions End ------------------------------------------------
69+
70+
1371
#Gets API_KEY, FILENAME and ALT_URL if present
1472
while getopts ":API_KEY:FILENAME:ALT_URL" arg; do
1573
case "${arg}" in
@@ -28,14 +86,9 @@ while getopts ":API_KEY:FILENAME:ALT_URL" arg; do
2886
esac
2987
done
3088

31-
#Checks if Rosette API key is valid
32-
function checkAPI {
33-
match=$(curl "https://api.rosette.com/rest/v1/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "forbidden")
34-
if [ ! -z $match ]; then
35-
echo -e "\nInvalid Rosette API Key"
36-
exit 1
37-
fi
38-
}
89+
cleanURL
90+
91+
validateURL
3992

4093
#Copy the examples from the mounted content in /source to current WORKDIR
4194
cp /source/examples/*.* .
@@ -44,16 +97,14 @@ cp /source/examples/*.* .
4497
if [ ! -z ${API_KEY} ]; then
4598
checkAPI
4699
if [ ! -z ${FILENAME} ]; then
47-
if [ ! -z ${ALT_URL} ]; then
48-
php ${FILENAME} --key ${API_KEY} --url=${ALT_URL}
49-
else
50-
php ${FILENAME} --key ${API_KEY}
51-
fi
52-
elif [ ! -z ${ALT_URL} ]; then
53-
find -maxdepth 1 -name '*.php' -print -exec php {} --key ${API_KEY} --url=${ALT_URL} \;
100+
runExample ${FILENAME}
54101
else
55-
find -maxdepth 1 -name '*.php' -print -exec php {} --key ${API_KEY} \;
102+
for file in *.php; do
103+
runExample ${file}
104+
done
56105
fi
57106
else
58107
HELP
59-
fi
108+
fi
109+
110+
exit ${retcode}

source/rosette/api/Api.php

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private function finishResult($resultObject, $action)
242242
$msg = $resultObject['message'];
243243
}
244244
$complaint_url = $this->subUrl === null ? 'Top level info' : $action . ' ' . $this->subUrl;
245-
if (array_key_exists('code', $resultObject)) {
245+
if (array_key_exists('code', $resultObject)) {
246246
$serverCode = $resultObject['code'];
247247
if ($msg === null) {
248248
$msg = $serverCode;
@@ -279,7 +279,7 @@ private function callEndpoint($parameters, $subUrl)
279279
$this->subUrl = $subUrl;
280280
$this->useMultiPart = $parameters->useMultiPart;
281281

282-
if($this->useMultiPart){
282+
if ($this->useMultiPart) {
283283
$content = $parameters->content;
284284
$filename = $parameters->fileName;
285285

@@ -315,7 +315,6 @@ private function callEndpoint($parameters, $subUrl)
315315

316316
$resultObject = $this->postHttp($url, $this->headers, $multi);
317317
return $this->finishResult($resultObject, 'callEndpoint');
318-
319318
} else {
320319
$url = $this->service_url . $this->subUrl;
321320
if ($this->debug) {
@@ -383,19 +382,23 @@ private function makeRequest($url, $headers, $data, $method)
383382
$message = null;
384383

385384
// check for multipart and set data accordingly
386-
if($this->useMultiPart === NULL){
385+
if ($this->useMultiPart === null) {
387386
$data = (array) $data;
388387

389-
if($data['content'] === ""){
388+
if ($data['content'] === "") {
390389
unset($data['content']);
391390
}
392391

393-
if($data['contentUri'] === ""){
392+
if ($data['contentUri'] === "") {
394393
unset($data['contentUri']);
395394
}
396395

397-
foreach($data as $v){
398-
$data = array_filter($data, function($v){ if($v !== NULL || $v !== ""){return $v;}});
396+
foreach ($data as $v) {
397+
$data = array_filter($data, function ($v) {
398+
if ($v !== null || $v !== "") {
399+
return $v;
400+
}
401+
});
399402
}
400403
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
401404
}
@@ -408,45 +411,45 @@ private function makeRequest($url, $headers, $data, $method)
408411
curl_setopt($ch, CURLOPT_URL, $url);
409412
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
410413

411-
if($method === 'POST'){
414+
if ($method === 'POST') {
412415
curl_setopt($ch, CURLOPT_POST, true);
413416
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
414-
} else if($method === 'GET'){
415-
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
417+
} elseif ($method === 'GET') {
418+
curl_setopt($ch, CURLOPT_HTTPGET, true);
416419
}
417420

418421
curl_setopt($ch, CURLOPT_HEADER, 1);
419422
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
420423
$response = curl_exec($ch);
421424
$resCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
422-
if($response === false){
425+
if ($response === false) {
423426
echo curl_errno($ch);
424427
echo curl_error($ch);
425428
}
426429
curl_close($ch);
427430
$response = explode(PHP_EOL, $response);
428431
$this->setResponseCode($resCode);
429432

430-
if (strlen($response[9]) > 3 && mb_strpos($response[9], "\x1f" . "\x8b" . "\x08", 0) === 0) {
431-
// a gzipped string starts with ID1(\x1f) ID2(\x8b) CM(\x08)
432-
// http://www.gzip.org/zlib/rfc-gzip.html#member-format
433-
$response = gzinflate(substr($response, 10, -8));
434-
}
435-
if ($this->getResponseCode() < 500) {
436-
return $response;
437-
}
438-
if ($response !== null) {
439-
try {
440-
if (array_key_exists('message', $json)) {
441-
$message = $json['message'];
442-
}
443-
if (array_key_exists('code', $json)) {
444-
$code = $json['code'];
445-
}
446-
} catch (\Exception $e) {
447-
// pass
433+
if (strlen($response[9]) > 3 && mb_strpos($response[9], "\x1f" . "\x8b" . "\x08", 0) === 0) {
434+
// a gzipped string starts with ID1(\x1f) ID2(\x8b) CM(\x08)
435+
// http://www.gzip.org/zlib/rfc-gzip.html#member-format
436+
$response = gzinflate(substr($response, 10, -8));
437+
}
438+
if ($this->getResponseCode() < 500) {
439+
return $response;
440+
}
441+
if ($response !== null) {
442+
try {
443+
if (array_key_exists('message', $json)) {
444+
$message = $json['message'];
448445
}
446+
if (array_key_exists('code', $json)) {
447+
$code = $json['code'];
448+
}
449+
} catch (\Exception $e) {
450+
// pass
449451
}
452+
}
450453

451454
if ($code === 'unknownError') {
452455
$message = sprintf('A retryable network operation has not succeeded after %d attempts', $this->numRetries);

0 commit comments

Comments
 (0)