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 domain="$1"
|
||||||
local max_attempts=5
|
local max_attempts=5
|
||||||
local attempt=0
|
local attempt=0
|
||||||
local resp=""
|
|
||||||
local http_code=""
|
|
||||||
local content_type=""
|
|
||||||
local max_sleep=8
|
local max_sleep=8
|
||||||
|
|
||||||
while :; do
|
while :; do
|
||||||
attempt=$((attempt+1))
|
attempt=$((attempt+1))
|
||||||
dbg " -> API attempt #$attempt for $domain"
|
dbg " -> API attempt #$attempt for $domain"
|
||||||
|
|
||||||
resp="$(timeout 20 curl -sS --compressed \
|
raw="$(timeout 20 curl -sS --compressed \
|
||||||
-m 10 --connect-timeout 5 \
|
-m 10 --connect-timeout 5 \
|
||||||
-H 'Accept: application/json' \
|
-H 'Accept: application/json' \
|
||||||
-w '\n%{http_code}\n%{content_type}' \
|
-w '\n%{http_code}\n%{content_type}' \
|
||||||
"${API_URL}${domain}" 2>>"$DEBUG_LOG" || true)"
|
"${API_URL}${domain}" 2>>"$DEBUG_LOG" || true)"
|
||||||
|
|
||||||
http_code="$(printf '%s' "$resp" | tail -n1)"
|
# корректно разделяем тело и метаданные
|
||||||
content_type="$(printf '%s' "$resp" | tail -n2 | head -n1)"
|
http_code="$(printf '%s' "$raw" | tail -n1)"
|
||||||
resp="$(printf '%s' "$resp" | head -n -2)"
|
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}"
|
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
|
# пустой body — retry
|
||||||
echo "$resp"
|
if [ -n "$body" ] && jq -e . >/dev/null 2>&1 <<<"$body"; then
|
||||||
return 0
|
echo "$body"
|
||||||
fi
|
|
||||||
|
|
||||||
if jq -e . >/dev/null 2>&1 <<<"$resp"; then
|
|
||||||
dbg " -> Body is valid JSON despite HTTP=${http_code}, accepting."
|
|
||||||
echo "$resp"
|
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
@ -113,6 +109,7 @@ query_api() {
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if [ ! -f "$INPUT_FILE" ]; then
|
if [ ! -f "$INPUT_FILE" ]; then
|
||||||
err "Input file not found: $INPUT_FILE"
|
err "Input file not found: $INPUT_FILE"
|
||||||
exit 3
|
exit 3
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue