Skip to content

Commit d1da5e4

Browse files
author
Chris Park
committed
docker script cleanup and added checks for errors
1 parent 4db35f3 commit d1da5e4

File tree

3 files changed

+125
-88
lines changed

3 files changed

+125
-88
lines changed

docker/runAll.sh

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

3+
ping_url="https://api.rosette.com/rest/v1/"
4+
retcode=0
5+
6+
#------------------- Functions -------------------------------------
7+
38
#Gets called when the user doesn't provide any args
49
function HELP {
510
echo -e "\nusage: source_file.py --key API_KEY [--url ALT_URL]"
@@ -12,6 +17,55 @@ 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+
# add the trailing slash of the alt_url if necessary
30+
function cleanURL() {
31+
if [ ! -z "${ALT_URL}" ]; then
32+
if [[ ! ${ALT_URL} == */ ]]; then
33+
ALT_URL="${ALT_URL}/"
34+
echo "No Slash detected"
35+
fi
36+
ping_url=${ALT_URL}
37+
fi
38+
}
39+
40+
#Checks for valid url
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="$(node ${1} --key ${API_KEY} 2>&1 )"
54+
else
55+
result="$(node ${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+
retcode=1
61+
elif [[ $result == *"processingFailure"* ]]; then
62+
retcode=1
63+
fi
64+
}
65+
66+
67+
#------------------- Functions End ----------------------------------
68+
1569
#Gets API_KEY, FILENAME and ALT_URL if present
1670
while getopts ":API_KEY:FILENAME:ALT_URL:GIT_USERNAME:VERSION" arg; do
1771
case "${arg}" in
@@ -37,32 +91,10 @@ while getopts ":API_KEY:FILENAME:ALT_URL:GIT_USERNAME:VERSION" arg; do
3791
;;
3892
esac
3993
done
40-
ping_url="https://api.rosette.com/rest/v1/"
41-
42-
# add the trailing slash of the alt_url if necessary
43-
if [ ! -z "${ALT_URL}" ]; then
44-
if [[ ! ${ALT_URL} == */ ]]; then
45-
ALT_URL="${ALT_URL}/"
46-
echo "No Slash detected"
47-
fi
48-
ping_url=${ALT_URL}
49-
fi
5094

51-
#Checks for valid url
52-
match=$(curl "${ping_url}ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "Rosette API")
53-
if [ "${match}" = "" ]; then
54-
echo -e "\n${ping_url} server not responding\n"
55-
exit 1
56-
fi
95+
cleanURL
5796

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

6799
#Copy the mounted content in /source to current WORKDIR
68100
cp -r -n /source/. .
@@ -92,24 +124,7 @@ istanbul cover _mocha unittests.js
92124
#run eslint
93125
eslint ../lib/**
94126

95-
retcode=0
96127

97-
function runExample() {
98-
echo -e "\n---------- ${1} start -------------"
99-
result=""
100-
if [ -z ${ALT_URL} ]; then
101-
result="$(node ${1} --key ${API_KEY})"
102-
else
103-
result="$(node ${1} --key ${API_KEY} --url ${ALT_URL})"
104-
fi
105-
echo ${result}
106-
echo -e "\n---------- ${1} end -------------"
107-
if [[ $result == *"Exception"* ]]; then
108-
retcode=1
109-
elif [[ $result == *"processingFailure"* ]]; then
110-
retcode=1
111-
fi
112-
}
113128

114129
#Run the examples
115130
if [ ! -z ${API_KEY} ]; then

examples/runAll.sh

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

3+
ping_url="https://api.rosette.com/rest/v1/"
4+
retcode=0
5+
6+
#------------------- Functions -------------------------------------
7+
38
#Gets called when the user doesn't provide any args
49
function HELP {
510
echo -e "\nusage: source_file.py --key API_KEY [--url ALT_URL]"
611
echo " API_KEY - Rosette API key (required)"
712
echo " FILENAME - Python source file (optional)"
813
echo " ALT_URL - Alternate service URL (optional)"
9-
echo " GIT_USERNAME - Git username where you would like to push regenerated gh-pages (optional)"
10-
echo " VERSION - Build version (optional)"
11-
echo "Compiles and runs the source file(s) using the local development source."
14+
echo "Runs the examples using the NPM package."
1215
exit 1
1316
}
1417

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+
# add the trailing slash of the alt_url if necessary
28+
function cleanURL() {
29+
if [ ! -z "${ALT_URL}" ]; then
30+
if [[ ! ${ALT_URL} == */ ]]; then
31+
ALT_URL="${ALT_URL}/"
32+
echo "No Slash detected"
33+
fi
34+
ping_url=${ALT_URL}
35+
fi
36+
}
37+
38+
#Checks for valid url
39+
function validateURL() {
40+
match=$(curl "${ping_url}ping" -H "X-RosetteAPI-Key: ${API_KEY}" -H "user_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="$(node ${1} --key ${API_KEY} 2>&1 )"
52+
else
53+
result="$(node ${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+
retcode=1
59+
elif [[ $result == *"processingFailure"* ]]; then
60+
retcode=1
61+
fi
62+
}
63+
64+
65+
#------------------- Functions End ----------------------------------
66+
67+
1568
#Gets API_KEY, FILENAME and ALT_URL if present
16-
while getopts ":API_KEY:FILENAME:ALT_URL:GIT_USERNAME:VERSION" arg; do
69+
while getopts ":API_KEY:FILENAME:ALT_URL" arg; do
1770
case "${arg}" in
1871
API_KEY)
1972
API_KEY=${OPTARG}
@@ -27,42 +80,12 @@ while getopts ":API_KEY:FILENAME:ALT_URL:GIT_USERNAME:VERSION" arg; do
2780
FILENAME=${OPTARG}
2881
usage
2982
;;
30-
GIT_USERNAME)
31-
GIT_USERNAME=${OPTARG}
32-
usage
33-
;;
34-
VERSION)
35-
VERSION={OPTARG}
36-
usage
37-
;;
3883
esac
3984
done
40-
ping_url="https://api.rosette.com/rest/v1/"
4185

42-
# add the trailing slash of the alt_url if necessary
43-
if [ ! -z "${ALT_URL}" ]; then
44-
if [[ ! ${ALT_URL} == */ ]]; then
45-
ALT_URL="${ALT_URL}/"
46-
echo "No Slash detected"
47-
fi
48-
ping_url=${ALT_URL}
49-
fi
86+
cleanURL
5087

51-
#Checks for valid url
52-
match=$(curl "${ping_url}ping" -H "user_key: ${API_KEY}" | grep -o "Rosette API")
53-
if [ "${match}" = "" ]; then
54-
echo -e "\n${ping_url} server not responding\n"
55-
exit 1
56-
fi
57-
58-
#Checks if Rosette API key is valid
59-
function checkAPI {
60-
match=$(curl "${ping_url}ping" -H "user_key: ${API_KEY}" | grep -o "forbidden")
61-
if [ ! -z $match ]; then
62-
echo -e "\nInvalid Rosette API Key"
63-
exit 1
64-
fi
65-
}
88+
validateURL
6689

6790
#Copy the mounted content in /source to current WORKDIR
6891
cp -r -n /source/. .
@@ -74,17 +97,16 @@ if [ ! -z ${API_KEY} ]; then
7497
cd ../examples
7598
npm install argparse
7699
npm install temporary
100+
npm install multipart-stream
77101
if [ ! -z ${FILENAME} ]; then
78-
if [ ! -z ${ALT_URL} ]; then
79-
node ${FILENAME} --key ${API_KEY} --url ${ALT_URL}
80-
else
81-
node ${FILENAME} --key ${API_KEY}
82-
fi
83-
elif [ ! -z ${ALT_URL} ]; then
84-
find . -name '*.js' -exec node {} --key ${API_KEY} --url ${ALT_URL} \;
102+
runExample ${FILENAME}
85103
else
86-
find . -name '*.js' -exec node {} --key ${API_KEY} \;
104+
for file in *.js; do
105+
runExample ${file}
106+
done
87107
fi
88108
else
89109
HELP
90110
fi
111+
112+
exit ${retcode}

lib/parameters.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ parameters.prototype.loadFile = function(filePath, loadedParameters, userKey, se
122122
mp.addPart({
123123
headers: {
124124
"Content-Type": "application/json",
125-
"Content-Disposition": "form-data; name=\"request\""
125+
"Content-Disposition": "mixed; name=\"request\""
126126
},
127127
body: JSON.stringify(loadedParameters.loadParams())
128128
})
129129

130130
mp.addPart({
131131
headers: {
132132
"Content-Type": "text/plain",
133-
"Content-Disposition": "form-data; name=\"content\"; filename=\"" + path.basename(filePath) + "\""
133+
"Content-Disposition": "mixed; name=\"content\"; filename=\"" + path.basename(filePath) + "\""
134134
},
135135
body: str.toString()
136136
})
@@ -149,7 +149,7 @@ parameters.prototype.loadFile = function(filePath, loadedParameters, userKey, se
149149
var headers = {
150150
"accept": 'application/json',
151151
"accept-encoding": "gzip",
152-
"content-type": 'multipart/form-data',
152+
"content-type": 'multipart/mixed',
153153
"user-agent": "rosetteapinode/" + BINDING_VERSION
154154
}
155155
headers["X-RosetteAPI-Key"] = userKey;

0 commit comments

Comments
 (0)