From 1e02178d3a62225d8230c36ca134cc01e02c9daa Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Sat, 28 Sep 2024 18:46:11 +0000 Subject: [PATCH] fix: async client options default values (#937) --- supabase/__init__.py | 8 +++- supabase/_async/client.py | 2 +- supabase/_sync/client.py | 2 +- supabase/client.py | 33 ++++++++++++- supabase/lib/client_options.py | 86 +++++++++++++++++++++++++++++++++- tests/test_client.py | 3 +- tests/test_client_options.py | 2 +- 7 files changed, 127 insertions(+), 9 deletions(-) diff --git a/supabase/__init__.py b/supabase/__init__.py index 77517668..a3d41fe5 100644 --- a/supabase/__init__.py +++ b/supabase/__init__.py @@ -16,9 +16,9 @@ # Async Client from ._async.auth_client import AsyncSupabaseAuthClient as ASupabaseAuthClient +from ._async.client import AsyncClient from ._async.client import AsyncClient as AClient from ._async.client import AsyncStorageClient as ASupabaseStorageClient -from ._async.client import ClientOptions as AClientOptions from ._async.client import create_client as acreate_client from ._async.client import create_client as create_async_client @@ -29,7 +29,9 @@ from ._sync.client import create_client # Lib -from .lib.client_options import ClientOptions +from .lib.client_options import AsyncClientOptions +from .lib.client_options import AsyncClientOptions as AClientOptions +from .lib.client_options import SyncClientOptions as ClientOptions # Version from .version import __version__ @@ -41,6 +43,8 @@ "ASupabaseAuthClient", "ASupabaseStorageClient", "AClientOptions", + "AsyncClient", + "AsyncClientOptions", "create_client", "Client", "SupabaseAuthClient", diff --git a/supabase/_async/client.py b/supabase/_async/client.py index ef2cefd7..4fba052c 100644 --- a/supabase/_async/client.py +++ b/supabase/_async/client.py @@ -16,7 +16,7 @@ from storage3.constants import DEFAULT_TIMEOUT as DEFAULT_STORAGE_CLIENT_TIMEOUT from supafunc import AsyncFunctionsClient -from ..lib.client_options import ClientOptions +from ..lib.client_options import AsyncClientOptions as ClientOptions from .auth_client import AsyncSupabaseAuthClient diff --git a/supabase/_sync/client.py b/supabase/_sync/client.py index c9d0d8e9..6231133c 100644 --- a/supabase/_sync/client.py +++ b/supabase/_sync/client.py @@ -15,7 +15,7 @@ from storage3.constants import DEFAULT_TIMEOUT as DEFAULT_STORAGE_CLIENT_TIMEOUT from supafunc import SyncFunctionsClient -from ..lib.client_options import ClientOptions +from ..lib.client_options import SyncClientOptions as ClientOptions from .auth_client import SyncSupabaseAuthClient diff --git a/supabase/client.py b/supabase/client.py index 397ea041..363fa37b 100644 --- a/supabase/client.py +++ b/supabase/client.py @@ -1,11 +1,24 @@ +from gotrue.errors import ( + AuthApiError, + AuthError, + AuthImplicitGrantRedirectError, + AuthInvalidCredentialsError, + AuthRetryableError, + AuthSessionMissingError, + AuthUnknownError, + AuthWeakPasswordError, +) from postgrest import APIError as PostgrestAPIError from postgrest import APIResponse as PostgrestAPIResponse +from realtime import AuthorizationError, NotConnectedError from storage3.utils import StorageException +from supafunc.errors import FunctionsError, FunctionsHttpError, FunctionsRelayError # Async Client from ._async.auth_client import AsyncSupabaseAuthClient from ._async.client import AsyncClient from ._async.client import AsyncStorageClient as AsyncSupabaseStorageClient +from ._async.client import create_client as acreate_client from ._async.client import create_client as create_async_client # Sync Client @@ -15,15 +28,20 @@ from ._sync.client import create_client # Lib -from .lib.client_options import ClientOptions +from .lib.client_options import AsyncClientOptions +from .lib.client_options import AsyncClientOptions as AClientOptions +from .lib.client_options import SyncClientOptions as ClientOptions # Version from .version import __version__ __all__ = [ "AsyncSupabaseAuthClient", + "acreate_client", "create_async_client", + "AClientOptions", "AsyncClient", + "AsyncClientOptions", "AsyncSupabaseStorageClient", "SupabaseAuthClient", "create_client", @@ -34,4 +52,17 @@ "PostgrestAPIResponse", "StorageException", "__version__", + "AuthApiError", + "AuthError", + "AuthImplicitGrantRedirectError", + "AuthInvalidCredentialsError", + "AuthRetryableError", + "AuthSessionMissingError", + "AuthWeakPasswordError", + "AuthUnknownError", + "FunctionsHttpError", + "FunctionsRelayError", + "FunctionsError", + "AuthorizationError", + "NotConnectedError", ] diff --git a/supabase/lib/client_options.py b/supabase/lib/client_options.py index 898eab25..2ce9d84b 100644 --- a/supabase/lib/client_options.py +++ b/supabase/lib/client_options.py @@ -1,7 +1,12 @@ from dataclasses import dataclass, field from typing import Any, Dict, Optional, Union -from gotrue import AuthFlowType, SyncMemoryStorage, SyncSupportedStorage +from gotrue import ( + AsyncMemoryStorage, + AuthFlowType, + SyncMemoryStorage, + SyncSupportedStorage, +) from httpx import Timeout from postgrest.constants import DEFAULT_POSTGREST_CLIENT_TIMEOUT from storage3.constants import DEFAULT_TIMEOUT as DEFAULT_STORAGE_CLIENT_TIMEOUT @@ -85,3 +90,82 @@ def replace( ) client_options.flow_type = flow_type or self.flow_type return client_options + + +@dataclass +class AsyncClientOptions(ClientOptions): + storage: SyncSupportedStorage = field(default_factory=AsyncMemoryStorage) + """A storage provider. Used to store the logged in session.""" + + def replace( + self, + schema: Optional[str] = None, + headers: Optional[Dict[str, str]] = None, + auto_refresh_token: Optional[bool] = None, + persist_session: Optional[bool] = None, + storage: Optional[SyncSupportedStorage] = None, + realtime: Optional[Dict[str, Any]] = None, + postgrest_client_timeout: Union[ + int, float, Timeout + ] = DEFAULT_POSTGREST_CLIENT_TIMEOUT, + storage_client_timeout: Union[ + int, float, Timeout + ] = DEFAULT_STORAGE_CLIENT_TIMEOUT, + flow_type: Optional[AuthFlowType] = None, + ) -> "AsyncClientOptions": + """Create a new SupabaseClientOptions with changes""" + client_options = AsyncClientOptions() + client_options.schema = schema or self.schema + client_options.headers = headers or self.headers + client_options.auto_refresh_token = ( + auto_refresh_token or self.auto_refresh_token + ) + client_options.persist_session = persist_session or self.persist_session + client_options.storage = storage or self.storage + client_options.realtime = realtime or self.realtime + client_options.postgrest_client_timeout = ( + postgrest_client_timeout or self.postgrest_client_timeout + ) + client_options.storage_client_timeout = ( + storage_client_timeout or self.storage_client_timeout + ) + client_options.flow_type = flow_type or self.flow_type + return client_options + + +@dataclass +class SyncClientOptions(ClientOptions): + def replace( + self, + schema: Optional[str] = None, + headers: Optional[Dict[str, str]] = None, + auto_refresh_token: Optional[bool] = None, + persist_session: Optional[bool] = None, + storage: Optional[SyncSupportedStorage] = None, + realtime: Optional[Dict[str, Any]] = None, + postgrest_client_timeout: Union[ + int, float, Timeout + ] = DEFAULT_POSTGREST_CLIENT_TIMEOUT, + storage_client_timeout: Union[ + int, float, Timeout + ] = DEFAULT_STORAGE_CLIENT_TIMEOUT, + flow_type: Optional[AuthFlowType] = None, + ) -> "SyncClientOptions": + """Create a new SupabaseClientOptions with changes""" + client_options = SyncClientOptions() + client_options.schema = schema or self.schema + client_options.headers = headers or self.headers + client_options.auto_refresh_token = ( + auto_refresh_token or self.auto_refresh_token + ) + client_options.persist_session = persist_session or self.persist_session + client_options.storage = storage or self.storage + client_options.realtime = realtime or self.realtime + client_options.postgrest_client_timeout = ( + postgrest_client_timeout or self.postgrest_client_timeout + ) + client_options.storage_client_timeout = ( + storage_client_timeout or self.storage_client_timeout + ) + client_options.flow_type = flow_type or self.flow_type + return client_options diff --git a/tests/test_client.py b/tests/test_client.py index e4c1369b..672e5e6d 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -6,8 +6,7 @@ import pytest -from supabase import Client, create_client -from supabase.lib.client_options import ClientOptions +from supabase import Client, ClientOptions, create_client @pytest.mark.xfail( diff --git a/tests/test_client_options.py b/tests/test_client_options.py index 75361722..ce2279b7 100644 --- a/tests/test_client_options.py +++ b/tests/test_client_options.py @@ -1,6 +1,6 @@ from gotrue import SyncMemoryStorage -from supabase.lib.client_options import ClientOptions +from supabase import ClientOptions class TestClientOptions: