Skip to content

Commit 40a08e9

Browse files
author
Chris Park
committed
Created run script to look for errors and allow an alternate URL
1 parent 20c925d commit 40a08e9

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

run-examples.sh

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/bin/bash
2+
3+
ping_url="https://api.rosette.com/rest/v1"
4+
retcode=0
5+
errors=( "Exception" "processingFailure" "badRequest" )
6+
7+
#------------ Start Functions --------------------------
8+
9+
#Gets called when the user doesn't provide any args
10+
function usage {
11+
echo -e "\n${0} -a API_KEY -f [FILENAME] -u [ALT_URL]"
12+
echo " API_KEY - Rosette API key (required)"
13+
echo " FILENAME - Shell script file (optional)"
14+
echo " ALT_URL - Alternate URL (optional)"
15+
exit 1
16+
}
17+
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+
# strip the trailing slash off of the alt_url if necessary
28+
function cleanURL() {
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+
#Checks for valid url
40+
function validateURL() {
41+
match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "Rosette API")
42+
if [ "${match}" = "" ]; then
43+
echo -e "\n${ping_url} server not responding\n"
44+
exit 1
45+
fi
46+
}
47+
48+
function runExample() {
49+
echo -e "\n---------- ${1} start -------------"
50+
result=""
51+
script="$(sed s/your_api_key/${API_KEY}/ < ./${1})"
52+
if [ ! -z ${ALT_URL} ]; then
53+
echo "ALT_URL: ${ping_url}"
54+
script=$(echo "${script}" | sed "s#https://api.rosette.com/rest/v1#${ping_url}#")
55+
fi
56+
echo $script
57+
result="$(echo ${script} | bash 2>&1)"
58+
echo "${result}"
59+
echo -e "\n---------- ${1} end -------------"
60+
for err in "${errors[@]}"; do
61+
if [[ ${result} == *"${err}"* ]]; then
62+
retcode=1
63+
fi
64+
done
65+
}
66+
67+
#------------ End Functions ----------------------------
68+
69+
#Gets API_KEY, FILENAME
70+
while getopts ":a:f:u:" arg; do
71+
case "${arg}" in
72+
a)
73+
API_KEY=${OPTARG}
74+
;;
75+
f)
76+
FILENAME=${OPTARG}
77+
;;
78+
u)
79+
ALT_URL=${OPTARG}
80+
echo "Using alternate URL: ${ALT_URL}"
81+
;;
82+
:)
83+
echo "Option -${OPTARG} requires an argument"
84+
usage
85+
;;
86+
esac
87+
done
88+
89+
if [ -z ${API_KEY} ]; then
90+
echo "-a API_KEY required"
91+
usage
92+
fi
93+
94+
cleanURL
95+
96+
validateURL
97+
98+
#Run the examples
99+
if [ ! -z ${API_KEY} ]; then
100+
checkAPI
101+
pushd examples
102+
if [ ! -z ${FILENAME} ]; then
103+
runExample ${FILENAME}
104+
else
105+
for file in *.curl; do
106+
runExample ${file}
107+
done
108+
fi
109+
else
110+
HELP
111+
fi
112+
113+
exit ${retcode}

0 commit comments

Comments
 (0)