# Is this number on the do-not-call list? Python 3.9+ with requests.
import os
import sys

import requests

base = os.environ.get("RELATIONSAI_API_BASE", "https://voice-api.relationsai.com/v1")
phone = os.environ.get("PHONE") or sys.exit("Set PHONE to the number in E.164 format")

r = requests.get(
    f"{base}/dnc-entries",
    params={"phone_number": phone},
    headers={"Authorization": f"Bearer {os.environ['RELATIONSAI_API_KEY']}"},
    timeout=30,
)
page = r.json()
if not r.ok:
    err = page["error"]
    sys.exit(f"{err['code']}: {err['message']} (request {err['request_id']})")
if page["data"]:
    for entry in page["data"]:
        print(f"{entry['phone_number']} is on the do-not-call list ({entry['id']})")
else:
    print(f"{phone} is not on the do-not-call list")
