Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some default headers to wrapped client libs #43

Merged
merged 7 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update
  • Loading branch information
lqmanh committed Aug 27, 2021
commit 04bf6ef1c854683b6ae1eb9b56b6273e147b2ed3
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__}'
}