This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_all_patron.sh
executable file
·168 lines (144 loc) · 4.46 KB
/
get_all_patron.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
#!/bin/bash -e
#
# jake deery 2021
# koha restapi debug scripts - get_all_patron.sh
#set -x # uncomment to debug
shopt -s nocasematch # don't match casing, its not necessary
SCRIPT_DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd)" # get current script dir portibly
CONFIG_FILE=${SCRIPT_DIR}/config/config.json # config location
TOKEN_FILE=${SCRIPT_DIR}/token_file
TOKEN_STRING='' # stores fetched token
REQUIRED_ARGS_COUNTER=0 # tracking for successful operation
MATCHPOINT=''
VALUE=''
PAGE_NO='1'
PAGE_COUNT='20' # these are set with arguments
#
#
# functions
function getConfig() { # fetch config values from file
# vars
local locConfigJson=$(cat ${CONFIG_FILE})
# check its a json file -- dumb check
if [[ ${CONFIG_FILE} != *.json ]]; then
echo '[E] That is not a json file!'
exit 1
fi
# check file exists
if [[ ! -f ${CONFIG_FILE} ]]; then
echo '[E] Config file does not exist at '${CONFIG_FILE}' . . . '
exit 1
fi
# get each item into the array
CONFIG_ARR+=( $(echo ${locConfigJson} | jq --raw-output '."staff-client-url"') )
if [[ ${CONFIG_ARR[0]} == null ]]; then # check our work
echo '[E] Config key staff-client-url is missing, even though its required!'
exit 1
fi
CONFIG_ARR+=( $(echo ${locConfigJson} | jq --raw-output '."client-id"') )
if [[ ${CONFIG_ARR[0]} == null ]]; then # check our work
echo '[E] Config key staff-client-url is missing, even though its required!'
exit 1
fi
CONFIG_ARR+=( $(echo ${locConfigJson} | jq --raw-output '."client-secret"') )
if [[ ${CONFIG_ARR[0]} == null ]]; then # check our work
echo '[E] Config key staff-client-url is missing, even though its required!'
exit 1
fi
# all is ok
return 0
}
function getToken() { # fetch token from api endpoint
# vars
local locRequestUrl=${CONFIG_ARR[0]}/api/v1/oauth/token
local locClientId=${CONFIG_ARR[1]}
local locClientSecret=${CONFIG_ARR[2]}
local locTokenFile=${TOKEN_FILE}
# test to see if the file exists or its stale
if ! test -f ${locTokenFile} || test "`find ${locTokenFile} -mmin +45`"; then
# fetch token from api
local locTokenJson=$(curl -s -X POST -F grant_type=client_credentials -F client_id=${locClientId} -F client_secret=${locClientSecret} ${locRequestUrl})
# parse & catch duff responses
TOKEN_STRING=$(echo ${locTokenJson} | jq --raw-output '.access_token')
if [[ ${TOKEN_STRING} == 'null' ]] || [[ -z ${TOKEN_STRING} ]]; then
echo '[E] No access_token was provided! Check client-id and client-secret in config.json . . . '
exit 1
else
# not-duff response goes in the token_file for reuse later on
echo ${TOKEN_STRING} > ${locTokenFile}
chmod 0600 ${locTokenFile}
fi
else
# the existing token isn't stale -- reuse it
TOKEN_STRING=$(cat ${locTokenFile})
fi
# token should always be 88 chars
if [[ ${#TOKEN_STRING} != 88 ]]; then
echo '[E] Token is malformed or invalid! Please rerun this script'
rm ${locTokenFile}
exit 1
fi
# all is ok
return 0
}
function getAllPatron() {
# vars
local locTokenString=${TOKEN_STRING}
local locPageNo=${PAGE_NO}
local locPageCount=${PAGE_COUNT}
local locRequestUrl=${CONFIG_ARR[0]}'/api/v1/patrons?_page='${locPageNo}'&_per_page='${locPageCount}
# get it
echo $(curl -s -X GET -H 'Authorization: Bearer '${locTokenString} ${locRequestUrl})
# all is ok
return 0
}
#
#
# process
#
# first, handle all arguments
for (( i=0; i<$#; i++)); do
# vars
j=$((i+1))
# --config
if [[ ${!i} == '--config' ]]; then
if [[ ! -f ${!j} ]]; then
echo '[E] '${!j}' is not a valid file path!'
exit 1
else
CONFIG_FILE=${!j}
fi
fi
# --page
if [[ ${!i} == '--page' ]]; then
if [[ ${!j} =~ '^-?[0-9]+$' ]]; then
echo '[E] '${!j}' is not an integer!'
exit 1
else
PAGE_NO=${!j}
fi
fi
# --per-page
if [[ ${!i} == '--per-page' ]]; then
if [[ ${!j} =~ '^-?[0-9]+$' ]]; then
echo '[E] '${!j}' is not an integer!'
exit 1
else
PAGE_COUNT=${!j}
fi
fi
done
#
# begin main logic
if [[ ${REQUIRED_ARGS_COUNTER} != 0 ]]; then # if the wrong number of args are passed
echo '[E] Usage: '${0}
echo '[E] Optional flags:'
echo '[E] --config <file> The json file used to configure this script. Will default to <script-dir>/config/config.json if unspecified.'
echo '[E] --page <int> The page to lookup. Will default to '${PAGE_NO}' if unspecified.'
echo '[E] --per-page <int> The number of results per page. Will default to '${PAGE_COUNT}' if unspecified.'
exit 1
else
getConfig # grab our config
getToken # grab a token
getAllPatron # get all patrons
fi