Skip to content

Commit 57511be

Browse files
authored
Merge pull request #43 from lqmanh/features/add-default-headers
Add some default headers to wrapped client libs
2 parents ec494dc + 4f64827 commit 57511be

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

pyproject.toml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@ license = "MIT"
77

88
[tool.poetry.dependencies]
99
python = "^3.7.1"
10-
postgrest-py = "0.5.0"
10+
postgrest-py = "^0.5.0"
1111
realtime-py = "^0.1.2"
1212
gotrue = "0.2.0"
1313
pytest = "^6"
1414
requests = "2.25.1"
1515

1616
[tool.poetry.dev-dependencies]
1717
pre_commit = "^2.1.0"
18+
black = "^21.7b0"
1819

1920
[build-system]
20-
requires = [
21-
"poetry>=0.12",
22-
"setuptools>=30.3.0,<50",
23-
]
21+
requires = ["poetry>=0.12", "setuptools>=30.3.0,<50"]
2422
build-backend = "poetry.masonry.api"

supabase_py/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
__version__ = "0.0.2"
2+
13
from supabase_py import client, lib
24
from supabase_py.client import Client, create_client
35

4-
__version__ = "0.0.2"
5-
66
__all__ = ["client", "lib", "Client", "create_client"]

supabase_py/client.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from postgrest_py import PostgrestClient
44

55
from supabase_py.lib.auth_client import SupabaseAuthClient
6+
from supabase_py.lib.constants import DEFAULT_HEADERS
67
from supabase_py.lib.query_builder import SupabaseQueryBuilder
78
from supabase_py.lib.realtime_client import SupabaseRealtimeClient
89
from supabase_py.lib.storage_client import SupabaseStorageClient
@@ -13,6 +14,7 @@
1314
"persist_session": True,
1415
"detect_session_in_url": True,
1516
"local_storage": {},
17+
"headers": DEFAULT_HEADERS,
1618
}
1719

1820

@@ -44,17 +46,15 @@ def __init__(
4446
raise Exception("supabase_key is required")
4547
self.supabase_url = supabase_url
4648
self.supabase_key = supabase_key
47-
# Start with defaults, write headers and prioritise user overwrites.
48-
settings: Dict[str, Any] = {
49-
**DEFAULT_OPTIONS,
50-
"headers": self._get_auth_headers(),
51-
**options,
52-
}
49+
50+
settings = {**DEFAULT_OPTIONS, **options}
51+
settings["headers"].update(self._get_auth_headers())
5352
self.rest_url: str = f"{supabase_url}/rest/v1"
5453
self.realtime_url: str = f"{supabase_url}/realtime/v1".replace("http", "ws")
5554
self.auth_url: str = f"{supabase_url}/auth/v1"
5655
self.storage_url = f"{supabase_url}/storage/v1"
5756
self.schema: str = settings.pop("schema")
57+
5858
# Instantiate clients.
5959
self.auth: SupabaseAuthClient = self._init_supabase_auth_client(
6060
auth_url=self.auth_url,
@@ -69,7 +69,8 @@ def __init__(
6969
self.realtime = None
7070
self.postgrest: PostgrestClient = self._init_postgrest_client(
7171
rest_url=self.rest_url,
72-
supabase_key=supabase_key,
72+
supabase_key=self.supabase_key,
73+
**settings,
7374
)
7475

7576
def storage(self):
@@ -174,9 +175,14 @@ def _init_supabase_auth_client(
174175
)
175176

176177
@staticmethod
177-
def _init_postgrest_client(rest_url: str, supabase_key: str) -> PostgrestClient:
178+
def _init_postgrest_client(
179+
rest_url: str,
180+
supabase_key: str,
181+
headers: Dict[str, str],
182+
**kwargs, # other unused settings
183+
) -> PostgrestClient:
178184
"""Private helper for creating an instance of the Postgrest client."""
179-
client = PostgrestClient(rest_url)
185+
client = PostgrestClient(rest_url, headers=headers)
180186
client.auth(token=supabase_key)
181187
return client
182188

supabase_py/lib/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from supabase_py import __version__
2+
3+
DEFAULT_HEADERS = {"X-Client-Info": f"supabase-py/{__version__}"}

0 commit comments

Comments
 (0)