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
/
update_patron.sh
executable file
·177 lines (152 loc) · 4.69 KB
/
update_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
169
170
171
172
173
174
175
176
177
#!/bin/bash -e
#
# jake deery 2021
# koha restapi debug scripts - update_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
PATRON_ID=0
PAYLOAD='' # 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}
fi
else
# the existing token isn't stale -- reuse it
TOKEN_STRING=$(cat ${locTokenFile})
chmod 0600 ${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 updatePatron() {
# vars
local locTokenString=${TOKEN_STRING}
local locPatronId=${PATRON_ID}
local locFilePath=${PAYLOAD}
local locRequestUrl=${CONFIG_ARR[0]}/api/v1/patrons/${PATRON_ID}
# check its a json file -- dumb check
if [[ ${locFilePath} != *.json ]]; then
echo '[E] That is not a json file!'
exit 1
fi
# put it
echo $(curl -s -X PUT -H 'Authorization: Bearer '${locTokenString} -H '' -d @${locFilePath} ${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
# --patron-id
if [[ ${!i} == '--patron-id' ]]; then
if [[ ${!j} =~ '^-?[0-9]+$' ]]; then
echo '[E] '${!j}' is not an integer!'
exit 1
else
PATRON_ID=${!j}
REQUIRED_ARGS_COUNTER=$((REQUIRED_ARGS_COUNTER+1)) # up the required
fi
fi
# --in
if [[ ${!i} == '--in' ]]; then
if [[ ! -f ${!j} ]]; then
echo '[E] '${!j}' is not a valid file path!'
exit 1
else
PAYLOAD=${!j}
REQUIRED_ARGS_COUNTER=$((REQUIRED_ARGS_COUNTER+1)) # up the required
fi
fi
done
#
# begin main logic
if [[ ${REQUIRED_ARGS_COUNTER} != 2 ]]; then # if the wrong number of args are passed
echo '[E] Usage: '${0}' --patron-id <int> --in <file>'
echo
echo '[E] Required flags:'
echo '[E] --patron-id <int> The internal Koha patron identifier to match against.'
echo '[E] --in <file> What to send. File must be json.'
echo
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.'
exit 1
else
getConfig # grab our config
getToken # grab a token
updatePatron # do the update
fi