-
Notifications
You must be signed in to change notification settings - Fork 1
/
run-api.sh
executable file
·59 lines (50 loc) · 1011 Bytes
/
run-api.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
#!/bin/bash
HOST=${HOST:-localhost:5000}
usage() {
cat << EOF
Usage:
load <filename>
Loads a document and returns its ID
ask <id> <question>
Asks a question to see if ALBERT can answer it
from the contents of the document identified by 'id'
EOF
}
ARG=$1
case "$ARG" in
load)
curl "$HOST/documents" \
-XPOST \
-H "Content-Type: application/text" \
-d "@$2"
;;
ask)
id="$2"
if [ -z "$id" ]
then
usage
exit 1
fi
# Bash urlencoding: https://stackoverflow.com/a/10660730
question="${@:3}"
if [ -z "$question" ]
then
usage
exit 1
fi
strlen=${#question}
encoded=""
#pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${question:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
curl "$HOST/documents/$id/answer?question=$encoded"
;;
*)
usage
esac