diff --git a/storage3/__init__.py b/storage3/__init__.py index 2beedb8e..98706847 100644 --- a/storage3/__init__.py +++ b/storage3/__init__.py @@ -6,6 +6,7 @@ from storage3._async import AsyncStorageClient from storage3._sync import SyncStorageClient +from storage3.constants import DEFAULT_TIMEOUT from storage3.utils import __version__ __all__ = ["create_client", "__version__"] @@ -26,9 +27,9 @@ def create_client( def create_client( - url: str, headers: dict[str, str], *, is_async: bool + url: str, headers: dict[str, str], *, is_async: bool, timeout: int = DEFAULT_TIMEOUT ) -> Union[AsyncStorageClient, SyncStorageClient]: if is_async: - return AsyncStorageClient(url, headers) + return AsyncStorageClient(url, headers, timeout) else: - return SyncStorageClient(url, headers) + return SyncStorageClient(url, headers, timeout) diff --git a/storage3/_async/client.py b/storage3/_async/client.py index ec6d697e..eac22394 100644 --- a/storage3/_async/client.py +++ b/storage3/_async/client.py @@ -1,5 +1,7 @@ from __future__ import annotations +from storage3.constants import DEFAULT_TIMEOUT + from ..utils import AsyncClient, __version__ from .bucket import AsyncStorageBucketAPI from .file_api import AsyncBucketProxy @@ -13,26 +15,19 @@ class AsyncStorageClient(AsyncStorageBucketAPI): """Manage storage buckets and files.""" def __init__( - self, - url: str, - headers: dict[str, str], + self, url: str, headers: dict[str, str], timeout: int = DEFAULT_TIMEOUT ) -> None: headers = { "User-Agent": f"supabase-py/storage3 v{__version__}", **headers, } - self.session = self._create_session(url, headers) + self.session = self._create_session(url, headers, timeout) super().__init__(self.session) def _create_session( - self, - base_url: str, - headers: dict[str, str], + self, base_url: str, headers: dict[str, str], timeout: int ) -> AsyncClient: - return AsyncClient( - base_url=base_url, - headers=headers, - ) + return AsyncClient(base_url=base_url, headers=headers, timeout=timeout) async def __aenter__(self) -> AsyncStorageClient: return self diff --git a/storage3/_sync/client.py b/storage3/_sync/client.py index 0e4b265e..c3bd9c43 100644 --- a/storage3/_sync/client.py +++ b/storage3/_sync/client.py @@ -1,5 +1,7 @@ from __future__ import annotations +from storage3.constants import DEFAULT_TIMEOUT + from ..utils import SyncClient, __version__ from .bucket import SyncStorageBucketAPI from .file_api import SyncBucketProxy @@ -13,26 +15,19 @@ class SyncStorageClient(SyncStorageBucketAPI): """Manage storage buckets and files.""" def __init__( - self, - url: str, - headers: dict[str, str], + self, url: str, headers: dict[str, str], timeout: int = DEFAULT_TIMEOUT ) -> None: headers = { "User-Agent": f"supabase-py/storage3 v{__version__}", **headers, } - self.session = self._create_session(url, headers) + self.session = self._create_session(url, headers, timeout) super().__init__(self.session) def _create_session( - self, - base_url: str, - headers: dict[str, str], + self, base_url: str, headers: dict[str, str], timeout: int ) -> SyncClient: - return SyncClient( - base_url=base_url, - headers=headers, - ) + return SyncClient(base_url=base_url, headers=headers, timeout=timeout) def __enter__(self) -> SyncStorageClient: return self diff --git a/storage3/constants.py b/storage3/constants.py index 7a1cda85..7bfc2f32 100644 --- a/storage3/constants.py +++ b/storage3/constants.py @@ -11,3 +11,5 @@ "content-type": "text/plain;charset=UTF-8", "x-upsert": "false", } + +DEFAULT_TIMEOUT = 20