Fix API logic in api.sh (#6193)

This commit is contained in:
Dan Schaper
2025-05-11 08:43:12 -07:00
committed by GitHub
+14 -13
View File
@@ -20,7 +20,7 @@
TestAPIAvailability() { TestAPIAvailability() {
# as we are running locally, we can get the port value from FTL directly # as we are running locally, we can get the port value from FTL directly
local chaos_api_list authResponse authStatus authData local chaos_api_list authResponse authStatus authData apiAvailable
# Query the API URLs from FTL using CHAOS TXT local.api.ftl # Query the API URLs from FTL using CHAOS TXT local.api.ftl
# The result is a space-separated enumeration of full URLs # The result is a space-separated enumeration of full URLs
@@ -57,23 +57,23 @@ TestAPIAvailability() {
authData=$(printf %s "${authResponse%???}") authData=$(printf %s "${authResponse%???}")
# Test if http status code was 200 (OK) or 401 (authentication required) # Test if http status code was 200 (OK) or 401 (authentication required)
if [ ! "${authStatus}" = 200 ] && [ ! "${authStatus}" = 401 ]; then
# API is not available at this port/protocol combination
API_PORT=""
else
# API is available at this URL combination
if [ "${authStatus}" = 200 ]; then if [ "${authStatus}" = 200 ]; then
# API is available without authentication # API is available without authentication
apiAvailable=true
needAuth=false needAuth=false
fi break
elif [ "${authStatus}" = 401 ]; then
# API is available with authentication
apiAvailable=true
needAuth=true
# Check if 2FA is required # Check if 2FA is required
needTOTP=$(echo "${authData}"| jq --raw-output .session.totp 2>/dev/null) needTOTP=$(echo "${authData}"| jq --raw-output .session.totp 2>/dev/null)
break break
fi
else
# API is not available at this port/protocol combination
apiAvailable=false
# Remove the first URL from the list # Remove the first URL from the list
local last_api_list local last_api_list
last_api_list="${chaos_api_list}" last_api_list="${chaos_api_list}"
@@ -84,11 +84,12 @@ TestAPIAvailability() {
# Remove the last element # Remove the last element
chaos_api_list="" chaos_api_list=""
fi fi
fi
done done
# if API_PORT is empty, no working API port was found # if apiAvailable is false, no working API was found
if [ -n "${API_PORT}" ]; then if [ "${apiAvailable}" = false ]; then
echo "API not available at: ${API_URL}" echo "API not available. Please check FTL.log"
echo "Exiting." echo "Exiting."
exit 1 exit 1
fi fi