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
49function 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
3984done
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
6891cp -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
88108else
89109 HELP
90110fi
111+
112+ exit ${retcode}
0 commit comments