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 all commits
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
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__}"}