-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch-api-calls.sh
executable file
·198 lines (168 loc) · 4.81 KB
/
patch-api-calls.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/bin/bash
#
VERSION="0.1.3"
#
###############################################################################
# Configuration
SCRIPT_CMD="./xml-client.sh"
AUTH_REQUEST="login"
AUTH_VARS="USERNAME PASSWORD"
###############################################################################
function help_screen () {
for V in ${AUTH_VARS}; do
VL="${VL} ${V} \"value for ${V}\""
done
echo
echo "Usage: $(basename $0) [-hv] ${VL}"
echo " -h --help print this usage and exit"
echo " -v --version print version information and exit"
echo " -d Enable the debug output"
echo " --log /path/ Use the provided a directory for log files (one log file per request)"
echo " --csv FILE.csv Use the provided filename as list of API calls to perform"
echo ""
echo "VARIABLES can be set as environment variables or as arguments in the followiug format"
echo " NAME value The variables can be passed to the script as arguments "
}
function errorout () {
echo -e "$@" >&2
}
function echoout () {
echo -e "$@"
}
function formatout () {
FORMATOUT_FORMAT=$1
shift
printf "${FORMATOUT_FORMAT}" $@
}
function debugout () {
# Send debug output when debug mode is enabled
if [[ ${DEBUG} != 0 ]]; then
if [[ "${LOG_PATH}" == "" ]]; then
echo -e "$(date "+%F %T") - $@" >&2
else
echo -e "$(date "+%F %T") - $@" | tee -a ${LOG_PATH}/csv_request_${REQUEST_COUNT}.log >&2
fi
fi
}
OSTYPE=$(uname -s)
DEBUG=0
DEBUG_CMD=''
LOG_PATH=""
while [ $# -gt 0 ]; do
case $1 in
# General parameter
-h|--help)
help_screen
exit 0
;;
-v|--version)
echo
echo "`basename $0` version ${VERSION}"
echo
exit 0
;;
-d)
DEBUG=1
DEBUG_CMD="-d"
shift 1
;;
--log)
if [[ ! -z "$2" ]] && [[ -d "$2" ]]; then
DEBUG=1
DEBUG_CMD="-d"
LOG_PATH=$2
TEMP_FILE=$(mktemp)
else
echo "ERROR: Specified log path does not exist."
help_screen
exit 1
fi
shift 2
;;
--csv)
if [[ ! -z "$2" ]] && [[ -f "$2" ]]; then
CSV_FILE=$2
else
echo "ERROR: Specified csv file does not exist."
help_screen
exit 1
fi
shift 2
;;
# VARIABLES
*)
if [[ "${1}" == "$(echo ${1} | egrep '^[A-Z_]+$')" ]] && [[ ! -z "$2" ]]; then
export PAC_${1}="${2}"
shift 2
else
echo "ERROR: Unknown option '$1'"
help_screen
exit 1
break
fi
;;
esac
done
for V in ${AUTH_VARS}; do
VN="PAC_$V"
if [[ -z "${!VN}" ]]; then
errorout "Missing variable $V"
exit 1
fi
done
if [[ -z "${CSV_FILE}" ]]; then
errorout "Missing --csv option."
exit 1
fi
###############################################################################
#
###############################################################################
REQUEST_COUNT=0
# Authenticate
echoout "# Authenticate ..."
for V in ${AUTH_VARS}; do
VN="PAC_$V"
VL="${VL} ${V} '${!VN}'"
done
debugout "${SCRIPT_CMD} ${DEBUG_CMD} -r ${AUTH_REQUEST} ${VL}"
if [[ "${LOG_PATH}" == "" ]]; then
AUTH_TOKEN=$(echo ${SCRIPT_CMD} ${DEBUG_CMD} -r ${AUTH_REQUEST} ${VL} | bash)
else
AUTH_TOKEN=$(echo ${SCRIPT_CMD} ${DEBUG_CMD} -r ${AUTH_REQUEST} ${VL} | bash 2>${TEMP_FILE})
debugout "$(cat ${TEMP_FILE})"
fi
debugout "Authentication: ${AUTH_TOKEN}"
((REQUEST_COUNT++))
CMD_VARS=$(sed -e 's/ *; */;/g' -e 's/ *$//' ${CSV_FILE} |awk -F\; '
/^#/ {next}
/^\s*$/ {next}
BEGIN {
getline
for (i=1;i<=NF;i++)
CNAME[i]=$i
}
{
for (i=1;i<=NF;i++)
{
VAL[CNAME[i]]=$i
if ($i == "")
delete VAL[CNAME[i]]
}
printf("%s ", VAL["API_REQUEST"])
delete VAL["API_REQUEST"]
for (FIELD in VAL)
printf("%s \"%s\" ",FIELD, VAL[FIELD])
printf("\n")
}')
echoout "# Execute API calls ..."
echo "${CMD_VARS}" | while read VARS; do
echoout "${REQUEST_COUNT} - Executing $(echo ${VARS} | awk '{print $1}')"
debugout "${REQUEST_COUNT} - ${SCRIPT_CMD} ${DEBUG_CMD} -r ${VARS} ${AUTH_TOKEN}"
if [[ "${LOG_PATH}" == "" ]]; then
debugout "$(echo ${SCRIPT_CMD} ${DEBUG_CMD} -r ${VARS} ${AUTH_TOKEN} | bash)"
else
debugout "$(echo ${SCRIPT_CMD} ${DEBUG_CMD} -r ${VARS} ${AUTH_TOKEN} | bash 2>&1)"
fi
((REQUEST_COUNT++))
done
exit