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
49function HELP {
510 echo -e " \nusage: source_file.py --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} " -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=" $( python ${1} --key ${API_KEY} 2>&1 ) "
52+ else
53+ result=" $( python ${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+
69+ # ------------------ Functions End ------------------------------------------------
70+
1371# Gets API_KEY, FILENAME and ALT_URL if present
1472while getopts " :API_KEY:FILENAME:ALT_URL" arg; do
1573 case " ${arg} " in
@@ -27,33 +85,10 @@ while getopts ":API_KEY:FILENAME:ALT_URL" arg; do
2785 ;;
2886 esac
2987done
30- ping_url=" https://api.rosette.com/rest/v1"
31-
32- # strip the trailing slash off of the alt_url if necessary
33- if [ ! -z " ${ALT_URL} " ]; then
34- case ${ALT_URL} in
35- * /) ALT_URL=${ALT_URL::- 1}
36- echo " Slash detected"
37- ;;
38- esac
39- ping_url=${ALT_URL}
40- fi
4188
42- # Checks for valid url
43- match=$( curl " ${ping_url} /ping" -H " X-RosetteAPI-Key: ${API_KEY} " | grep -o " Rosette API" )
44- if [ " ${match} " = " " ]; then
45- echo -e " \n${ping_url} server not responding\n"
46- exit 1
47- fi
89+ cleanURL
4890
49- # Checks if Rosette API key is valid
50- function checkAPI {
51- match=$( curl " ${ping_url} /ping" -H " X-RosetteAPI-Key: ${API_KEY} " | grep -o " forbidden" )
52- if [ ! -z $match ]; then
53- echo -e " \nInvalid Rosette API Key"
54- exit 1
55- fi
56- }
91+ validateURL
5792
5893# Copy the examples from the mounted content in /source to current WORKDIR
5994cp /source/examples/* .* .
@@ -62,16 +97,14 @@ cp /source/examples/*.* .
6297if [ ! -z ${API_KEY} ]; then
6398 checkAPI
6499 if [ ! -z ${FILENAME} ]; then
65- if [ ! -z ${ALT_URL} ]; then
66- python ${FILENAME} --key ${API_KEY} --url ${ALT_URL}
67- else
68- python ${FILENAME} --key ${API_KEY}
69- fi
70- elif [ ! -z ${ALT_URL} ]; then
71- find -maxdepth 1 -name ' *.py' -print -exec python {} --key ${API_KEY} --url ${ALT_URL} \;
100+ runExample ${FILENAME}
72101 else
73- find -maxdepth 1 -name ' *.py' -print -exec python {} --key ${API_KEY} \;
74- fi
102+ for file in * .py; do
103+ runExample ${file}
104+ done
105+ fi
75106else
76107 HELP
77108fi
109+
110+ exit ${retcode}
0 commit comments