#!/usr/bin/env bash set -euo pipefail : "${HP_API_TOKEN:?Set the server-side API token}" [[ "$HP_API_TOKEN" =~ ^hp_live_[a-f0-9]{32}_[A-Za-z0-9_-]{43}$ ]] umask 077 temp=$(mktemp -d) trap 'rm -f "$temp/headers" "$temp/body"; rmdir "$temp"' EXIT for attempt in 0 1 2 3; do status=$(curl -sS --proto '=https' --max-time 6 --header @- \ -D "$temp/headers" -o "$temp/body" -w '%{http_code}' \ https://api.harmonic-patterns.com/v1/catalog <<<"Authorization: Bearer $HP_API_TOKEN") || status=000 case "$status" in 200) cat "$temp/body"; exit 0 ;; 429|503|000) ;; *) echo "HTTP $status" >&2; exit 1 ;; esac retry=$(awk 'tolower($1)=="retry-after:" {gsub(/\r/,"",$2); print $2}' "$temp/headers") retry=${retry:-0} [[ "$retry" =~ ^[0-9]+$ ]] && (( retry <= 60 && attempt < 3 )) || exit 1 wait_s=$((1 << attempt)) (( retry <= wait_s )) || wait_s=$retry sleep "$wait_s.$((100 + RANDOM % 900))" done