qecho / src /database.py
valent-p's picture
feat: Implement Supabase client initialization, including environment variable validation and a connectivity test.
ee09c9c
Raw History Blame Contribute Delete
1.43 kB
import os
from supabase import create_client, Client
import httpx
# Ensure these environment variables are set
url: str = os.environ.get("SUPABASE_URL", "").strip().strip("'").strip('"')
key: str = os.environ.get("SUPABASE_KEY", "").strip().strip("'").strip('"')
if not url or not key:
print("[-] CRITICAL: SUPABASE_URL or SUPABASE_KEY not set in environment.")
raise RuntimeError("Missing SUPABASE_URL or SUPABASE_KEY environment variables.")
if not url.startswith("https://"):
print(f"[-] CRITICAL: Invalid SUPABASE_URL format: {url}")
raise RuntimeError(f"SUPABASE_URL must start with 'https://'. Got: {url}")
print(f"[*] Raw URL repr: {repr(url)}") # Debug hidden characters
print(f"[*] Initializing Supabase Client with URL: {url[:15]}...{url[-5:]} (Masked)")
# --- CONNECTIVITY CHECK ---
try:
print(f"[*] Testing connectivity to {url}...")
# Supabase projects usually respond to /rest/v1/ or just base with 404 or 200
# We just want to check if DNS resolves.
with httpx.Client(timeout=5.0) as client:
resp = client.get(url)
print(f"[*] Connectivity Check Status: {resp.status_code}")
except Exception as e:
print(f"[-] CRITICAL: Connectivity Check Failed. DNS or Network Issue.")
print(f"[-] Error: {e}")
# We do not raise here to allow app to try anyway, but this log will be vital.
# --------------------------
supabase: Client = create_client(url, key)