diff --git a/supabase/client.py b/supabase/client.py index 007b6d1e..918fed14 100644 --- a/supabase/client.py +++ b/supabase/client.py @@ -1,12 +1,13 @@ -from typing import Any, Dict +from typing import Any, Dict, Union +from httpx import Timeout from postgrest import SyncFilterRequestBuilder, SyncPostgrestClient, SyncRequestBuilder +from postgrest.constants import DEFAULT_POSTGREST_CLIENT_TIMEOUT from .lib.auth_client import SupabaseAuthClient from .lib.client_options import ClientOptions from .lib.storage_client import SupabaseStorageClient - class Client: """Supabase client class.""" @@ -152,10 +153,14 @@ def _init_supabase_auth_client( @staticmethod def _init_postgrest_client( - rest_url: str, supabase_key: str, headers: Dict[str, str], schema: str + rest_url: str, + supabase_key: str, + headers: Dict[str, str], + schema: str, + timeout: Union[int, float, Timeout] = DEFAULT_POSTGREST_CLIENT_TIMEOUT ) -> SyncPostgrestClient: """Private helper for creating an instance of the Postgrest client.""" - client = SyncPostgrestClient(rest_url, headers=headers, schema=schema) + client = SyncPostgrestClient(rest_url, headers=headers, schema=schema, timeout=timeout) client.auth(token=supabase_key) return client diff --git a/supabase/lib/client_options.py b/supabase/lib/client_options.py index f3cbffdd..b2c93b25 100644 --- a/supabase/lib/client_options.py +++ b/supabase/lib/client_options.py @@ -1,7 +1,9 @@ from dataclasses import dataclass, field -from typing import Any, Callable, Dict, Optional +from typing import Any, Callable, Dict, Optional, Union from gotrue import SyncMemoryStorage, SyncSupportedStorage +from httpx import Timeout +from postgrest.constants import DEFAULT_POSTGREST_CLIENT_TIMEOUT from supabase import __version__ @@ -34,6 +36,9 @@ class ClientOptions: fetch: Optional[Callable] = None """A custom `fetch` implementation.""" + timeout: Union[int, float, Timeout] = DEFAULT_POSTGREST_CLIENT_TIMEOUT + """Timeout passed to the SyncPostgrestClient instance.""" + def replace( self, schema: Optional[str] = None, @@ -43,6 +48,7 @@ def replace( local_storage: Optional[SyncSupportedStorage] = None, realtime: Optional[Dict[str, Any]] = None, fetch: Optional[Callable] = None, + timeout: Union[int, float, Timeout] = DEFAULT_POSTGREST_CLIENT_TIMEOUT ) -> "ClientOptions": """Create a new SupabaseClientOptions with changes""" client_options = ClientOptions() @@ -55,4 +61,5 @@ def replace( client_options.local_storage = local_storage or self.local_storage client_options.realtime = realtime or self.realtime client_options.fetch = fetch or self.fetch + client_options.timeout = timeout or self.timeout return client_options diff --git a/tests/test_client_options.py b/tests/test_client_options.py index 46273ec0..4eb38eca 100644 --- a/tests/test_client_options.py +++ b/tests/test_client_options.py @@ -13,6 +13,7 @@ def test__client_options__replace__returns_updated_options(): persist_session=False, local_storage=local_storage, realtime={"key": "value"}, + timeout=5 ) actual = options.replace(schema="new schema") @@ -23,6 +24,7 @@ def test__client_options__replace__returns_updated_options(): persist_session=False, local_storage=local_storage, realtime={"key": "value"}, + timeout=5 ) assert actual == expected