Last active
April 3, 2020 13:14
-
-
Save nandilugio/8e5d5066da8ac143fde4ddc280c1129c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Dependencies ================================================================= | |
err_unless_executable () { | |
if ! [ -x "$(command -v $1)" ]; then | |
echo "Program '$1' is required. Please install it and make it available in your \$PATH. Aborting." >&2 | |
exit 1 | |
fi | |
} | |
# TODO: can we expect curl these deps to be installed in all target machines? | |
err_unless_executable curl | |
err_unless_executable python | |
# JSON ========================================================================= | |
json_read () { | |
local path=$1 | |
local json_str=$2 | |
echo "$json_str" | python -c "import sys, json; print(json.load(sys.stdin)${path})" | |
} | |
# Test if available python version is compatible with this implementation of json_read() | |
if [ "123" != "$(json_read '["asd"][3]["zxc"]' '{"asd": ["q", "w", "e", {"zxc": 123}]}')" ] | |
then | |
echo "Available Python version is not compatible. Aborting." >&2 | |
exit 1 | |
fi | |
# HTTP ========================================================================= | |
safe_curl () { | |
local request_method=$1 | |
local url=$2 | |
shift 2 # So now "$@" is all the remaining args | |
local body_and_status | |
local exit_code | |
# NOTE: --disable (local config) has to be the 1st param to curl (see man) | |
body_and_status="$(curl --disable --location --silent --write-out "\nHTTP_CODE:%{http_code}" -X "$request_method" "$url" "$@")" | |
exit_code=$? | |
# Output vars | |
# See https://superuser.com/questions/501690/curl-http-code-of-000 | |
last_http_status="$(echo "$body_and_status" | tail -n1 | cut -d":" -f2)" | |
last_response_body="$(echo "$body_and_status" | sed '$d')" | |
last_curl_exit_code=$exit_code | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment