#!/usr/bin/env bash
set -euo pipefail

: "${VOYCED_API_BASE_URL:=https://sandbox.voycedconnect.eu/v1}"
: "${VOYCED_API_KEY:?Set VOYCED_API_KEY}"
: "${VOYCED_API_SECRET:?Set VOYCED_API_SECRET}"

PAYLOAD=$(python3 - <<'PYJSON'
import json, os
print(json.dumps({"api_key": os.environ["VOYCED_API_KEY"], "api_secret": os.environ["VOYCED_API_SECRET"]}))
PYJSON
)
TOKEN_RESPONSE=$(curl --fail-with-body --silent --show-error \
  --proto '=https' --tlsv1.2 --connect-timeout 5 --max-time 20 \
  --request POST --url "$VOYCED_API_BASE_URL/auth/token" \
  --header 'Accept: application/json' --header 'Content-Type: application/json' \
  --data "$PAYLOAD")
TOKEN=$(printf '%s' "$TOKEN_RESPONSE" | python3 -c 'import json,sys; print(json.load(sys.stdin)["data"]["access_token"])')

curl --fail-with-body --silent --show-error \
  --proto '=https' --tlsv1.2 --connect-timeout 5 --max-time 20 \
  --request GET --url "$VOYCED_API_BASE_URL/customer" \
  --header 'Accept: application/json' --header "Authorization: Bearer $TOKEN"
