Skip to content

Commit

Permalink
Added asynchronous methods to supabase client
Browse files Browse the repository at this point in the history
  • Loading branch information
aschung01 committed Sep 14, 2023
1 parent d2b721f commit c04915e
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions supabase/client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import re
from typing import Any, Dict, Union
from typing import Any, Dict, Union, Coroutine

from httpx import Timeout
from postgrest import SyncFilterRequestBuilder, SyncPostgrestClient, SyncRequestBuilder
from postgrest import (
SyncFilterRequestBuilder,
SyncPostgrestClient,
SyncRequestBuilder,
AsyncFilterRequestBuilder,
AsyncPostgrestClient,
AsyncRequestBuilder,
)
from postgrest.constants import DEFAULT_POSTGREST_CLIENT_TIMEOUT
from storage3.constants import DEFAULT_TIMEOUT as DEFAULT_STORAGE_CLIENT_TIMEOUT
from supafunc import FunctionsClient
Expand Down Expand Up @@ -84,6 +91,13 @@ def __init__(
schema=options.schema,
timeout=options.postgrest_client_timeout,
)
self.apostgrest = self._init_apostgrest_client(
rest_url=self.rest_url,
supabase_key=self.supabase_key,
headers=options.headers,
schema=options.schema,
timeout=options.postgrest_client_timeout,
)
self.storage = self._init_storage_client(
self.storage_url, self._get_auth_headers(), options.storage_client_timeout
)
Expand All @@ -100,13 +114,27 @@ def table(self, table_name: str) -> SyncRequestBuilder:
"""
return self.from_(table_name)

def atable(self, table_name: str) -> AsyncRequestBuilder:
"""Perform a table operation asynchronously.
Alternatively you can use the `.afrom_()` method.
"""
return self.afrom_(table_name)

def from_(self, table_name: str) -> SyncRequestBuilder:
"""Perform a table operation.
See the `table` method.
"""
return self.postgrest.from_(table_name)

def afrom_(self, table_name: str) -> AsyncRequestBuilder:
"""Perform a table operation asynchronously.
See the `atable` method.
"""
return self.apostgrest.from_(table_name)

def rpc(self, fn: str, params: Dict[Any, Any]) -> SyncFilterRequestBuilder:
"""Performs a stored procedure call.
Expand All @@ -125,6 +153,26 @@ def rpc(self, fn: str, params: Dict[Any, Any]) -> SyncFilterRequestBuilder:
"""
return self.postgrest.rpc(fn, params)

def arpc(
self, fn: str, params: Dict[Any, Any]
) -> Coroutine[Any, Any, AsyncFilterRequestBuilder]:
"""Performs a stored procedure call asynchronously.
Parameters
----------
fn : callable
The stored procedure call to be executed.
params : dict of any
Parameters passed into the stored procedure call.
Returns
-------
AsyncFilterRequestBuilder
Returns an async filter builder. This lets you apply filters on the response
of an RPC.
"""
return self.apostgrest.rpc(fn, params)

# async def remove_subscription_helper(resolve):
# try:
# await self._close_subscription(subscription)
Expand Down Expand Up @@ -205,6 +253,21 @@ def _get_auth_headers(self) -> Dict[str, str]:
"Authorization": f"Bearer {self.supabase_key}",
}

@staticmethod
def _init_apostgrest_client(
rest_url: str,
supabase_key: str,
headers: Dict[str, str],
schema: str,
timeout: Union[int, float, Timeout] = DEFAULT_POSTGREST_CLIENT_TIMEOUT,
) -> AsyncPostgrestClient:
"""Private helper for creating an instance of the asynchronous Postgrest client."""
client = AsyncPostgrestClient(
rest_url, headers=headers, schema=schema, timeout=timeout
)
client.auth(token=supabase_key)
return client


def create_client(
supabase_url: str,
Expand Down

0 comments on commit c04915e

Please sign in to comment.