-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
api.sh
108 lines (91 loc) · 3.62 KB
/
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
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
#!/usr/bin/env bash
# ==============================================================================
# Home Assistant Community Add-ons: Bashio
# Bashio is a bash function library for use with Home Assistant add-ons.
#
# It contains a set of commonly used operations and can be used
# to be included in add-on scripts to reduce code duplication across add-ons.
# ==============================================================================
# ------------------------------------------------------------------------------
# Makes a call to the Supervisor API.
#
# Arguments:
# $1 HTTP Method (GET/POST)
# $2 API Resource requested
# $3 Whether or not this resource returns raw data instead of json (optional)
# $3 In case of a POST method, this parameter is the JSON to POST (optional)
# $4 jq filter command (optional)
# ------------------------------------------------------------------------------
function bashio::api.supervisor() {
local method=${1}
local resource=${2}
local raw=${3:-}
local filter=${4:-}
local auth_header='Authorization: Bearer'
local response
local status
local data='{}'
local result
bashio::log.trace "${FUNCNAME[0]}" "$@"
if [[ -n "${__BASHIO_SUPERVISOR_TOKEN:-}" ]]; then
auth_header="Authorization: Bearer ${__BASHIO_SUPERVISOR_TOKEN}"
fi
if [[ "${method}" = "POST" ]] && bashio::var.has_value "${raw}"; then
data="${raw}"
fi
if ! response=$(curl --silent --show-error \
--write-out '\n%{http_code}' --request "${method}" \
-H "${auth_header}" \
-H "Content-Type: application/json" \
-d "${data}" \
"${__BASHIO_SUPERVISOR_API}${resource}"
); then
bashio::log.debug "${response}"
bashio::log.error "Something went wrong contacting the API"
return "${__BASHIO_EXIT_NOK}"
fi
status=${response##*$'\n'}
response=${response%"$status"}
bashio::log.debug "Requested API resource: ${__BASHIO_SUPERVISOR_API}${resource}"
bashio::log.debug "Request method: ${method}"
bashio::log.debug "Request data: ${data}"
bashio::log.debug "API HTTP Response code: ${status}"
bashio::log.debug "API Response: ${response}"
if [[ "${status}" -eq 401 ]]; then
bashio::log.error "Unable to authenticate with the API, permission denied"
return "${__BASHIO_EXIT_NOK}"
fi
if [[ "${status}" -eq 403 ]]; then
bashio::log.error "Unable to access the API, forbidden"
return "${__BASHIO_EXIT_NOK}"
fi
if [[ "${status}" -eq 404 ]]; then
bashio::log.error "Requested resource ${resource} was not found"
return "${__BASHIO_EXIT_NOK}"
fi
if [[ "${status}" -eq 405 ]]; then
bashio::log.error "Requested resource ${resource} was called using an" \
"unallowed method."
return "${__BASHIO_EXIT_NOK}"
fi
if [[ $(bashio::jq "${response}" ".result") = "error" ]]; then
bashio::log.error "Got unexpected response from the API:" \
"$(bashio::jq "${response}" '.message // empty')"
return "${__BASHIO_EXIT_NOK}"
fi
if [[ "${status}" -ne 200 ]]; then
bashio::log.error "Unknown HTTP error occurred"
return "${__BASHIO_EXIT_NOK}"
fi
if bashio::var.true "${raw}"; then
echo "${response}"
return "${__BASHIO_EXIT_OK}"
fi
result=$(bashio::jq "${response}" 'if .data == {} then empty else .data end')
if bashio::var.has_value "${filter}"; then
bashio::log.debug "Filtering response using: ${filter}"
result=$(bashio::jq "${result}" "${filter}")
fi
echo "${result}"
return "${__BASHIO_EXIT_OK}"
}