/
deqar-get
executable file
·98 lines (86 loc) · 2.34 KB
/
deqar-get
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
#!/bin/bash
#
# deqar-(get|head|submit|filesubmit)
#
# A simple script to make requests to DEQAR APIs
#
# This is mainly meant for ad-hoc queries to the Web API or to send test files
# to the Submission API. The script is not intended for use in regular, production
# data submission.
#
# base path
if [ "${DEQAR_BASE: -1}" != "/" ]; then
BASE="${DEQAR_BASE}/"
else
BASE="${DEQAR_BASE}"
fi
# determine our "personality"
case "$(basename $0)" in
deqar-get)
URL="${DEQAR_BASE}$1"
METHOD="GET"
FILE=""
;;
deqar-head)
URL="${DEQAR_BASE}$1"
METHOD="HEAD"
FILE=""
;;
deqar-post)
URL="${DEQAR_BASE}$1"
METHOD="POST"
FILE="$2"
CTYPE="application/json"
;;
deqar-put)
URL="${DEQAR_BASE}$1"
METHOD="PUT"
FILE="$2"
CTYPE="application/json"
;;
deqar-patch)
URL="${DEQAR_BASE}$1"
METHOD="PATCH"
FILE="$2"
CTYPE="application/json"
;;
deqar-submit)
URL="${DEQAR_BASE}submissionapi/v1/submit/report"
METHOD="POST"
FILE="$1"
CTYPE="application/json"
;;
deqar-filesubmit)
URL="${DEQAR_BASE}submissionapi/v1/submit/reportfile/$1/$(basename $2)"
METHOD="PUT"
FILE="$2"
CTYPE="$(file -b --mime-type $2)"
;;
*)
echo "$(basename $0): don't know what to do as this name"
exit 5
esac
if [ -x "$(command -v http)" ]; then
# we have HTTPie installed
if [ "$FILE" = "" ]; then
http $METHOD $URL "Authorization: Bearer $DEQAR_TOKEN" "${@:2}"
else
http -v $METHOD $URL "Authorization: Bearer $DEQAR_TOKEN" "Content-type: $CTYPE" "${@:3}" < $FILE
fi
elif [ -x "$(command -v curl)" ]; then
# fallback on curl, works equally well but less pretty
# use json_pp to pretty-print output, if available
if [ -x "$(command -v json_pp)" ]; then
PP="json_pp"
else
PP="cat"
fi
if [ "$FILE" = "" ]; then
curl -v -H "Authorization: Bearer $DEQAR_TOKEN" -X$METHOD $URL "${@:2}" | $PP
else
curl -v -H "Content-type: $CTYPE" -H "Authorization: Bearer $DEQAR_TOKEN" -X$METHOD $URL --data-binary @$FILE "${@:3}" | $PP
fi
else
echo "$(basename $0): you need to have either HTTPie or curl installed."
exit 10
fi