This repository was archived by the owner on Mar 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi-test.sh
More file actions
executable file
·238 lines (219 loc) · 6.26 KB
/
api-test.sh
File metadata and controls
executable file
·238 lines (219 loc) · 6.26 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/bash
function usage() {
echo "usage: $0 -s server
-p sps|labtest (post sample signal)
-c correlation_id (optional, use with -p. This will create a sample signal containing a correction to a previous signal)
-q all|query-parameters (get signals from the ISN server)
unless running in a local dev env, you must set a BEARER_TOKEN environment variable specifying the bearer token for your site before running the script
use '-q token' to get details of indieauth token associated with your bearer token ">&2
exit 1
}
function isDevEnv() {
if [ "$(uname)" = "Darwin" ]; then
return 0
else
return 1
fi
}
function getScheme() {
if isDevEnv ; then
echo http
else
echo https
fi
}
function getETA() {
if isDevEnv ; then
date -u -v+2d +"%Y-%m-%dT%H:00:00Z" # mac
else
date -u -d "+2 days" +"%Y-%m-%dT%H:00:00"Z
fi
}
function spsSignal() {
u=$1
e=$2
cat <<!
{
"h": "event",
"name": "chicken and beef (movement ref: $u)",
"start": "$e",
"summary": "moving to PortA with ETA $e",
"category": [
"pre-notification",
"isn@sps-signal.info-sharing.network"
],
"payload": {
"cnCodes": [
"010594",
"020100"
],
"commodityDescription": "Chicken 40%, beef 60%",
"countryOfOrigin": "GB",
"chedNumbers": [
"CHEDA .GB.YYYY.XXXXXXX"
],
"unitIdentification": {
"containerNumber": "12346"
},
"mode": "RORO",
"exporterEORI": "EORI-EXP-01",
"importerEORI": "EORI-IMP-01"
}
}
!
}
function spsSignalCorrection() {
u=$1
c=$2
cat <<!
{
"h": "event",
"name": "chicken and pork (movement ref: $u)",
"summary": "correction",
"category": [
"pre-notification",
"isn@sps-signal.info-sharing.network"
],
"correlation-id": "$c",
"payload": {
"cnCodes": [
"010594",
"020300"
],
"commodityDescription": "Chicken 40%, pork 60%"
}
}
!
}
function labtestSignal() {
u=$1
e=$2
cat <<!
{
"h": "event",
"name": "lab sample B (lab test ref: $u)",
"start": "$e",
"summary": "unsatisfactory test",
"category": [
"lab-test-result",
"isn@lab-test-signal.info-sharing.network"
],
"payload": {
"cnCode": "010300",
"commodityDescription": "beef hocks",
"storageTemperature": "ambient",
"countryOfOrigin": "FR",
"testDate": "2024-07-05T16:51:51.379676Z",
"batchNumber": 134149,
"hazardIndicator": "Aflatoxin B1",
"acceptedHazardLevel": 2,
"testResult": 3,
"testOutcome": "unsatisfactory",
"countryofLab": "FR"
}
}
!
}
function labtestSignalCorrection() {
u=$1
c="$2"
cat <<!
{
"h": "event",
"name": "lab sample B (lab test ref: $u)",
"summary": "satisfactory test (correction)",
"category": [
"lab-test",
"isn@lab-test-signal.info-sharing.network"
],
"correlation-id": "$c",
"payload": {
"hazardIndicator": "none",
"testOutcome": "satisfactory"
}
}
!
}
# main
while getopts "s:p:q:c:" arg; do
case $arg in
s) server=$(getScheme)://$OPTARG;;
q) query=0 ; query_parameters=$OPTARG;;
p) post=0 ; post_type=$OPTARG;;
c) correlation_id=$OPTARG;;
*) usage;;
esac
done
if [ -z "$server" ]; then
echo "specify a server" >&2
usage
fi
if [ "$correlation_id" ] && [ -z "$post" ]; then
echo "error: the -c correlation id option cam only be used with -p" >&2
usage
fi
if [ -z "$post" ] && [ -z "$query" ]; then
echo "error: you must specify -q or -p " >&2
usage
fi
if [ "$query" ] && [ "$post" ]; then
echo "error: -p and -q can't be used together" >&2
usage
fi
if ! isDevEnv ; then
if [ -z "$BEARER_TOKEN" ]; then
echo "set BEARER_TOKEN variable" >&2
exit 1;
fi
fi
if [ "$post" ] ; then
id=$(LC_ALL=C tr -dc A-Z0-9 </dev/urandom | head -c 4)
eta=$(getETA)
echo "transaction ref: $id"
case $post_type in
sps)
if [ "$correlation_id" ]; then
curl --silent -i -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $BEARER_TOKEN" \
-d "$(spsSignalCorrection $id $correlation_id)" \
$server/micropub |egrep "HTTP|^\"|^Location"
else
curl --silent -i -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $BEARER_TOKEN" \
-d "$(spsSignal $id $eta)" \
$server/micropub |egrep "HTTP|^\"|^Location"
fi ;;
labtest)
if [ "$correlation_id" ]; then
curl --silent -i -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $BEARER_TOKEN" \
-d "$(labtestSignalCorrection $id $correlation_id)" \
$server/micropub |egrep "HTTP|^\"|^Location"
else
curl --silent -i -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $BEARER_TOKEN" \
-d "$(labtestSignal $id $eta)" \
$server/micropub |egrep "HTTP|^\"|^Location"
fi ;;
*)
echo unknown post option >&2; usage ;;
esac
fi
if [ "$query" ]; then
case $query_parameters in
all)
curl --silent -H "Authorization: Bearer $BEARER_TOKEN" "$server/signals";;
*=*)
curl --silent -H "Authorization: Bearer $BEARER_TOKEN" "$server/signals?$query_parameters";;
token)
curl --silent -H "Accept: application/json" \
-H "Authorization: Bearer $BEARER_TOKEN" \
"https://tokens.indieauth.com/token";;
*)
echo "invalid query parameters supplied (should be "all" or, for example, countryOfOrigin=FR ) " >&2; usage ;;
esac
fi