Enhance query API code
All checks were successful
Deploy DNS Configuration / deploy (push) Successful in 2m15s
All checks were successful
Deploy DNS Configuration / deploy (push) Successful in 2m15s
ADD DOMAIN: cloud.hetzner.com
This commit is contained in:
parent
58ecd19fc3
commit
fca3ae254a
2 changed files with 57 additions and 9 deletions
|
|
@ -114,4 +114,5 @@ yt3.ggpht.com
|
||||||
yt4.ggpht.com
|
yt4.ggpht.com
|
||||||
yt.be
|
yt.be
|
||||||
zona.media
|
zona.media
|
||||||
googlevideo.com
|
googlevideo.com
|
||||||
|
cloud.hetzner.com
|
||||||
|
|
@ -87,18 +87,65 @@ while IFS= read -r line || [ -n "$line" ]; do
|
||||||
normalized_ok=$((normalized_ok+1))
|
normalized_ok=$((normalized_ok+1))
|
||||||
dbg " -> NORMALIZED: $dom_norm"
|
dbg " -> NORMALIZED: $dom_norm"
|
||||||
|
|
||||||
dbg " -> Querying API: ${API_URL}${dom_norm}"
|
dbg " -> Querying API: ${API_URL}${dom_norm}"
|
||||||
resp="$(curl -s --max-time 15 "${API_URL}${dom_norm}" || true)"
|
|
||||||
preview="$(printf '%s' "$resp" | tr '\n' ' ' | cut -c1-400)"
|
|
||||||
dbg " -> API response preview: ${preview}"
|
|
||||||
|
|
||||||
if ! jq -e . >/dev/null 2>&1 <<<"$resp"; then
|
# параметры retry
|
||||||
api_error=$((api_error+1))
|
max_attempts=3
|
||||||
ERRORS["$dom_norm"]="non-json-response"
|
attempt=0
|
||||||
dbg " -> non-JSON, skipping"
|
resp=""
|
||||||
|
http_code=0
|
||||||
|
content_type=""
|
||||||
|
|
||||||
|
while :; do
|
||||||
|
attempt=$((attempt+1))
|
||||||
|
# делаем запрос, записываем тело и код отдельно
|
||||||
|
# --compressed: поддержка gzip
|
||||||
|
# -sS: показывать ошибки curl в debug (если DEBUG enabled, они попадут в DEBUG_LOG)
|
||||||
|
# -m: общий timeout
|
||||||
|
# --connect-timeout: timeout на установление соединения
|
||||||
|
raw="$(curl -sS --compressed -m 20 --connect-timeout 8 -H 'Accept: application/json' -w '\n%{http_code}\n%{content_type}' "${API_URL}${dom_norm}" 2>>"$DEBUG_LOG" )" || true
|
||||||
|
|
||||||
|
# последний \n%{http_code}\n%{content_type} — отделяем
|
||||||
|
http_code="$(printf '%s' "$raw" | tail -n1)"
|
||||||
|
content_type="$(printf '%s' "$raw" | tail -n2 | head -n1)"
|
||||||
|
resp="$(printf '%s' "$raw" | sed '$d' | sed '$d')" # всё кроме двух последних строк
|
||||||
|
|
||||||
|
preview="$(printf '%s' "$resp" | tr '\n' ' ' | cut -c1-400)"
|
||||||
|
dbg " -> Attempt #${attempt}: HTTP=${http_code}, Content-Type=${content_type}, preview=${preview}"
|
||||||
|
|
||||||
|
# если получили 200 и content-type похож на json — выходим
|
||||||
|
if [ "$http_code" = "200" ] && printf '%s' "$content_type" | grep -qEi 'application/(json|javascript)|^text/json'; then
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
|
||||||
|
# если тело выглядит как json (на всякий случай), тоже принимаем
|
||||||
|
if jq -e . >/dev/null 2>&1 <<<"$resp"; then
|
||||||
|
dbg " -> Body is valid JSON despite HTTP=${http_code}, accepting."
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
|
||||||
|
# если исчерпали попытки — помечаем как ошибка
|
||||||
|
if [ "$attempt" -ge "$max_attempts" ]; then
|
||||||
|
api_error=$((api_error+1))
|
||||||
|
ERRORS["$dom_norm"]="http_${http_code}_or_nonjson"
|
||||||
|
dbg " -> Failed after ${attempt} attempts: HTTP=${http_code}, preview=${preview}"
|
||||||
|
resp="" # явный маркер
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
|
||||||
|
# backoff: 0.5s, 1s, 2s ...
|
||||||
|
sleep_time=$(awk "BEGIN {printf \"%.0f\", 0.5 * (2 ^ ($attempt - 1))}")
|
||||||
|
dbg " -> Retry after ${sleep_time}s..."
|
||||||
|
sleep "$sleep_time"
|
||||||
|
done
|
||||||
|
|
||||||
|
# если нет валидного JSON — пропускаем, с логом
|
||||||
|
if [ -z "$resp" ] || ! jq -e . >/dev/null 2>&1 <<<"$resp"; then
|
||||||
|
dbg " -> non-JSON or empty response, skipping domain: $dom_norm"
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
# error handling
|
# error handling
|
||||||
if jq -e 'has("error")' <<<"$resp" >/dev/null; then
|
if jq -e 'has("error")' <<<"$resp" >/dev/null; then
|
||||||
err_msg="$(jq -r '.error' <<<"$resp")"
|
err_msg="$(jq -r '.error' <<<"$resp")"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue