|
1 | 1 | import re
|
2 |
| -from typing import Any, Dict, Optional, Union |
| 2 | +from typing import Any, Dict, List, Optional, Union |
3 | 3 |
|
4 | 4 | from gotrue import AsyncMemoryStorage
|
5 | 5 | from gotrue.types import AuthChangeEvent, Session
|
|
10 | 10 | AsyncRPCFilterRequestBuilder,
|
11 | 11 | )
|
12 | 12 | from postgrest.constants import DEFAULT_POSTGREST_CLIENT_TIMEOUT
|
| 13 | +from realtime import AsyncRealtimeChannel, AsyncRealtimeClient, RealtimeChannelOptions |
13 | 14 | from storage3 import AsyncStorageClient
|
14 | 15 | from storage3.constants import DEFAULT_TIMEOUT as DEFAULT_STORAGE_CLIENT_TIMEOUT
|
15 | 16 | from supafunc import AsyncFunctionsClient
|
@@ -80,11 +81,11 @@ def __init__(
|
80 | 81 | auth_url=self.auth_url,
|
81 | 82 | client_options=options,
|
82 | 83 | )
|
83 |
| - # TODO: Bring up to parity with JS client. |
84 |
| - # self.realtime: SupabaseRealtimeClient = self._init_realtime_client( |
85 |
| - # realtime_url=self.realtime_url, |
86 |
| - # supabase_key=self.supabase_key, |
87 |
| - # ) |
| 84 | + self.realtime = self._init_realtime_client( |
| 85 | + realtime_url=self.realtime_url, |
| 86 | + supabase_key=self.supabase_key, |
| 87 | + options=options.realtime if options else None, |
| 88 | + ) |
88 | 89 | self.realtime = None
|
89 | 90 | self._postgrest = None
|
90 | 91 | self._storage = None
|
@@ -197,41 +198,33 @@ def functions(self):
|
197 | 198 | )
|
198 | 199 | return self._functions
|
199 | 200 |
|
200 |
| - # async def remove_subscription_helper(resolve): |
201 |
| - # try: |
202 |
| - # await self._close_subscription(subscription) |
203 |
| - # open_subscriptions = len(self.get_subscriptions()) |
204 |
| - # if not open_subscriptions: |
205 |
| - # error = await self.realtime.disconnect() |
206 |
| - # if error: |
207 |
| - # return {"error": None, "data": { open_subscriptions}} |
208 |
| - # except Exception as e: |
209 |
| - # raise e |
210 |
| - # return remove_subscription_helper(subscription) |
211 |
| - |
212 |
| - # async def _close_subscription(self, subscription): |
213 |
| - # """Close a given subscription |
214 |
| - |
215 |
| - # Parameters |
216 |
| - # ---------- |
217 |
| - # subscription |
218 |
| - # The name of the channel |
219 |
| - # """ |
220 |
| - # if not subscription.closed: |
221 |
| - # await self._closeChannel(subscription) |
222 |
| - |
223 |
| - # def get_subscriptions(self): |
224 |
| - # """Return all channels the client is subscribed to.""" |
225 |
| - # return self.realtime.channels |
226 |
| - |
227 |
| - # @staticmethod |
228 |
| - # def _init_realtime_client( |
229 |
| - # realtime_url: str, supabase_key: str |
230 |
| - # ) -> SupabaseRealtimeClient: |
231 |
| - # """Private method for creating an instance of the realtime-py client.""" |
232 |
| - # return SupabaseRealtimeClient( |
233 |
| - # realtime_url, {"params": {"apikey": supabase_key}} |
234 |
| - # ) |
| 201 | + def channel( |
| 202 | + self, topic: str, params: RealtimeChannelOptions = {} |
| 203 | + ) -> AsyncRealtimeChannel: |
| 204 | + """Creates a Realtime channel with Broadcast, Presence, and Postgres Changes.""" |
| 205 | + return self.realtime.channel(topic, params) |
| 206 | + |
| 207 | + def get_channels(self) -> List[AsyncRealtimeChannel]: |
| 208 | + """Returns all realtime channels.""" |
| 209 | + return self.realtime.get_channels() |
| 210 | + |
| 211 | + async def remove_channel(self, channel: AsyncRealtimeChannel) -> None: |
| 212 | + """Unsubscribes and removes Realtime channel from Realtime client.""" |
| 213 | + await self.realtime.remove_channel(channel) |
| 214 | + |
| 215 | + async def remove_all_channels(self) -> None: |
| 216 | + """Unsubscribes and removes all Realtime channels from Realtime client.""" |
| 217 | + await self.realtime.remove_all_channels() |
| 218 | + |
| 219 | + @staticmethod |
| 220 | + def _init_realtime_client( |
| 221 | + realtime_url: str, supabase_key: str, options: Optional[Dict[str, Any]] |
| 222 | + ) -> AsyncRealtimeClient: |
| 223 | + """Private method for creating an instance of the realtime-py client.""" |
| 224 | + return AsyncRealtimeClient( |
| 225 | + realtime_url, token=supabase_key, params=options or {} |
| 226 | + ) |
| 227 | + |
235 | 228 | @staticmethod
|
236 | 229 | def _init_storage_client(
|
237 | 230 | storage_url: str,
|
@@ -303,6 +296,9 @@ def _listen_to_auth_events(
|
303 | 296 |
|
304 | 297 | self.options.headers["Authorization"] = self._create_auth_header(access_token)
|
305 | 298 |
|
| 299 | + # set_auth is a coroutine, how to handle this? |
| 300 | + self.realtime.set_auth(access_token) |
| 301 | + |
306 | 302 |
|
307 | 303 | async def create_client(
|
308 | 304 | supabase_url: str,
|
|
0 commit comments