api-helper: Move client data assembly logic to a separate function

This commit is contained in:
Samantaz Fox 2024-03-30 22:51:18 +01:00
parent fd743bcc80
commit d45d85391e
No known key found for this signature in database
GPG Key ID: F42821059186176E

View File

@ -309,6 +309,57 @@ endpoint_select()
} }
#
# Function to craft the client data
#
make_request_data()
{
client="\"hl\":\"${hl}\",\"gl\":\"${gl}\""
client="${client},\"deviceMake\":\"${client_extra_device_make}\""
client="${client},\"deviceModel\":\"${client_extra_device_model}\""
if ! [ -z "$screen" ]; then
client="${client},\"clientScreen\":\"${screen}\""
fi
client="${client},\"clientName\":\"${client_name}\""
client="${client},\"clientVersion\":\"${client_vers}\""
if ! [ -z "$client_extra_os_name" ]; then
client="${client},\"osName\":\"${client_extra_os_name}\""
fi
if ! [ -z "$client_extra_os_vers" ]; then
client="${client},\"osVersion\":\"${client_extra_os_vers}\""
fi
if ! [ -z "$client_extra_platform" ]; then
client="${client},\"platform\":\"${client_extra_platform}\""
fi
if ! [ -z "$client_extra_form_factor" ]; then
client="${client},\"clientFormFactor\":\"${client_extra_form_factor}\""
fi
data="{\"context\":{\"client\":{$client}},$endpoint_data}"
# Basic debug
if [ $debug = true ]; then
if command -v jq >&2 >/dev/null; then
printf "\nSending: %s\n\n" "$data" | jq . >&2
else
printf "\nSending: %s\n\n" "$data" | sed 's/{/{\n/g; s/}/\n}/g; s/,/,\n/g' >&2
fi
fi
# Return data
printf "{\"context\":{\"client\":{%s}},%s}\n" "$client" "$endpoint_data"
}
# #
# Parameters init # Parameters init
# #
@ -323,7 +374,7 @@ data=""
# #
# Interactive client selection # Parse arguments
# #
while :; do while :; do
@ -449,7 +500,7 @@ fi
# #
# Client selection # Required parameters. Ask if not provided
# #
if [ -z "$client_option" ]; then if [ -z "$client_option" ]; then
@ -461,13 +512,6 @@ if [ -z "$client_option" ]; then
fi fi
fi fi
client_select "$client_option"
#
# Endpoint selection
#
if [ -z "$endpoint_option" ]; then if [ -z "$endpoint_option" ]; then
if [ $interactive = true ]; then if [ $interactive = true ]; then
print_endpoints print_endpoints
@ -480,63 +524,25 @@ fi
# new line # new line
echo >&2 echo >&2
endpoint_select "$endpoint_option"
#
# Interactive language/region selection # Interactive language/region selection
#
if [ $interactive = true ]; then if [ $interactive = true ]; then
hl=$(query_with_default "Enter content language (hl)" "en") hl=$(query_with_default "Enter content language (hl)" "en")
gl=$(query_with_default "Enter content region (gl)" "US") gl=$(query_with_default "Enter content region (gl)" "US")
client="\"hl\":\"${hl}\",\"gl\":\"${gl}\""
client="${client},\"deviceMake\":\"${client_extra_device_make}\""
client="${client},\"deviceModel\":\"${client_extra_device_model}\""
if ! [ -z "$screen" ]; then
client="${client},\"clientScreen\":\"${screen}\""
fi
client="${client},\"clientName\":\"${client_name}\""
client="${client},\"clientVersion\":\"${client_vers}\""
if ! [ -z "$client_extra_os_name" ]; then
client="${client},\"osName\":\"${client_extra_os_name}\""
fi
if ! [ -z "$client_extra_os_vers" ]; then
client="${client},\"osVersion\":\"${client_extra_os_vers}\""
fi
if ! [ -z "$client_extra_platform" ]; then
client="${client},\"platform\":\"${client_extra_platform}\""
fi
if ! [ -z "$client_extra_form_factor" ]; then
client="${client},\"clientFormFactor\":\"${client_extra_form_factor}\""
fi
data="{\"context\":{\"client\":{$client}},$endpoint_data}"
# Basic debug
if [ $debug = true ]; then
if command -v jq >&2 >/dev/null; then
printf "\nSending: %s\n\n" "$data" | jq . >&2
else
printf "\nSending: %s\n\n" "$data" | sed 's/{/{\n/g; s/}/\n}/g; s/,/,\n/g' >&2
fi
fi
fi fi
# #
# Final command # Run the request
# #
client_select "$client_option"
endpoint_select "$endpoint_option"
if [ $interactive = true ]; then
data=$(make_request_data)
fi
url="https://www.youtube.com/${endpoint}?key=${apikey}" url="https://www.youtube.com/${endpoint}?key=${apikey}"
# Headers # Headers