| 3 | |
| 4 | |
| 5 | class Supabase: |
| 6 | def __init__(self, supabase_url, supabase_key): |
| 7 | self.supabase_url = supabase_url |
| 8 | self.supabase_key = supabase_key |
| 9 | |
| 10 | def get_url(self): |
| 11 | return self.supabase_url |
| 12 | |
| 13 | async def get(self, table: str, columns: str, key_name: str, key_value): |
| 14 | logging.info(f"Retrieving {columns} for {key_name}: {key_value}") |
| 15 | |
| 16 | url = f"{self.supabase_url}/rest/v1/{table}?{key_name}=eq.{key_value}&select={columns}" |
| 17 | headers = { |
| 18 | "apiKey": self.supabase_key, |
| 19 | "Authorization": f"Bearer {self.supabase_key}", |
| 20 | } |
| 21 | |
| 22 | async with httpx.AsyncClient() as async_client: |
| 23 | response = await async_client.get(url, headers=headers) |
| 24 | |
| 25 | json = response.json() |
| 26 | if not response.is_success: |
| 27 | raise RuntimeError( |
| 28 | f"Error retrieving {columns=} from {table=} : {json['code']=}. {json['message']=}" |
| 29 | ) |
| 30 | |
| 31 | return json |
| 32 | |
| 33 | async def get_first(self, table: str, columns: str, key_name: str, key_value): |
| 34 | rows = await self.get(table, columns, key_name, key_value) |
| 35 | return rows[0] if rows else None |
| 36 | |
| 37 | async def delete(self, table: str, key_name: str, key_value): |
| 38 | logging.info(f"Deleting row with {key_name}: {key_value} from table {table}") |
| 39 | |
| 40 | url = f"{self.supabase_url}/rest/v1/{table}?{key_name}=eq.{key_value}" |
| 41 | headers = { |
| 42 | "apiKey": self.supabase_key, |
| 43 | "Authorization": f"Bearer {self.supabase_key}", |
| 44 | } |
| 45 | |
| 46 | async with httpx.AsyncClient() as async_client: |
| 47 | response = await async_client.delete(url, headers=headers) |
| 48 | |
| 49 | if not response.status_code == 204: |
| 50 | raise RuntimeError(f"Error deleting row from {table=} : {table}") |
| 51 | |
| 52 | return response.status_code == 204 |
| 53 | |
| 54 | async def insert(self, table: str, data): |
| 55 | logging.info(f"Inserting to {table} table: {data}") |
| 56 | table_url = f"{self.supabase_url}/rest/v1/{table}" |
| 57 | headers = { |
| 58 | "apiKey": self.supabase_key, |
| 59 | "Authorization": f"Bearer {self.supabase_key}", |
| 60 | } |
| 61 | |
| 62 | async with httpx.AsyncClient() as async_client: |
no outgoing calls
searching dependent graphs…