3 which curl
&> /dev
/null
6 echo "> curl not found"
10 which php
&> /dev
/null
13 echo "> php not found"
17 HOST
="${1:-localhost}"
21 # @see https://stackoverflow.com/a/5947802
24 # Black 0;30 Dark Gray 1;30
25 # Red 0;31 Light Red 1;31
26 # Green 0;32 Light Green 1;32
27 # Brown/Orange 0;33 Yellow 1;33
28 # Blue 0;34 Light Blue 1;34
29 # Purple 0;35 Light Purple 1;35
30 # Cyan 0;36 Light Cyan 1;36
31 # Light Gray 0;37 White 1;37
33 # .---------- constant part!
34 # vvvv vvvv-- the code from above
36 # NC='\033[0m' # No Color
37 # printf "I ${RED}love${NC} Stack Overflow\n"
39 # --------------------------------------
43 # - 'f': Failure message
44 # - 's': Success message
45 # - (empty): Normal message
47 # @param string $1 Message
48 # @param string $2 Flag
49 # --------------------------------------
58 if [[ 's' == "${flag}" ]]; then
59 echo -e "${gr}[✅ SUCCESS] ${message}${nc}"
60 elif [[ 'f' == "${flag}" ]]; then
61 echo -e "${rd}[❌ FAILURE] ${message}${nc}"
63 echo ">>>>>>>>>>>> ${message}"
67 # --------------------------------------
70 # Use following global variables to return/export values:
73 # - CURL_RESPONSE_HEADER
74 # - CURL_RESPONSE_DATA
76 # @param string $1 Path
77 # @param string $2 Method
78 # @param string $3 Data
79 # --------------------------------------
80 function callRestApi
{
82 CURL_RESPONSE_HEADER
=''
86 local method
="${2:-GET}"
89 if [[ ! -z "${data}" ]]; then
93 curl
"${HOST}:${PORT}${path}" \
94 --request "${method}" \
95 --header "Content-Type: application/json" \
96 --header "Accept: application/json" \
97 --dump-header curl_response_header.txt \
98 --output curl_response_data.txt \
103 if [ -f curl_response_header.txt
]; then
104 CURL_RESPONSE_HEADER
=$
(cat curl_response_header.txt
)
105 rm curl_response_header.txt
108 if [ -f curl_response_data.txt
]; then
109 CURL_RESPONSE_DATA
=$
(cat curl_response_data.txt
)
110 rm curl_response_data.txt
114 # --------------------------------------
115 # Get the value from a response header
117 # @param string $1 Headers list
118 # @param string $2 Header name
119 # --------------------------------------
120 function getValueFromHeader
{
123 if [[ "code" == "${key}" ]]; then
124 echo $
(echo "${list}" |
grep '^HTTP/' | cut
-d' ' -f2 |
xargs)
126 echo $
(echo "${list}" |
grep "^${key}:" | cut
-d':' -f2 |
xargs)
130 # --------------------------------------
131 # Get a value from a JSON string
133 # @param string $1 JSON
134 # @param string $2 KEY
135 # --------------------------------------
136 function getValueFromJson
{
139 local php
=$
(cat << PHP
140 \$list = json_decode('${json}', true);
141 echo \$list['${key}'];
144 echo $
(php
-r "${php}")
147 # --------------------------------------
148 # Count items in JSON list
150 # @param string $1 JSON
151 # --------------------------------------
152 function countItemsInJsonList
{
154 local php
=$
(cat << PHP
155 \$list = json_decode('${json}', true);
159 echo $
(php
-r "${php}")
162 # --------------------------------------
163 # Get name for a given id from a JSON string
165 # @param string $1 JSON
166 # @param string $2 ID
167 # --------------------------------------
168 function getNameFromId
{
171 local php
=$
(cat << PHP
172 \$list = json_decode('${json}', true);
174 foreach (\$list as \$candidato) {
175 if (${id} == \$candidato['id']) {
176 \$name = \$candidato['name'];
183 echo $
(php
-r "${php}")
186 # --------------------------------------
187 # Get amount of votes for a given id from a JSON string
189 # @param string $1 JSON
190 # @param string $2 ID
191 # --------------------------------------
192 function getVotesFromId
{
195 local php
=$
(cat << PHP
196 \$list = json_decode('${json}', true);
198 foreach (\$list as \$candidato) {
199 if (${id} == \$candidato['id']) {
200 \$votes = \$candidato['votes'];
207 echo $
(php
-r "${php}")