1
1
import gotrue
2
2
3
3
from postgrest_py import PostgrestClient
4
- from .lib .supabase_auth_client import SupabaseAuthClient
5
- from .lib .supabase_realtime_client import SupabaseRealtimeClient
6
- from .lib .supabase_query_builder import SupabaseQueryBuilder
7
- from typing import Optional
4
+ from lib .auth_client import SupabaseAuthClient
5
+ from lib .realtime_client import SupabaseRealtimeClient
6
+ from lib .query_builder import SupabaseQueryBuilder
7
+
8
+ from typing import Any , Dict
8
9
9
10
10
11
DEFAULT_OPTIONS = {
18
19
19
20
class Client :
20
21
def __init__ (
21
- self , supabaseUrl : str , supabaseKey : str , options : Optional [ dict ] = {}
22
+ self , supabase_url : str , supabase_key : str , ** options ,
22
23
):
23
- """
24
- Initialize a Supabase Client
24
+ """Instanciate the client.
25
+
25
26
Parameters
26
27
----------
27
- SupabaseUrl
28
- URL of the Supabase instance that we are acting on
29
- SupabaseKey
30
- API key for the Supabase instance that we are acting on
31
- Options
32
- Any other settings that we wish to override
33
-
34
- Returns
35
- None
36
- -------
37
- """
38
- if not supabaseUrl :
39
- raise Exception ("supabaseUrl is required" )
40
- if not supabaseKey :
41
- raise Exception ("supabaseKey is required" )
42
-
43
- settings = {** DEFAULT_OPTIONS , ** options }
44
- self .restUrl = f"{ supabaseUrl } /rest/v1"
45
- self .realtimeUrl = f"{ supabaseUrl } /realtime/v1" .replace ("http" , "ws" )
46
- self .authUrl = f"{ supabaseUrl } /auth/v1"
28
+ supabase_url: str
29
+ The URL to the Supabase instance that should be connected to.
30
+ supabase_key: str
31
+ The API key to the Supabase instance that should be connected to.
32
+ **options
33
+ Any extra settings to be optionally specified - also see the
34
+ `DEFAULT_OPTIONS` dict.
35
+ """
36
+ if not supabase_url :
37
+ raise Exception ("supabase_url is required" )
38
+ if not supabase_key :
39
+ raise Exception ("supabase_key is required" )
40
+ settings : Dict [str , Any ] = {** DEFAULT_OPTIONS , ** options }
41
+ self .restUrl = f"{ supabase_url } /rest/v1"
42
+ self .realtimeUrl = f"{ supabase_url } /realtime/v1" .replace ("http" , "ws" )
43
+ self .authUrl = f"{ supabase_url } /auth/v1"
47
44
self .schema = settings ["schema" ]
48
- self .supabaseUrl = supabaseUrl
49
- self .supabaseKey = supabaseKey
50
- self .auth = self ._initSupabaseAuthClient (* settings )
51
- # TODO: Fix this once Realtime-py is working
52
- # self.realtime = self._initRealtimeClient()
45
+ self .supabaseUrl = supabase_url
46
+ self .supabaseKey = supabase_key
47
+ self .auth = self ._init_supabase_auth_client (* settings )
48
+ self .realtime = self ._init_realtime_client ()
53
49
54
50
def _from (self , table : str ):
55
- """
56
- Perform a table operation on a given table
57
- Parameters
58
- ----------
59
- table
60
- Name of table to execute operations on
61
- Returns
62
- -------
63
- SupabaseQueryBuilder
64
- Wrapper for Postgrest-py client which we can perform operations(e.g. select/update) with
65
- """
66
- url = f"{ self .restUrl } /{ table } "
51
+ """Perform a table operation."""
52
+ url = f"{ self .rest_url } /{ table } "
67
53
return SupabaseQueryBuilder (
68
54
url ,
69
55
{
70
- "headers" : self ._getAuthHeaders (),
56
+ "headers" : self ._get_auth_headers (),
71
57
"schema" : self .schema ,
72
58
"realtime" : self .realtime ,
73
59
},
@@ -95,22 +81,19 @@ def rpc(self, fn, params):
95
81
rest = self ._initPostgrestClient ()
96
82
return rest .rpc (fn , params )
97
83
98
- # TODO: Fix this segment after realtime-py is working
99
- # def removeSubscription(self, subscription):
100
84
# async def remove_subscription_helper(resolve):
101
85
# try:
102
- # await self._closeSubscription (subscription)
103
- # openSubscriptions = len(self.getSubscriptions ())
104
- # if not openSubscriptions :
86
+ # await self._close_subscription (subscription)
87
+ # open_subscriptions = len(self.get_subscriptions ())
88
+ # if not open_subscriptions :
105
89
# error = await self.realtime.disconnect()
106
90
# if error:
107
- # return {"error": None, "data": { openSubscriptions}}
108
- # except Error as e:
109
- # return {error}
110
-
91
+ # return {"error": None, "data": { open_subscriptions}}
92
+ # except Exception as e:
93
+ # raise e
111
94
# return remove_subscription_helper(subscription)
112
95
113
- async def _closeSubscription (self , subscription ):
96
+ async def _close_subscription (self , subscription ):
114
97
"""
115
98
Close a given subscription
116
99
@@ -122,19 +105,19 @@ async def _closeSubscription(self, subscription):
122
105
if not subscription .closed :
123
106
await self ._closeChannel (subscription )
124
107
125
- def getSubscriptions (self ):
108
+ def get_subscriptions (self ):
126
109
"""
127
110
Return all channels the the client is subscribed to.
128
111
"""
129
112
return self .realtime .channels
130
113
131
- def _initRealtimeClient (self ):
114
+ def _init_realtime_client (self ):
132
115
"""
133
116
Private method for creating an instance of the realtime-py client.
134
117
"""
135
118
return RealtimeClient (self .realtimeUrl , {"params" : {apikey : self .supabaseKey }})
136
119
137
- def _initSupabaseAuthClient (
120
+ def _init_supabase_auth_client (
138
121
self ,
139
122
schema ,
140
123
autoRefreshToken ,
@@ -157,13 +140,13 @@ def _initSupabaseAuthClient(
157
140
},
158
141
)
159
142
160
- def _initPostgrestClient (self ):
143
+ def _init_postgrest_client (self ):
161
144
"""
162
145
Private helper method for creating a wrapped instance of the Postgrest client.
163
146
"""
164
147
return PostgrestClient (self .restUrl )
165
148
166
- def _getAuthHeaders (self ):
149
+ def _get_auth_headers (self ):
167
150
"""
168
151
Helper method to get auth headers
169
152
"""
@@ -172,12 +155,3 @@ def _getAuthHeaders(self):
172
155
headers ["apiKey" ] = self .supabaseKey
173
156
headers ["Authorization" ] = f"Bearer { self .supabaseKey } "
174
157
return headers
175
-
176
- # TODO: Fix this segment after realtime-py is working
177
- # def closeSubscription(self):
178
- # if not subscription.closed:
179
- # await self._closeChannel(subscription)
180
-
181
- # def _closeChannel(self, subscription):
182
- # async def _closeChannelHelper():
183
- # subscription.unsubscribe().on('OK')
0 commit comments