# First step for any 401 or 403: ask the API which workspace, mode and scopes your key has. Python 3.9+ with requests.
import os
import sys

import requests

base = os.environ.get("RELATIONSAI_API_BASE", "https://voice-api.relationsai.com/v1")

r = requests.get(
    f"{base}/workspace",
    headers={"Authorization": f"Bearer {os.environ.get('RELATIONSAI_API_KEY', '')}"},
    timeout=30,
)
body = r.json()
if r.ok:
    key = body["api_key"]
    print(f"OK: {body['name']}, {key['environment']} key {key['id']}, scopes: {' '.join(key['scopes'])}")
else:
    err = body["error"]
    print(f"HTTP {r.status_code} {err['code']}: {err['message']}", file=sys.stderr)
    print(f"  request_id: {err['request_id']}", file=sys.stderr)
    print(f"  fix: {err['doc_url']}", file=sys.stderr)
    sys.exit(1)
