Skip to content

Commit fd612a0

Browse files
author
Andrew Smith
authored
feat: add create method to handle token headers (#630)
2 parents b9240d8 + 4f47306 commit fd612a0

File tree

5 files changed

+72
-35
lines changed

5 files changed

+72
-35
lines changed

Diff for: poetry.lock

+11-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: supabase/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from .__version__ import __version__
66
from ._sync.auth_client import SyncSupabaseAuthClient as SupabaseAuthClient
7-
from ._sync.client import Client
7+
from ._sync.client import SyncClient as Client
88
from ._sync.client import SyncStorageClient as SupabaseStorageClient
99
from ._sync.client import create_client
1010
from .lib.realtime_client import SupabaseRealtimeClient

Diff for: supabase/_async/client.py

+30-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
22
from typing import Any, Dict, Union
33

4-
from gotrue.types import AuthChangeEvent
4+
from gotrue.types import AuthChangeEvent, Session
55
from httpx import Timeout
66
from postgrest import (
77
AsyncFilterRequestBuilder,
@@ -24,7 +24,7 @@ def __init__(self, message: str):
2424
super().__init__(self.message)
2525

2626

27-
class Client:
27+
class AsyncClient:
2828
"""Supabase client class."""
2929

3030
def __init__(
@@ -63,6 +63,9 @@ def __init__(
6363

6464
self.supabase_url = supabase_url
6565
self.supabase_key = supabase_key
66+
self._auth_token = {
67+
"Authorization": f"Bearer {supabase_key}",
68+
}
6669
options.headers.update(self._get_auth_headers())
6770
self.options = options
6871
self.rest_url = f"{supabase_url}/rest/v1"
@@ -88,6 +91,17 @@ def __init__(
8891
self._functions = None
8992
self.auth.on_auth_state_change(self._listen_to_auth_events)
9093

94+
@classmethod
95+
async def create(
96+
cls,
97+
supabase_url: str,
98+
supabase_key: str,
99+
options: ClientOptions = ClientOptions(),
100+
):
101+
client = cls(supabase_url, supabase_key, options)
102+
client._auth_token = await client._get_token_header()
103+
return client
104+
91105
def table(self, table_name: str) -> AsyncRequestBuilder:
92106
"""Perform a table operation.
93107
@@ -125,20 +139,21 @@ def rpc(self, fn: str, params: Dict[Any, Any]) -> AsyncFilterRequestBuilder:
125139
@property
126140
def postgrest(self):
127141
if self._postgrest is None:
128-
self.options.headers.update(self._get_token_header())
142+
self.options.headers.update(self._auth_token)
129143
self._postgrest = self._init_postgrest_client(
130144
rest_url=self.rest_url,
131145
headers=self.options.headers,
132146
schema=self.options.schema,
133147
timeout=self.options.postgrest_client_timeout,
134148
)
149+
135150
return self._postgrest
136151

137152
@property
138153
def storage(self):
139154
if self._storage is None:
140155
headers = self._get_auth_headers()
141-
headers.update(self._get_token_header())
156+
headers.update(self._auth_token)
142157
self._storage = self._init_storage_client(
143158
storage_url=self.storage_url,
144159
headers=headers,
@@ -150,7 +165,7 @@ def storage(self):
150165
def functions(self):
151166
if self._functions is None:
152167
headers = self._get_auth_headers()
153-
headers.update(self._get_token_header())
168+
headers.update(self._auth_token)
154169
self._functions = AsyncFunctionsClient(self.functions_url, headers)
155170
return self._functions
156171

@@ -231,29 +246,30 @@ def _get_auth_headers(self) -> Dict[str, str]:
231246
"Authorization": f"Bearer {self.supabase_key}",
232247
}
233248

234-
def _get_token_header(self):
249+
async def _get_token_header(self):
235250
try:
236-
access_token = self.auth.get_session().access_token
237-
except:
251+
session = await self.auth.get_session()
252+
access_token = session.access_token
253+
except Exception as err:
238254
access_token = self.supabase_key
239255

240256
return {
241257
"Authorization": f"Bearer {access_token}",
242258
}
243259

244-
def _listen_to_auth_events(self, event: AuthChangeEvent, session):
260+
def _listen_to_auth_events(self, event: AuthChangeEvent, session: Session):
245261
if event in ["SIGNED_IN", "TOKEN_REFRESHED", "SIGNED_OUT"]:
246262
# reset postgrest and storage instance on event change
247263
self._postgrest = None
248264
self._storage = None
249265
self._functions = None
250266

251267

252-
def create_client(
268+
async def create_client(
253269
supabase_url: str,
254270
supabase_key: str,
255271
options: ClientOptions = ClientOptions(),
256-
) -> Client:
272+
) -> AsyncClient:
257273
"""Create client function to instantiate supabase client like JS runtime.
258274
259275
Parameters
@@ -280,4 +296,6 @@ def create_client(
280296
-------
281297
Client
282298
"""
283-
return Client(supabase_url=supabase_url, supabase_key=supabase_key, options=options)
299+
return await AsyncClient.create(
300+
supabase_url=supabase_url, supabase_key=supabase_key, options=options
301+
)

Diff for: supabase/_sync/client.py

+28-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
22
from typing import Any, Dict, Union
33

4-
from gotrue.types import AuthChangeEvent
4+
from gotrue.types import AuthChangeEvent, Session
55
from httpx import Timeout
66
from postgrest import SyncFilterRequestBuilder, SyncPostgrestClient, SyncRequestBuilder
77
from postgrest.constants import DEFAULT_POSTGREST_CLIENT_TIMEOUT
@@ -20,7 +20,7 @@ def __init__(self, message: str):
2020
super().__init__(self.message)
2121

2222

23-
class Client:
23+
class SyncClient:
2424
"""Supabase client class."""
2525

2626
def __init__(
@@ -59,6 +59,9 @@ def __init__(
5959

6060
self.supabase_url = supabase_url
6161
self.supabase_key = supabase_key
62+
self._auth_token = {
63+
"Authorization": f"Bearer {supabase_key}",
64+
}
6265
options.headers.update(self._get_auth_headers())
6366
self.options = options
6467
self.rest_url = f"{supabase_url}/rest/v1"
@@ -84,6 +87,17 @@ def __init__(
8487
self._functions = None
8588
self.auth.on_auth_state_change(self._listen_to_auth_events)
8689

90+
@classmethod
91+
def create(
92+
cls,
93+
supabase_url: str,
94+
supabase_key: str,
95+
options: ClientOptions = ClientOptions(),
96+
):
97+
client = cls(supabase_url, supabase_key, options)
98+
client._auth_token = client._get_token_header()
99+
return client
100+
87101
def table(self, table_name: str) -> SyncRequestBuilder:
88102
"""Perform a table operation.
89103
@@ -121,20 +135,21 @@ def rpc(self, fn: str, params: Dict[Any, Any]) -> SyncFilterRequestBuilder:
121135
@property
122136
def postgrest(self):
123137
if self._postgrest is None:
124-
self.options.headers.update(self._get_token_header())
138+
self.options.headers.update(self._auth_token)
125139
self._postgrest = self._init_postgrest_client(
126140
rest_url=self.rest_url,
127141
headers=self.options.headers,
128142
schema=self.options.schema,
129143
timeout=self.options.postgrest_client_timeout,
130144
)
145+
131146
return self._postgrest
132147

133148
@property
134149
def storage(self):
135150
if self._storage is None:
136151
headers = self._get_auth_headers()
137-
headers.update(self._get_token_header())
152+
headers.update(self._auth_token)
138153
self._storage = self._init_storage_client(
139154
storage_url=self.storage_url,
140155
headers=headers,
@@ -146,7 +161,7 @@ def storage(self):
146161
def functions(self):
147162
if self._functions is None:
148163
headers = self._get_auth_headers()
149-
headers.update(self._get_token_header())
164+
headers.update(self._auth_token)
150165
self._functions = SyncFunctionsClient(self.functions_url, headers)
151166
return self._functions
152167

@@ -229,15 +244,16 @@ def _get_auth_headers(self) -> Dict[str, str]:
229244

230245
def _get_token_header(self):
231246
try:
232-
access_token = self.auth.get_session().access_token
233-
except:
247+
session = self.auth.get_session()
248+
access_token = session.access_token
249+
except Exception as err:
234250
access_token = self.supabase_key
235251

236252
return {
237253
"Authorization": f"Bearer {access_token}",
238254
}
239255

240-
def _listen_to_auth_events(self, event: AuthChangeEvent, session):
256+
def _listen_to_auth_events(self, event: AuthChangeEvent, session: Session):
241257
if event in ["SIGNED_IN", "TOKEN_REFRESHED", "SIGNED_OUT"]:
242258
# reset postgrest and storage instance on event change
243259
self._postgrest = None
@@ -249,7 +265,7 @@ def create_client(
249265
supabase_url: str,
250266
supabase_key: str,
251267
options: ClientOptions = ClientOptions(),
252-
) -> Client:
268+
) -> SyncClient:
253269
"""Create client function to instantiate supabase client like JS runtime.
254270
255271
Parameters
@@ -276,4 +292,6 @@ def create_client(
276292
-------
277293
Client
278294
"""
279-
return Client(supabase_url=supabase_url, supabase_key=supabase_key, options=options)
295+
return SyncClient.create(
296+
supabase_url=supabase_url, supabase_key=supabase_key, options=options
297+
)

Diff for: supabase/client.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
from .__version__ import __version__
66
from ._sync.auth_client import SyncSupabaseAuthClient as SupabaseAuthClient
7-
from ._sync.client import Client, ClientOptions
7+
from ._sync.client import ClientOptions
8+
from ._sync.client import SyncClient as Client
89
from ._sync.client import SyncStorageClient as SupabaseStorageClient
910
from ._sync.client import create_client
1011
from .lib.realtime_client import SupabaseRealtimeClient

0 commit comments

Comments
 (0)