Speed up pihole --api

Get session authentication information via single jq operation,
setting defaults if no data returned.

Simplify jq test for valid JSON data

Signed-off-by: Rob Gill <rrobgill@protonmail.com>
This commit is contained in:
Rob Gill 2025-07-14 05:44:46 +10:00
parent 1e88ce4975
commit 0187087da0
No known key found for this signature in database
GPG Key ID: 625CBEA2C626280D

View File

@ -183,13 +183,20 @@ Authentication() {
echo "No response from FTL server. Please check connectivity" echo "No response from FTL server. Please check connectivity"
exit 1 exit 1
fi fi
# obtain validity, session ID and sessionMessage from session response
validSession=$(echo "${sessionResponse}"| jq .session.valid 2>/dev/null)
SID=$(echo "${sessionResponse}"| jq --raw-output .session.sid 2>/dev/null)
sessionMessage=$(echo "${sessionResponse}"| jq --raw-output .session.message 2>/dev/null)
# obtain the error message from the session response # obtain validity, session ID, sessionMessage and error message from
sessionError=$(echo "${sessionResponse}"| jq --raw-output .error.message 2>/dev/null) # session response, apply default values if none returned
result=$(echo "${sessionResponse}" | jq -r '
(.session.valid // false),
(.session.sid // null),
(.session.message // null),
(.error.message // null)
' 2>/dev/null)
validSession=$(echo "${result}" | sed -n '1p')
SID=$(echo "${result}" | sed -n '2p')
sessionMessage=$(echo "${result}" | sed -n '3p')
sessionError=$(echo "${result}" | sed -n '4p')
if [ "${1}" = "verbose" ]; then if [ "${1}" = "verbose" ]; then
if [ "${validSession}" = true ]; then if [ "${validSession}" = true ]; then
@ -353,12 +360,9 @@ apiFunc() {
if [ "${verbosity}" = "verbose" ]; then if [ "${verbosity}" = "verbose" ]; then
echo "Data:" echo "Data:"
fi fi
# Attempt to print the data with jq, if it is not valid JSON, or not installed
if command -v jq >/dev/null && echo "${data}" | jq . >/dev/null 2>&1; then # then print the plain text.
echo "${data}" | jq . echo "${data}" | jq . 2>/dev/null || echo "${data}"
else
echo "${data}"
fi
# Delete the session # Delete the session
LogoutAPI "${verbosity}" LogoutAPI "${verbosity}"