File size: 1,219 Bytes
6e02ff7
86c1ac3
45a0fac
6e02ff7
 
 
 
 
 
 
86c1ac3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e02ff7
45a0fac
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
import httpx
from supabase import create_client, Client
from dotenv import load_dotenv

load_dotenv()

SUPABASE_URL: str = os.environ["SUPABASE_URL"]
SUPABASE_SERVICE_KEY: str = os.environ["SUPABASE_SERVICE_KEY"]

def supabase_rest_call(method: str, endpoint: str, json_data: dict = None) -> list | dict:
    """
    Directly call Supabase REST API using httpx.
    Bypasses the buggy supabase-py client entirely.
    """
    url = f"{SUPABASE_URL}/rest/v1/{endpoint}"
    headers = {
        "apikey": SUPABASE_SERVICE_KEY,
        "Authorization": f"Bearer {SUPABASE_SERVICE_KEY}",
        "Content-Type": "application/json",
        "Prefer": "return=representation"
    }
    
    # Disable proxies explicitly to avoid HF Space proxy issues
    with httpx.Client(proxies=None) as client:
        response = client.request(method, url, headers=headers, json=json_data)
        response.raise_for_status()
        return response.json()

_client: Client | None = None

def get_supabase_client() -> Client:
    """Return a cached Supabase client using the service role key."""
    global _client
    if _client is None:
        _client = create_client(SUPABASE_URL, SUPABASE_SERVICE_KEY)
    return _client