#!/usr/bin/env bash
set -euo pipefail
: "${CRYPTANIO_API_KEY:?Set CRYPTANIO_API_KEY}"
# A live SSE connection is deliberately limited to 30 seconds.
exec 3>&1
transfer_status=0
http_status="$(curl \
  --silent --show-error --no-buffer --max-time 30 --request GET \
  --header "Authorization: Bearer $CRYPTANIO_API_KEY" \
  --header 'Accept: text/event-stream' \
  "https://api.cryptanio.com/v1/stream" \
  --output /dev/fd/3 --write-out '%{http_code}')" || transfer_status=$?
exec 3>&-
if [[ "$http_status" == 2?? ]]; then
  exit "$transfer_status"
fi
printf 'HTTP %s\n' "$http_status" >&2
exit 1
