Add logging

This commit is contained in:
2025-10-16 05:45:19 -04:00
parent fb892c13ed
commit fedecacd0b
5 changed files with 158 additions and 23 deletions

View File

@@ -169,19 +169,50 @@ get_token () {
rest_api_get () {
check_get_token
http_response=$(curl -s -k -o /tmp/rbkresponse.$$ -w "%{http_code}" -X GET $ENDPOINT -H "accept: application/json" -H "Authorization: Bearer $AUTH_TOKEN")
if [ -n "$RBK_API_LOG" ]; then
{
echo "==== [GET] $ENDPOINT ===="
echo "-- Headers: accept: application/json, Authorization: Bearer <hidden>"
echo "-- Response ($http_response):"
jq . < /tmp/rbkresponse.$$ 2>/dev/null || cat /tmp/rbkresponse.$$
echo
} >> "$RBK_API_LOG"
fi
check_http_error
}
# HTTP POST: Given $ENDPOINT and $PAYLOAD write output to file
rest_api_post () {
check_get_token
http_response=$(curl -s -k -o /tmp/rbkresponse.$$ -w "%{http_code}" -X POST $ENDPOINT -H "accept: application/json" -H "Authorization: Bearer $AUTH_TOKEN" -H "Content-Type: application/json" -d $PAYLOAD)
http_response=$(curl -s -k -o /tmp/rbkresponse.$$ -w "%{http_code}" -X POST $ENDPOINT -H "accept: application/json" -H "Authorization: Bearer $AUTH_TOKEN" -H "Content-Type: application/json" -d "$PAYLOAD")
if [ -n "$RBK_API_LOG" ]; then
{
echo "==== [POST] $ENDPOINT ===="
echo "-- Headers: accept: application/json, Authorization: Bearer <hidden>, Content-Type: application/json"
echo "-- Payload:"
echo "$PAYLOAD" | jq . 2>/dev/null || echo "$PAYLOAD"
echo "-- Response ($http_response):"
jq . < /tmp/rbkresponse.$$ 2>/dev/null || cat /tmp/rbkresponse.$$
echo
} >> "$RBK_API_LOG"
fi
check_http_error
}
rest_api_post_file () {
check_get_token
http_response=$(curl -s -k -o /tmp/rbkresponse.$$ -w "%{http_code}" -X POST $ENDPOINT -H "accept: application/json" -H "Authorization: Bearer $AUTH_TOKEN" -H "Content-Type: application/json" --data-binary @/tmp/payload.$$)
if [ -n "$RBK_API_LOG" ]; then
{
echo "==== [POST FILE] $ENDPOINT ===="
echo "-- Headers: accept: application/json, Authorization: Bearer <hidden>, Content-Type: application/json"
echo "-- Payload file: /tmp/payload.$$"
jq . < /tmp/payload.$$ 2>/dev/null || cat /tmp/payload.$$
echo "-- Response ($http_response):"
jq . < /tmp/rbkresponse.$$ 2>/dev/null || cat /tmp/rbkresponse.$$
echo
} >> "$RBK_API_LOG"
fi
check_http_error
rm -f /tmp/payload.$$
}
@@ -189,18 +220,47 @@ rest_api_post_file () {
rest_api_post_empty () {
check_get_token
http_response=$(curl -s -k -o /tmp/rbkresponse.$$ -w "%{http_code}" -X POST $ENDPOINT -H "accept: application/json" -H "Authorization: Bearer $AUTH_TOKEN" -H "Content-Type: application/json")
if [ -n "$RBK_API_LOG" ]; then
{
echo "==== [POST EMPTY] $ENDPOINT ===="
echo "-- Headers: accept: application/json, Authorization: Bearer <hidden>, Content-Type: application/json"
echo "-- Response ($http_response):"
jq . < /tmp/rbkresponse.$$ 2>/dev/null || cat /tmp/rbkresponse.$$
echo
} >> "$RBK_API_LOG"
fi
check_http_error
}
rest_api_patch () {
check_get_token
http_response=$(curl -s -k -o /tmp/rbkresponse.$$ -w "%{http_code}" -X PATCH $ENDPOINT -H "accept: application/json" -H "Authorization: Bearer $AUTH_TOKEN" -H "Content-Type: application/json" -d $PAYLOAD)
http_response=$(curl -s -k -o /tmp/rbkresponse.$$ -w "%{http_code}" -X PATCH $ENDPOINT -H "accept: application/json" -H "Authorization: Bearer $AUTH_TOKEN" -H "Content-Type: application/json" -d "$PAYLOAD")
if [ -n "$RBK_API_LOG" ]; then
{
echo "==== [PATCH] $ENDPOINT ===="
echo "-- Headers: accept: application/json, Authorization: Bearer <hidden>, Content-Type: application/json"
echo "-- Payload:"
echo "$PAYLOAD" | jq . 2>/dev/null || echo "$PAYLOAD"
echo "-- Response ($http_response):"
jq . < /tmp/rbkresponse.$$ 2>/dev/null || cat /tmp/rbkresponse.$$
echo
} >> "$RBK_API_LOG"
fi
check_http_error
}
rest_api_delete () {
check_get_token
http_response=$(curl -s -k -o /tmp/rbkresponse.$$ -w "%{http_code}" -X DELETE $ENDPOINT -H "accept: application/json" -H "Authorization: Bearer $AUTH_TOKEN")
if [ -n "$RBK_API_LOG" ]; then
{
echo "==== [DELETE] $ENDPOINT ===="
echo "-- Headers: accept: application/json, Authorization: Bearer <hidden>"
echo "-- Response ($http_response):"
jq . < /tmp/rbkresponse.$$ 2>/dev/null || cat /tmp/rbkresponse.$$
echo
} >> "$RBK_API_LOG"
fi
check_http_error
}