Skip to content

Commit

Permalink
Merge pull request #43 from lqmanh/features/add-default-headers
Browse files Browse the repository at this point in the history
Add some default headers to wrapped client libs
  • Loading branch information
J0 authored Sep 29, 2021
2 parents ec494dc + 4f64827 commit 57511be
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
8 changes: 3 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ license = "MIT"

[tool.poetry.dependencies]
python = "^3.7.1"
postgrest-py = "0.5.0"
postgrest-py = "^0.5.0"
realtime-py = "^0.1.2"
gotrue = "0.2.0"
pytest = "^6"
requests = "2.25.1"

[tool.poetry.dev-dependencies]
pre_commit = "^2.1.0"
black = "^21.7b0"

[build-system]
requires = [
"poetry>=0.12",
"setuptools>=30.3.0,<50",
]
requires = ["poetry>=0.12", "setuptools>=30.3.0,<50"]
build-backend = "poetry.masonry.api"
4 changes: 2 additions & 2 deletions supabase_py/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__version__ = "0.0.2"

from supabase_py import client, lib
from supabase_py.client import Client, create_client

__version__ = "0.0.2"

__all__ = ["client", "lib", "Client", "create_client"]
24 changes: 15 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,14 @@ 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],
**kwargs, # other unused settings
) -> 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
3 changes: 3 additions & 0 deletions supabase_py/lib/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from supabase_py import __version__

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

0 comments on commit 57511be

Please sign in to comment.