This commit is contained in:
parent
71c393d91d
commit
a92f188c91
1 changed files with 13 additions and 16 deletions
|
|
@ -67,36 +67,32 @@ query_api() {
|
|||
local domain="$1"
|
||||
local max_attempts=5
|
||||
local attempt=0
|
||||
local resp=""
|
||||
local http_code=""
|
||||
local content_type=""
|
||||
local max_sleep=8
|
||||
|
||||
while :; do
|
||||
attempt=$((attempt+1))
|
||||
dbg " -> API attempt #$attempt for $domain"
|
||||
|
||||
resp="$(timeout 20 curl -sS --compressed \
|
||||
raw="$(timeout 20 curl -sS --compressed \
|
||||
-m 10 --connect-timeout 5 \
|
||||
-H 'Accept: application/json' \
|
||||
-w '\n%{http_code}\n%{content_type}' \
|
||||
"${API_URL}${domain}" 2>>"$DEBUG_LOG" || true)"
|
||||
|
||||
http_code="$(printf '%s' "$resp" | tail -n1)"
|
||||
content_type="$(printf '%s' "$resp" | tail -n2 | head -n1)"
|
||||
resp="$(printf '%s' "$resp" | head -n -2)"
|
||||
# корректно разделяем тело и метаданные
|
||||
http_code="$(printf '%s' "$raw" | tail -n1)"
|
||||
content_type="$(printf '%s' "$raw" | tail -n2 | head -n1)"
|
||||
body="$(printf '%s' "$raw" | head -n -2)" # тело без последних двух строк
|
||||
|
||||
preview="$(printf '%s' "$resp" | tr '\n' ' ' | cut -c1-400)"
|
||||
# убираем возможные пустые строки
|
||||
body="$(printf '%s' "$body" | sed '/^[[:space:]]*$/d')"
|
||||
|
||||
preview="$(printf '%s' "$body" | tr '\n' ' ' | cut -c1-400)"
|
||||
dbg " -> HTTP=${http_code}, Content-Type=${content_type}, preview=${preview}"
|
||||
|
||||
if [ "$http_code" = "200" ] && printf '%s' "$content_type" | grep -qEi '^application/json|^text/json'; then
|
||||
echo "$resp"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if jq -e . >/dev/null 2>&1 <<<"$resp"; then
|
||||
dbg " -> Body is valid JSON despite HTTP=${http_code}, accepting."
|
||||
echo "$resp"
|
||||
# пустой body — retry
|
||||
if [ -n "$body" ] && jq -e . >/dev/null 2>&1 <<<"$body"; then
|
||||
echo "$body"
|
||||
return 0
|
||||
fi
|
||||
|
||||
|
|
@ -113,6 +109,7 @@ query_api() {
|
|||
done
|
||||
}
|
||||
|
||||
|
||||
if [ ! -f "$INPUT_FILE" ]; then
|
||||
err "Input file not found: $INPUT_FILE"
|
||||
exit 3
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue