-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconcile
executable file
·95 lines (86 loc) · 2.41 KB
/
reconcile
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
#!/usr/bin/bash
# Minimal Reconciliation Service API client.
# Requires bash, curl and jq.
# No support of properties, suggest nor data extension
set -euo pipefail
usage() {
echo "$0 [options] [queries...]"
echo "Options:"
echo " -a URL API endpoint URL (required)"
echo " -t id type (optional)"
echo " -v version API version (0.1/0.2=default/draft)"
echo " -l number limit (default: 10)"
echo " -n don't execute request but print it"
echo
echo "Examples:"
echo " $0 -a https://wikidata.reconci.link/en/api"
echo " $0 -a https://wikidata.reconci.link/en/api Marx"
echo " $0 -a https://services.getty.edu/vocab/reconcile/ -t /aat Maria"
echo " $0 -n -v draft \"Emma Goldman\""
exit
}
api=
limit=10
version=0.2
typeid=
execute=1
[ $# -eq 0 ] && usage
while getopts "a:v:t:l:hn" arg; do
case "$arg" in
a) api="$OPTARG";;
v) version="$OPTARG";;
t) typeid="$OPTARG";;
l) limit="$OPTARG";;
n) execute=;;
*) usage;;
esac
done
shift $(($OPTIND - 1))
if [[ ! "$api" =~ ^https?://.* ]]; then
if [ ! -z "$execute" ]; then
echo "Missing API endpoint (-a)"
exit 1
fi
fi
if [ "$version" == "draft" ]; then
api=${api%/}/reconcile
fi
fetch() {
set +e
res=$(curl -s "$@" 2>&1)
ok=$?
set -e
if [ "$ok" -eq 0 ]; then
echo "$res" | jq
else
echo "$res"
exit 1
fi
}
if [ $# -eq 0 ]; then
if [ -z "$execute" ]; then
echo "$api"
else
fetch "$api"
fi
else
queries=$(for query in "$@"; do echo "$query"; done | jq --raw-input .)
if [ "$version" == "draft" ]; then
request=$(echo "$queries" | jq --slurp --arg type "$typeid" --arg limit "$limit" \
'{queries:map({query:.,limit:($limit|tonumber)}+if $type!="" then {$type} else {} end)}'
)
else
request=$(echo "$queries" | jq --slurp --arg type "$typeid" --arg limit "$limit" \
'map({key:.,value:({query:.,limit:($limit|tonumber)}+if $type!="" then {$type} else {} end)})|from_entries'
)
fi
if [ -z "$execute" ]; then
echo "$request" | jq
else
if [ "$version" == "draft" ]; then
fetch -X POST --fail-with-body "$api" -H 'Content-Type: application/json' -d "$request"
else
fetch -X POST --fail-with-body "$api" -d "queries=$request"
fi
fi
fi