Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
lqmanh committed Aug 27, 2021
1 parent 528abb3 commit 04bf6ef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
23 changes: 14 additions & 9 deletions supabase_py/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from postgrest_py import PostgrestClient

from supabase_py.lib.auth_client import SupabaseAuthClient
from supabase_py.lib.constants import DEFAULT_HEADERS
from supabase_py.lib.query_builder import SupabaseQueryBuilder
from supabase_py.lib.realtime_client import SupabaseRealtimeClient
from supabase_py.lib.storage_client import SupabaseStorageClient
Expand All @@ -13,6 +14,7 @@
"persist_session": True,
"detect_session_in_url": True,
"local_storage": {},
"headers": DEFAULT_HEADERS,
}


Expand Down Expand Up @@ -44,17 +46,15 @@ def __init__(
raise Exception("supabase_key is required")
self.supabase_url = supabase_url
self.supabase_key = supabase_key
# Start with defaults, write headers and prioritise user overwrites.
settings: Dict[str, Any] = {
**DEFAULT_OPTIONS,
"headers": self._get_auth_headers(),
**options,
}

settings = {**DEFAULT_OPTIONS, **options}
settings["headers"].update(self._get_auth_headers())
self.rest_url: str = f"{supabase_url}/rest/v1"
self.realtime_url: str = f"{supabase_url}/realtime/v1".replace("http", "ws")
self.auth_url: str = f"{supabase_url}/auth/v1"
self.storage_url = f"{supabase_url}/storage/v1"
self.schema: str = settings.pop("schema")

# Instantiate clients.
self.auth: SupabaseAuthClient = self._init_supabase_auth_client(
auth_url=self.auth_url,
Expand All @@ -69,7 +69,8 @@ def __init__(
self.realtime = None
self.postgrest: PostgrestClient = self._init_postgrest_client(
rest_url=self.rest_url,
supabase_key=supabase_key,
supabase_key=self.supabase_key,
**settings,
)

def storage(self):
Expand Down Expand Up @@ -174,9 +175,13 @@ def _init_supabase_auth_client(
)

@staticmethod
def _init_postgrest_client(rest_url: str, supabase_key: str) -> PostgrestClient:
def _init_postgrest_client(
rest_url: str,
supabase_key: str,
headers: Dict[str, str],
) -> PostgrestClient:
"""Private helper for creating an instance of the Postgrest client."""
client = PostgrestClient(rest_url)
client = PostgrestClient(rest_url, headers=headers)
client.auth(token=supabase_key)
return client

Expand Down
5 changes: 5 additions & 0 deletions supabase_py/lib/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from supabase_py import __version__

DEFAULT_HEADERS = {
'X-Client-Info': f'supabase-py/{__version__}'
}

0 comments on commit 04bf6ef

Please sign in to comment.