From 5bd00941d29cc0c5ff968d38cbef94ec086e1f46 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Sat, 28 Sep 2024 19:49:33 +0000 Subject: [PATCH 1/2] fix(realtime): enable auto_reconnect option from supabase client --- poetry.lock | 2 +- pyproject.toml | 1 + supabase/_async/client.py | 8 ++++---- supabase/lib/client_options.py | 12 +++++++----- supabase/types.py | 8 ++++++++ 5 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 supabase/types.py diff --git a/poetry.lock b/poetry.lock index fc8997b1..f2a1ffd5 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1940,4 +1940,4 @@ test = ["big-O", "importlib-resources", "jaraco.functools", "jaraco.itertools", [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "d3030576412b09022f2cd4523645e09a823d2eb911bc711f9f5a238eb6147104" +content-hash = "c0ffa6905c3aa43f7c1ecb93b1a6e24c78ab1a723e44455dd75ae9e55b5b78fc" diff --git a/pyproject.toml b/pyproject.toml index 153ade8f..fab9f328 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ gotrue = ">=1.3,<3.0" httpx = ">=0.24,<0.28" storage3 = ">=0.5.3,<0.8.0" supafunc = ">=0.3.1,<0.6.0" +typing-extensions = "^4.12.2" [tool.poetry.dev-dependencies] pre-commit = "^3.8.0" diff --git a/supabase/_async/client.py b/supabase/_async/client.py index 4fba052c..29bb629a 100644 --- a/supabase/_async/client.py +++ b/supabase/_async/client.py @@ -218,12 +218,12 @@ async def remove_all_channels(self) -> None: @staticmethod def _init_realtime_client( - realtime_url: str, supabase_key: str, options: Optional[Dict[str, Any]] + realtime_url: str, supabase_key: str, options: Optional[Dict[str, Any]] = None ) -> AsyncRealtimeClient: + if options is None: + options = {} """Private method for creating an instance of the realtime-py client.""" - return AsyncRealtimeClient( - realtime_url, token=supabase_key, params=options or {} - ) + return AsyncRealtimeClient(realtime_url, token=supabase_key, **options) @staticmethod def _init_storage_client( diff --git a/supabase/lib/client_options.py b/supabase/lib/client_options.py index 2ce9d84b..d7079c3d 100644 --- a/supabase/lib/client_options.py +++ b/supabase/lib/client_options.py @@ -1,5 +1,5 @@ from dataclasses import dataclass, field -from typing import Any, Dict, Optional, Union +from typing import Dict, Optional, Union from gotrue import ( AsyncMemoryStorage, @@ -12,6 +12,8 @@ from storage3.constants import DEFAULT_TIMEOUT as DEFAULT_STORAGE_CLIENT_TIMEOUT from supafunc.utils import DEFAULT_FUNCTION_CLIENT_TIMEOUT +from supabase.types import RealtimeClientOptions + from ..version import __version__ DEFAULT_HEADERS = {"X-Client-Info": f"supabase-py/{__version__}"} @@ -37,7 +39,7 @@ class ClientOptions: storage: SyncSupportedStorage = field(default_factory=SyncMemoryStorage) """A storage provider. Used to store the logged in session.""" - realtime: Optional[Dict[str, Any]] = None + realtime: Optional[RealtimeClientOptions] = None """Options passed to the realtime-py instance""" postgrest_client_timeout: Union[int, float, Timeout] = ( @@ -63,7 +65,7 @@ def replace( auto_refresh_token: Optional[bool] = None, persist_session: Optional[bool] = None, storage: Optional[SyncSupportedStorage] = None, - realtime: Optional[Dict[str, Any]] = None, + realtime: Optional[RealtimeClientOptions] = None, postgrest_client_timeout: Union[ int, float, Timeout ] = DEFAULT_POSTGREST_CLIENT_TIMEOUT, @@ -104,7 +106,7 @@ def replace( auto_refresh_token: Optional[bool] = None, persist_session: Optional[bool] = None, storage: Optional[SyncSupportedStorage] = None, - realtime: Optional[Dict[str, Any]] = None, + realtime: Optional[RealtimeClientOptions] = None, postgrest_client_timeout: Union[ int, float, Timeout ] = DEFAULT_POSTGREST_CLIENT_TIMEOUT, @@ -142,7 +144,7 @@ def replace( auto_refresh_token: Optional[bool] = None, persist_session: Optional[bool] = None, storage: Optional[SyncSupportedStorage] = None, - realtime: Optional[Dict[str, Any]] = None, + realtime: Optional[RealtimeClientOptions] = None, postgrest_client_timeout: Union[ int, float, Timeout ] = DEFAULT_POSTGREST_CLIENT_TIMEOUT, diff --git a/supabase/types.py b/supabase/types.py new file mode 100644 index 00000000..fdee53ff --- /dev/null +++ b/supabase/types.py @@ -0,0 +1,8 @@ +from typing_extensions import NotRequired, TypedDict + + +class RealtimeClientOptions(TypedDict, total=False): + auto_reconnect: NotRequired[bool] + hb_interval: NotRequired[int] + max_retries: NotRequired[int] + initial_backoff: NotRequired[float] From 629d37447bd770a397d59a5cc96d9c64df579214 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Sat, 28 Sep 2024 21:39:31 +0000 Subject: [PATCH 2/2] chore: add options to sync client --- supabase/_sync/client.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/supabase/_sync/client.py b/supabase/_sync/client.py index 6231133c..0cf3a227 100644 --- a/supabase/_sync/client.py +++ b/supabase/_sync/client.py @@ -217,12 +217,12 @@ def remove_all_channels(self) -> None: @staticmethod def _init_realtime_client( - realtime_url: str, supabase_key: str, options: Optional[Dict[str, Any]] + realtime_url: str, supabase_key: str, options: Optional[Dict[str, Any]] = None ) -> SyncRealtimeClient: + if options is None: + options = {} """Private method for creating an instance of the realtime-py client.""" - return SyncRealtimeClient( - realtime_url, token=supabase_key, params=options or {} - ) + return SyncRealtimeClient(realtime_url, token=supabase_key, **options) @staticmethod def _init_storage_client(