From 281fbe7e298aed67cd133c3a51265b356481b120 Mon Sep 17 00:00:00 2001 From: "joel@joellee.org" Date: Tue, 21 Feb 2023 14:13:16 +0800 Subject: [PATCH 1/4] fix: add configurable timeout --- storage3/__init__.py | 6 +++--- storage3/_async/client.py | 7 ++++++- storage3/_sync/bucket.py | 2 +- storage3/_sync/client.py | 7 ++++++- storage3/_sync/file_api.py | 2 +- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/storage3/__init__.py b/storage3/__init__.py index 2beedb8e..fadff14a 100644 --- a/storage3/__init__.py +++ b/storage3/__init__.py @@ -26,9 +26,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..513bf943 100644 --- a/storage3/_async/client.py +++ b/storage3/_async/client.py @@ -8,6 +8,8 @@ "AsyncStorageClient", ] +DEFAULT_TIMEOUT = 5 + class AsyncStorageClient(AsyncStorageBucketAPI): """Manage storage buckets and files.""" @@ -16,22 +18,25 @@ def __init__( 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], + timeout: int ) -> AsyncClient: return AsyncClient( base_url=base_url, headers=headers, + timeout=timeout ) async def __aenter__(self) -> AsyncStorageClient: diff --git a/storage3/_sync/bucket.py b/storage3/_sync/bucket.py index efbd6cd8..1dbfd81b 100644 --- a/storage3/_sync/bucket.py +++ b/storage3/_sync/bucket.py @@ -5,7 +5,7 @@ from httpx import HTTPError, Response from ..types import RequestMethod -from ..utils import StorageException, SyncClient +from ..utils import SyncClient, StorageException from .file_api import SyncBucket __all__ = ["SyncStorageBucketAPI"] diff --git a/storage3/_sync/client.py b/storage3/_sync/client.py index 0e4b265e..19d6f40b 100644 --- a/storage3/_sync/client.py +++ b/storage3/_sync/client.py @@ -8,6 +8,8 @@ "SyncStorageClient", ] +DEFAULT_TIMEOUT = 5 + class SyncStorageClient(SyncStorageBucketAPI): """Manage storage buckets and files.""" @@ -16,22 +18,25 @@ def __init__( 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], + timeout: int ) -> SyncClient: return SyncClient( base_url=base_url, headers=headers, + timeout=timeout ) def __enter__(self) -> SyncStorageClient: diff --git a/storage3/_sync/file_api.py b/storage3/_sync/file_api.py index 4f5da05f..509a9b27 100644 --- a/storage3/_sync/file_api.py +++ b/storage3/_sync/file_api.py @@ -16,7 +16,7 @@ RequestMethod, TransformOptions, ) -from ..utils import StorageException, SyncClient +from ..utils import SyncClient, StorageException __all__ = ["SyncBucket"] From 873037a51144685be76619eb151b917e8b0b8fdc Mon Sep 17 00:00:00 2001 From: "joel@joellee.org" Date: Tue, 21 Feb 2023 14:17:39 +0800 Subject: [PATCH 2/4] fix: move timeout to constants file --- storage3/__init__.py | 2 ++ storage3/_async/client.py | 2 +- storage3/_sync/client.py | 2 +- storage3/constants.py | 2 ++ 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/storage3/__init__.py b/storage3/__init__.py index fadff14a..1c39a147 100644 --- a/storage3/__init__.py +++ b/storage3/__init__.py @@ -7,10 +7,12 @@ from storage3._async import AsyncStorageClient from storage3._sync import SyncStorageClient from storage3.utils import __version__ +from storage3.constants import DEFAULT_TIMEOUT __all__ = ["create_client", "__version__"] + @overload def create_client( url: str, headers: dict[str, str], *, is_async: Literal[True] diff --git a/storage3/_async/client.py b/storage3/_async/client.py index 513bf943..02d996e6 100644 --- a/storage3/_async/client.py +++ b/storage3/_async/client.py @@ -3,12 +3,12 @@ from ..utils import AsyncClient, __version__ from .bucket import AsyncStorageBucketAPI from .file_api import AsyncBucketProxy +from storage3.constants import DEFAULT_TIMEOUT __all__ = [ "AsyncStorageClient", ] -DEFAULT_TIMEOUT = 5 class AsyncStorageClient(AsyncStorageBucketAPI): diff --git a/storage3/_sync/client.py b/storage3/_sync/client.py index 19d6f40b..0b6d2a15 100644 --- a/storage3/_sync/client.py +++ b/storage3/_sync/client.py @@ -3,12 +3,12 @@ from ..utils import SyncClient, __version__ from .bucket import SyncStorageBucketAPI from .file_api import SyncBucketProxy +from storage3.constants import DEFAULT_TIMEOUT __all__ = [ "SyncStorageClient", ] -DEFAULT_TIMEOUT = 5 class SyncStorageClient(SyncStorageBucketAPI): diff --git a/storage3/constants.py b/storage3/constants.py index 7a1cda85..3e282d8b 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 = 5 From 15f154789c329916b292d5504ad60b37f8b57ff2 Mon Sep 17 00:00:00 2001 From: "joel@joellee.org" Date: Tue, 21 Feb 2023 14:31:59 +0800 Subject: [PATCH 3/4] fix: bump timeout --- storage3/__init__.py | 3 +-- storage3/_async/client.py | 20 +++++--------------- storage3/_sync/client.py | 20 +++++--------------- storage3/constants.py | 2 +- tests/_sync/test_client.py | 4 +++- 5 files changed, 15 insertions(+), 34 deletions(-) diff --git a/storage3/__init__.py b/storage3/__init__.py index 1c39a147..98706847 100644 --- a/storage3/__init__.py +++ b/storage3/__init__.py @@ -6,13 +6,12 @@ from storage3._async import AsyncStorageClient from storage3._sync import SyncStorageClient -from storage3.utils import __version__ from storage3.constants import DEFAULT_TIMEOUT +from storage3.utils import __version__ __all__ = ["create_client", "__version__"] - @overload def create_client( url: str, headers: dict[str, str], *, is_async: Literal[True] diff --git a/storage3/_async/client.py b/storage3/_async/client.py index 02d996e6..eac22394 100644 --- a/storage3/_async/client.py +++ b/storage3/_async/client.py @@ -1,24 +1,21 @@ from __future__ import annotations +from storage3.constants import DEFAULT_TIMEOUT + from ..utils import AsyncClient, __version__ from .bucket import AsyncStorageBucketAPI from .file_api import AsyncBucketProxy -from storage3.constants import DEFAULT_TIMEOUT __all__ = [ "AsyncStorageClient", ] - class AsyncStorageClient(AsyncStorageBucketAPI): """Manage storage buckets and files.""" def __init__( - self, - url: str, - headers: dict[str, str], - timeout: int = DEFAULT_TIMEOUT + self, url: str, headers: dict[str, str], timeout: int = DEFAULT_TIMEOUT ) -> None: headers = { "User-Agent": f"supabase-py/storage3 v{__version__}", @@ -28,16 +25,9 @@ def __init__( super().__init__(self.session) def _create_session( - self, - base_url: str, - headers: dict[str, str], - timeout: int + self, base_url: str, headers: dict[str, str], timeout: int ) -> AsyncClient: - return AsyncClient( - base_url=base_url, - headers=headers, - timeout=timeout - ) + 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 0b6d2a15..c3bd9c43 100644 --- a/storage3/_sync/client.py +++ b/storage3/_sync/client.py @@ -1,24 +1,21 @@ from __future__ import annotations +from storage3.constants import DEFAULT_TIMEOUT + from ..utils import SyncClient, __version__ from .bucket import SyncStorageBucketAPI from .file_api import SyncBucketProxy -from storage3.constants import DEFAULT_TIMEOUT __all__ = [ "SyncStorageClient", ] - class SyncStorageClient(SyncStorageBucketAPI): """Manage storage buckets and files.""" def __init__( - self, - url: str, - headers: dict[str, str], - timeout: int = DEFAULT_TIMEOUT + self, url: str, headers: dict[str, str], timeout: int = DEFAULT_TIMEOUT ) -> None: headers = { "User-Agent": f"supabase-py/storage3 v{__version__}", @@ -28,16 +25,9 @@ def __init__( super().__init__(self.session) def _create_session( - self, - base_url: str, - headers: dict[str, str], - timeout: int + self, base_url: str, headers: dict[str, str], timeout: int ) -> SyncClient: - return SyncClient( - base_url=base_url, - headers=headers, - timeout=timeout - ) + 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 3e282d8b..7bfc2f32 100644 --- a/storage3/constants.py +++ b/storage3/constants.py @@ -12,4 +12,4 @@ "x-upsert": "false", } -DEFAULT_TIMEOUT = 5 +DEFAULT_TIMEOUT = 20 diff --git a/tests/_sync/test_client.py b/tests/_sync/test_client.py index d3622128..30154626 100644 --- a/tests/_sync/test_client.py +++ b/tests/_sync/test_client.py @@ -79,7 +79,9 @@ def bucket(storage: SyncStorageClient, uuid_factory: Callable[[], str]) -> str: @pytest.fixture(scope="module") -def public_bucket(storage: SyncStorageClient, uuid_factory: Callable[[], str]) -> str: +def public_bucket( + storage: SyncStorageClient, uuid_factory: Callable[[], str] +) -> str: """Creates a test public bucket which will be used in the whole storage tests run and deleted at the end""" bucket_id = uuid_factory() From 2edbd6b74e1cd784b5ef4b39ffdce6e47e5e5e64 Mon Sep 17 00:00:00 2001 From: "joel@joellee.org" Date: Tue, 21 Feb 2023 14:35:43 +0800 Subject: [PATCH 4/4] chore: run black --- storage3/_sync/bucket.py | 2 +- storage3/_sync/file_api.py | 2 +- tests/_sync/test_client.py | 4 +--- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/storage3/_sync/bucket.py b/storage3/_sync/bucket.py index 1dbfd81b..efbd6cd8 100644 --- a/storage3/_sync/bucket.py +++ b/storage3/_sync/bucket.py @@ -5,7 +5,7 @@ from httpx import HTTPError, Response from ..types import RequestMethod -from ..utils import SyncClient, StorageException +from ..utils import StorageException, SyncClient from .file_api import SyncBucket __all__ = ["SyncStorageBucketAPI"] diff --git a/storage3/_sync/file_api.py b/storage3/_sync/file_api.py index 509a9b27..4f5da05f 100644 --- a/storage3/_sync/file_api.py +++ b/storage3/_sync/file_api.py @@ -16,7 +16,7 @@ RequestMethod, TransformOptions, ) -from ..utils import SyncClient, StorageException +from ..utils import StorageException, SyncClient __all__ = ["SyncBucket"] diff --git a/tests/_sync/test_client.py b/tests/_sync/test_client.py index 30154626..d3622128 100644 --- a/tests/_sync/test_client.py +++ b/tests/_sync/test_client.py @@ -79,9 +79,7 @@ def bucket(storage: SyncStorageClient, uuid_factory: Callable[[], str]) -> str: @pytest.fixture(scope="module") -def public_bucket( - storage: SyncStorageClient, uuid_factory: Callable[[], str] -) -> str: +def public_bucket(storage: SyncStorageClient, uuid_factory: Callable[[], str]) -> str: """Creates a test public bucket which will be used in the whole storage tests run and deleted at the end""" bucket_id = uuid_factory()