Fix jq for oneline JSON answer
Some checks failed
Deploy DNS Configuration / deploy (push) Has been cancelled
Some checks failed
Deploy DNS Configuration / deploy (push) Has been cancelled
This commit is contained in:
parent
a92f188c91
commit
568701d1cb
1 changed files with 19 additions and 21 deletions
|
|
@ -25,8 +25,9 @@ log() { printf '%s\n' "$*"; }
|
||||||
dbg() { if [ "$DEBUG" != "0" ]; then printf '[DEBUG] %s\n' "$*" | tee -a "$DEBUG_LOG"; fi }
|
dbg() { if [ "$DEBUG" != "0" ]; then printf '[DEBUG] %s\n' "$*" | tee -a "$DEBUG_LOG"; fi }
|
||||||
err() { printf '[ERROR] %s\n' "$*" | tee -a "$DEBUG_LOG" >&2; }
|
err() { printf '[ERROR] %s\n' "$*" | tee -a "$DEBUG_LOG" >&2; }
|
||||||
|
|
||||||
if ! command -v curl >/dev/null 2>&1; then err "curl is required"; exit 2; fi
|
for cmd in curl jq; do
|
||||||
if ! command -v jq >/dev/null 2>&1; then err "jq is required"; exit 2; fi
|
if ! command -v "$cmd" >/dev/null 2>&1; then err "$cmd is required"; exit 2; fi
|
||||||
|
done
|
||||||
|
|
||||||
if [ "$DEBUG" != "0" ]; then : > "$DEBUG_LOG"; dbg "Debugging enabled"; fi
|
if [ "$DEBUG" != "0" ]; then : > "$DEBUG_LOG"; dbg "Debugging enabled"; fi
|
||||||
|
|
||||||
|
|
@ -68,37 +69,35 @@ query_api() {
|
||||||
local max_attempts=5
|
local max_attempts=5
|
||||||
local attempt=0
|
local attempt=0
|
||||||
local max_sleep=8
|
local max_sleep=8
|
||||||
|
local tmpfile body http_code sleep_time
|
||||||
|
|
||||||
while :; do
|
while :; do
|
||||||
attempt=$((attempt+1))
|
attempt=$((attempt+1))
|
||||||
dbg " -> API attempt #$attempt for $domain"
|
dbg " -> API attempt #$attempt for $domain"
|
||||||
|
|
||||||
raw="$(timeout 20 curl -sS --compressed \
|
tmpfile=$(mktemp)
|
||||||
|
# curl с отдельным файлом для тела, HTTP-код через -w
|
||||||
|
http_code=$(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 '%{http_code}' \
|
||||||
"${API_URL}${domain}" 2>>"$DEBUG_LOG" || true)"
|
-o "$tmpfile" \
|
||||||
|
"${API_URL}${domain}" 2>>"$DEBUG_LOG" || true)
|
||||||
|
|
||||||
# корректно разделяем тело и метаданные
|
body=$(cat "$tmpfile")
|
||||||
http_code="$(printf '%s' "$raw" | tail -n1)"
|
rm -f "$tmpfile"
|
||||||
content_type="$(printf '%s' "$raw" | tail -n2 | head -n1)"
|
|
||||||
body="$(printf '%s' "$raw" | head -n -2)" # тело без последних двух строк
|
|
||||||
|
|
||||||
# убираем возможные пустые строки
|
|
||||||
body="$(printf '%s' "$body" | sed '/^[[:space:]]*$/d')"
|
|
||||||
|
|
||||||
preview="$(printf '%s' "$body" | tr '\n' ' ' | cut -c1-400)"
|
preview="$(printf '%s' "$body" | tr '\n' ' ' | cut -c1-400)"
|
||||||
dbg " -> HTTP=${http_code}, Content-Type=${content_type}, preview=${preview}"
|
dbg " -> HTTP=$http_code, preview=${preview}"
|
||||||
|
|
||||||
# пустой body — retry
|
if [ "$http_code" = "200" ] && jq -e . >/dev/null 2>&1 <<<"$body"; then
|
||||||
if [ -n "$body" ] && jq -e . >/dev/null 2>&1 <<<"$body"; then
|
|
||||||
echo "$body"
|
echo "$body"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$attempt" -ge "$max_attempts" ]; then
|
if [ "$attempt" -ge "$max_attempts" ]; then
|
||||||
ERRORS["$domain"]="http_${http_code}_or_nonjson"
|
ERRORS["$domain"]="http_${http_code}_or_invalid_json"
|
||||||
dbg " -> Failed after $attempt attempts: HTTP=${http_code}, preview=${preview}"
|
dbg " -> Failed after $attempt attempts, preview=${preview}"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
@ -109,7 +108,6 @@ 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
|
||||||
|
|
@ -119,7 +117,7 @@ raw_total_lines=$(wc -l < "$INPUT_FILE" | tr -d ' ')
|
||||||
dbg "Raw input lines: $raw_total_lines"
|
dbg "Raw input lines: $raw_total_lines"
|
||||||
|
|
||||||
lineno=0
|
lineno=0
|
||||||
while IFS= read -r line; do
|
while IFS= read -r line || [ -n "$line" ]; do
|
||||||
lineno=$((lineno+1))
|
lineno=$((lineno+1))
|
||||||
total_lines=$((total_lines+1))
|
total_lines=$((total_lines+1))
|
||||||
dbg "Processing line #$lineno: '$line'"
|
dbg "Processing line #$lineno: '$line'"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue