1
- from typing import Any , Dict
1
+ from typing import Any , Coroutine , Dict
2
2
3
+ from httpx import Response
3
4
from postgrest_py import PostgrestClient
5
+ from postgrest_py .request_builder import RequestBuilder
4
6
5
7
from supabase .lib .auth_client import SupabaseAuthClient
6
- from supabase .lib .constants import DEFAULT_HEADERS
8
+ from supabase .lib .client_options import ClientOptions
9
+ from supabase .lib .constants import DEFAULT_OPTIONS
7
10
from supabase .lib .query_builder import SupabaseQueryBuilder
8
11
from supabase .lib .realtime_client import SupabaseRealtimeClient
9
12
from supabase .lib .storage_client import SupabaseStorageClient
10
13
11
- DEFAULT_OPTIONS = {
12
- "schema" : "public" ,
13
- "auto_refresh_token" : True ,
14
- "persist_session" : True ,
15
- "detect_session_in_url" : True ,
16
- "local_storage" : {},
17
- "headers" : DEFAULT_HEADERS ,
18
- }
19
-
20
14
21
15
class Client :
22
16
"""Supabase client class."""
@@ -47,19 +41,19 @@ def __init__(
47
41
self .supabase_url = supabase_url
48
42
self .supabase_key = supabase_key
49
43
50
- settings = { ** DEFAULT_OPTIONS , ** options }
51
- settings [ " headers" ] .update (self ._get_auth_headers ())
44
+ settings = DEFAULT_OPTIONS . replace ( ** options )
45
+ settings . headers .update (self ._get_auth_headers ())
52
46
self .rest_url : str = f"{ supabase_url } /rest/v1"
53
47
self .realtime_url : str = f"{ supabase_url } /realtime/v1" .replace ("http" , "ws" )
54
48
self .auth_url : str = f"{ supabase_url } /auth/v1"
55
49
self .storage_url = f"{ supabase_url } /storage/v1"
56
- self .schema : str = settings .pop ( " schema" )
50
+ self .schema : str = settings .schema
57
51
58
52
# Instantiate clients.
59
53
self .auth : SupabaseAuthClient = self ._init_supabase_auth_client (
60
54
auth_url = self .auth_url ,
61
55
supabase_key = self .supabase_key ,
62
- ** settings ,
56
+ client_options = settings ,
63
57
)
64
58
# TODO(fedden): Bring up to parity with JS client.
65
59
# self.realtime: SupabaseRealtimeClient = self._init_realtime_client(
@@ -70,14 +64,14 @@ def __init__(
70
64
self .postgrest : PostgrestClient = self ._init_postgrest_client (
71
65
rest_url = self .rest_url ,
72
66
supabase_key = self .supabase_key ,
73
- ** settings ,
67
+ headers = settings . headers ,
74
68
)
75
69
76
- def storage (self ):
70
+ def storage (self ) -> SupabaseStorageClient :
77
71
"""Create instance of the storage client"""
78
72
return SupabaseStorageClient (self .storage_url , self ._get_auth_headers ())
79
73
80
- def table (self , table_name : str ):
74
+ def table (self , table_name : str ) -> RequestBuilder :
81
75
"""Perform a table operation.
82
76
83
77
Note that the supabase client uses the `from` method, but in Python,
@@ -86,7 +80,7 @@ def table(self, table_name: str):
86
80
"""
87
81
return self .from_ (table_name )
88
82
89
- def from_ (self , table_name : str ):
83
+ def from_ (self , table_name : str ) -> RequestBuilder :
90
84
"""Perform a table operation.
91
85
92
86
See the `table` method.
@@ -100,7 +94,7 @@ def from_(self, table_name: str):
100
94
)
101
95
return query_builder .from_ (table_name )
102
96
103
- def rpc (self , fn , params ) :
97
+ def rpc (self , fn : str , params : Dict [ Any , Any ]) -> Coroutine [ Any , Any , Response ] :
104
98
"""Performs a stored procedure call.
105
99
106
100
Parameters
@@ -158,28 +152,23 @@ def _init_realtime_client(
158
152
def _init_supabase_auth_client (
159
153
auth_url : str ,
160
154
supabase_key : str ,
161
- detect_session_in_url : bool ,
162
- auto_refresh_token : bool ,
163
- persist_session : bool ,
164
- local_storage : Dict [str , Any ],
165
- headers : Dict [str , str ],
155
+ client_options : ClientOptions ,
166
156
) -> SupabaseAuthClient :
167
157
"""Creates a wrapped instance of the GoTrue Client."""
168
158
return SupabaseAuthClient (
169
159
url = auth_url ,
170
- auto_refresh_token = auto_refresh_token ,
171
- detect_session_in_url = detect_session_in_url ,
172
- persist_session = persist_session ,
173
- local_storage = local_storage ,
174
- headers = headers ,
160
+ auto_refresh_token = client_options . auto_refresh_token ,
161
+ detect_session_in_url = client_options . detect_session_in_url ,
162
+ persist_session = client_options . persist_session ,
163
+ local_storage = client_options . local_storage ,
164
+ headers = client_options . headers ,
175
165
)
176
166
177
167
@staticmethod
178
168
def _init_postgrest_client (
179
169
rest_url : str ,
180
170
supabase_key : str ,
181
171
headers : Dict [str , str ],
182
- ** kwargs , # other unused settings
183
172
) -> PostgrestClient :
184
173
"""Private helper for creating an instance of the Postgrest client."""
185
174
client = PostgrestClient (rest_url , headers = headers )
@@ -189,11 +178,10 @@ def _init_postgrest_client(
189
178
def _get_auth_headers (self ) -> Dict [str , str ]:
190
179
"""Helper method to get auth headers."""
191
180
# What's the corresponding method to get the token
192
- headers : Dict [ str , str ] = {
181
+ return {
193
182
"apiKey" : self .supabase_key ,
194
183
"Authorization" : f"Bearer { self .supabase_key } " ,
195
184
}
196
- return headers
197
185
198
186
199
187
def create_client (supabase_url : str , supabase_key : str , ** options ) -> Client :
0 commit comments