From 50d4910b97a5054358369ade41f295d6adf7f78f Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Thu, 24 Apr 2025 17:46:18 +0000 Subject: [PATCH 1/7] wip: dependency injection for httpx --- Makefile | 1 + postgrest/_async/client.py | 6 ++++++ postgrest/_sync/client.py | 6 ++++++ postgrest/base_client.py | 1 + 4 files changed, 14 insertions(+) diff --git a/Makefile b/Makefile index 652e0e6..577bf5e 100644 --- a/Makefile +++ b/Makefile @@ -37,6 +37,7 @@ remove_pytest_asyncio_from_sync: sed -i 's/@pytest.mark.asyncio//g' tests/_sync/test_client.py sed -i 's/_async/_sync/g' tests/_sync/test_client.py sed -i 's/Async/Sync/g' tests/_sync/test_client.py + sed -i 's/Async/Sync/g' postgrest/_sync/request_builder.py sleep: sleep 2 diff --git a/postgrest/_async/client.py b/postgrest/_async/client.py index 94c9c7f..40f6c67 100644 --- a/postgrest/_async/client.py +++ b/postgrest/_async/client.py @@ -30,6 +30,7 @@ def __init__( timeout: Union[int, float, Timeout] = DEFAULT_POSTGREST_CLIENT_TIMEOUT, verify: bool = True, proxy: Optional[str] = None, + client: Union[AsyncClient, None] = None, ) -> None: BasePostgrestClient.__init__( self, @@ -39,6 +40,7 @@ def __init__( timeout=timeout, verify=verify, proxy=proxy, + client=client, ) self.session = cast(AsyncClient, self.session) @@ -49,7 +51,11 @@ def create_session( timeout: Union[int, float, Timeout], verify: bool = True, proxy: Optional[str] = None, + client: Union[AsyncClient, None] = None, ) -> AsyncClient: + if client is not None: + return client + return AsyncClient( base_url=base_url, headers=headers, diff --git a/postgrest/_sync/client.py b/postgrest/_sync/client.py index a9dd307..53328d6 100644 --- a/postgrest/_sync/client.py +++ b/postgrest/_sync/client.py @@ -30,6 +30,7 @@ def __init__( timeout: Union[int, float, Timeout] = DEFAULT_POSTGREST_CLIENT_TIMEOUT, verify: bool = True, proxy: Optional[str] = None, + client: Union[SyncClient, None] = None, ) -> None: BasePostgrestClient.__init__( self, @@ -39,6 +40,7 @@ def __init__( timeout=timeout, verify=verify, proxy=proxy, + client=client, ) self.session = cast(SyncClient, self.session) @@ -49,7 +51,11 @@ def create_session( timeout: Union[int, float, Timeout], verify: bool = True, proxy: Optional[str] = None, + client: Union[SyncClient, None] = None, ) -> SyncClient: + if client is not None: + return client + return SyncClient( base_url=base_url, headers=headers, diff --git a/postgrest/base_client.py b/postgrest/base_client.py index 2c9756a..faa31a1 100644 --- a/postgrest/base_client.py +++ b/postgrest/base_client.py @@ -45,6 +45,7 @@ def create_session( timeout: Union[int, float, Timeout], verify: bool = True, proxy: Optional[str] = None, + client: Union[SyncClient, AsyncClient, None] = None, ) -> Union[SyncClient, AsyncClient]: raise NotImplementedError() From f0765d0866ee2c22387a063df6388200cb62eab3 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Sun, 27 Apr 2025 02:29:02 +0000 Subject: [PATCH 2/7] fix: use headers and base_url from client config --- postgrest/_async/client.py | 2 ++ postgrest/_sync/client.py | 2 ++ postgrest/base_client.py | 9 ++++++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/postgrest/_async/client.py b/postgrest/_async/client.py index 40f6c67..781ee3c 100644 --- a/postgrest/_async/client.py +++ b/postgrest/_async/client.py @@ -54,6 +54,8 @@ def create_session( client: Union[AsyncClient, None] = None, ) -> AsyncClient: if client is not None: + client.base_url = base_url + client.headers = headers return client return AsyncClient( diff --git a/postgrest/_sync/client.py b/postgrest/_sync/client.py index 53328d6..e1403b4 100644 --- a/postgrest/_sync/client.py +++ b/postgrest/_sync/client.py @@ -54,6 +54,8 @@ def create_session( client: Union[SyncClient, None] = None, ) -> SyncClient: if client is not None: + client.base_url = base_url + client.headers = headers return client return SyncClient( diff --git a/postgrest/base_client.py b/postgrest/base_client.py index faa31a1..6955f7a 100644 --- a/postgrest/base_client.py +++ b/postgrest/base_client.py @@ -20,6 +20,7 @@ def __init__( timeout: Union[int, float, Timeout], verify: bool = True, proxy: Optional[str] = None, + client: Union[SyncClient, AsyncClient, None] = None, ) -> None: if not is_http_url(base_url): ValueError("base_url must be a valid HTTP URL string") @@ -33,8 +34,14 @@ def __init__( self.timeout = timeout self.verify = verify self.proxy = proxy + self.client = client self.session = self.create_session( - self.base_url, self.headers, self.timeout, self.verify, self.proxy + self.base_url, + self.headers, + self.timeout, + self.verify, + self.proxy, + self.client, ) @abstractmethod From d34d99e81efabdd69936b7a68255d29d3853a473 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Wed, 30 Apr 2025 09:00:10 +0000 Subject: [PATCH 3/7] change client property name to standardise with the auth client --- postgrest/_async/client.py | 14 +++++++------- postgrest/_sync/client.py | 14 +++++++------- postgrest/base_client.py | 8 ++++---- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/postgrest/_async/client.py b/postgrest/_async/client.py index 781ee3c..6cc3b2d 100644 --- a/postgrest/_async/client.py +++ b/postgrest/_async/client.py @@ -30,7 +30,7 @@ def __init__( timeout: Union[int, float, Timeout] = DEFAULT_POSTGREST_CLIENT_TIMEOUT, verify: bool = True, proxy: Optional[str] = None, - client: Union[AsyncClient, None] = None, + http_client: Union[AsyncClient, None] = None, ) -> None: BasePostgrestClient.__init__( self, @@ -40,7 +40,7 @@ def __init__( timeout=timeout, verify=verify, proxy=proxy, - client=client, + http_client=http_client, ) self.session = cast(AsyncClient, self.session) @@ -51,12 +51,12 @@ def create_session( timeout: Union[int, float, Timeout], verify: bool = True, proxy: Optional[str] = None, - client: Union[AsyncClient, None] = None, + http_client: Union[AsyncClient, None] = None, ) -> AsyncClient: - if client is not None: - client.base_url = base_url - client.headers = headers - return client + if http_client is not None: + http_client.base_url = base_url + http_client.headers = headers + return http_client return AsyncClient( base_url=base_url, diff --git a/postgrest/_sync/client.py b/postgrest/_sync/client.py index e1403b4..d9c8b2f 100644 --- a/postgrest/_sync/client.py +++ b/postgrest/_sync/client.py @@ -30,7 +30,7 @@ def __init__( timeout: Union[int, float, Timeout] = DEFAULT_POSTGREST_CLIENT_TIMEOUT, verify: bool = True, proxy: Optional[str] = None, - client: Union[SyncClient, None] = None, + http_client: Union[SyncClient, None] = None, ) -> None: BasePostgrestClient.__init__( self, @@ -40,7 +40,7 @@ def __init__( timeout=timeout, verify=verify, proxy=proxy, - client=client, + http_client=http_client, ) self.session = cast(SyncClient, self.session) @@ -51,12 +51,12 @@ def create_session( timeout: Union[int, float, Timeout], verify: bool = True, proxy: Optional[str] = None, - client: Union[SyncClient, None] = None, + http_client: Union[SyncClient, None] = None, ) -> SyncClient: - if client is not None: - client.base_url = base_url - client.headers = headers - return client + if http_client is not None: + http_client.base_url = base_url + http_client.headers = headers + return http_client return SyncClient( base_url=base_url, diff --git a/postgrest/base_client.py b/postgrest/base_client.py index 6955f7a..92805b3 100644 --- a/postgrest/base_client.py +++ b/postgrest/base_client.py @@ -20,7 +20,7 @@ def __init__( timeout: Union[int, float, Timeout], verify: bool = True, proxy: Optional[str] = None, - client: Union[SyncClient, AsyncClient, None] = None, + http_client: Union[SyncClient, AsyncClient, None] = None, ) -> None: if not is_http_url(base_url): ValueError("base_url must be a valid HTTP URL string") @@ -34,14 +34,14 @@ def __init__( self.timeout = timeout self.verify = verify self.proxy = proxy - self.client = client + self.http_client = http_client self.session = self.create_session( self.base_url, self.headers, self.timeout, self.verify, self.proxy, - self.client, + self.http_client, ) @abstractmethod @@ -52,7 +52,7 @@ def create_session( timeout: Union[int, float, Timeout], verify: bool = True, proxy: Optional[str] = None, - client: Union[SyncClient, AsyncClient, None] = None, + http_client: Union[SyncClient, AsyncClient, None] = None, ) -> Union[SyncClient, AsyncClient]: raise NotImplementedError() From 7b2928df48ff7efe750c19dc3930a95a8beec46c Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Sat, 10 May 2025 08:38:57 +0000 Subject: [PATCH 4/7] fix: update type for httpx client --- postgrest/_async/client.py | 2 +- postgrest/_sync/client.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/postgrest/_async/client.py b/postgrest/_async/client.py index 6cc3b2d..4ac4085 100644 --- a/postgrest/_async/client.py +++ b/postgrest/_async/client.py @@ -51,7 +51,7 @@ def create_session( timeout: Union[int, float, Timeout], verify: bool = True, proxy: Optional[str] = None, - http_client: Union[AsyncClient, None] = None, + http_client: Optional[AsyncClient] = None, ) -> AsyncClient: if http_client is not None: http_client.base_url = base_url diff --git a/postgrest/_sync/client.py b/postgrest/_sync/client.py index d9c8b2f..2e7f9bf 100644 --- a/postgrest/_sync/client.py +++ b/postgrest/_sync/client.py @@ -51,7 +51,7 @@ def create_session( timeout: Union[int, float, Timeout], verify: bool = True, proxy: Optional[str] = None, - http_client: Union[SyncClient, None] = None, + http_client: Optional[SyncClient] = None, ) -> SyncClient: if http_client is not None: http_client.base_url = base_url From e37b37ce594957961d67056e63cfdb8fa7723395 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Sat, 10 May 2025 09:10:17 +0000 Subject: [PATCH 5/7] fix: change type from union to optional --- postgrest/_async/client.py | 2 +- postgrest/_sync/client.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/postgrest/_async/client.py b/postgrest/_async/client.py index 4ac4085..63d2901 100644 --- a/postgrest/_async/client.py +++ b/postgrest/_async/client.py @@ -30,7 +30,7 @@ def __init__( timeout: Union[int, float, Timeout] = DEFAULT_POSTGREST_CLIENT_TIMEOUT, verify: bool = True, proxy: Optional[str] = None, - http_client: Union[AsyncClient, None] = None, + http_client: Optional[AsyncClient] = None, ) -> None: BasePostgrestClient.__init__( self, diff --git a/postgrest/_sync/client.py b/postgrest/_sync/client.py index 2e7f9bf..5a18932 100644 --- a/postgrest/_sync/client.py +++ b/postgrest/_sync/client.py @@ -30,7 +30,7 @@ def __init__( timeout: Union[int, float, Timeout] = DEFAULT_POSTGREST_CLIENT_TIMEOUT, verify: bool = True, proxy: Optional[str] = None, - http_client: Union[SyncClient, None] = None, + http_client: Optional[SyncClient] = None, ) -> None: BasePostgrestClient.__init__( self, From 9c85aca0e74190d1a4fe8289f5e24e7cad0601b6 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Mon, 12 May 2025 11:49:10 +0000 Subject: [PATCH 6/7] fix: update headers instead of replacing them --- postgrest/_async/client.py | 2 +- postgrest/_sync/client.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/postgrest/_async/client.py b/postgrest/_async/client.py index 63d2901..b8f9ff5 100644 --- a/postgrest/_async/client.py +++ b/postgrest/_async/client.py @@ -55,7 +55,7 @@ def create_session( ) -> AsyncClient: if http_client is not None: http_client.base_url = base_url - http_client.headers = headers + http_client.headers.update({**headers}) return http_client return AsyncClient( diff --git a/postgrest/_sync/client.py b/postgrest/_sync/client.py index 5a18932..d7d135b 100644 --- a/postgrest/_sync/client.py +++ b/postgrest/_sync/client.py @@ -55,7 +55,7 @@ def create_session( ) -> SyncClient: if http_client is not None: http_client.base_url = base_url - http_client.headers = headers + http_client.headers.update({**headers}) return http_client return SyncClient( From 0c50d9ab3757df4685a03d422cdb3a728e2b9416 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Thu, 15 May 2025 08:32:14 +0100 Subject: [PATCH 7/7] fix: add deprecation warnings for params to be removed in the future --- postgrest/_async/client.py | 33 +++++++++++++++++++++++++++++---- postgrest/_sync/client.py | 33 +++++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/postgrest/_async/client.py b/postgrest/_async/client.py index b8f9ff5..35480f5 100644 --- a/postgrest/_async/client.py +++ b/postgrest/_async/client.py @@ -1,6 +1,7 @@ from __future__ import annotations from typing import Any, Dict, Optional, Union, cast +from warnings import warn from deprecation import deprecated from httpx import Headers, QueryParams, Timeout @@ -27,18 +28,42 @@ def __init__( *, schema: str = "public", headers: Dict[str, str] = DEFAULT_POSTGREST_CLIENT_HEADERS, - timeout: Union[int, float, Timeout] = DEFAULT_POSTGREST_CLIENT_TIMEOUT, - verify: bool = True, + timeout: Union[int, float, Timeout, None] = None, + verify: Optional[bool] = None, proxy: Optional[str] = None, http_client: Optional[AsyncClient] = None, ) -> None: + if timeout is not None: + warn( + "The 'timeout' parameter is deprecated. Please configure it in the http client instead.", + DeprecationWarning, + stacklevel=2, + ) + if verify is not None: + warn( + "The 'verify' parameter is deprecated. Please configure it in the http client instead.", + DeprecationWarning, + stacklevel=2, + ) + if proxy is not None: + warn( + "The 'proxy' parameter is deprecated. Please configure it in the http client instead.", + DeprecationWarning, + stacklevel=2, + ) + + self.verify = bool(verify) if verify is not None else True + self.timeout = ( + int(abs(timeout)) if timeout is not None else DEFAULT_POSTGREST_CLIENT_TIMEOUT + ) + BasePostgrestClient.__init__( self, base_url, schema=schema, headers=headers, - timeout=timeout, - verify=verify, + timeout=self.timeout, + verify=self.verify, proxy=proxy, http_client=http_client, ) diff --git a/postgrest/_sync/client.py b/postgrest/_sync/client.py index d7d135b..7eed780 100644 --- a/postgrest/_sync/client.py +++ b/postgrest/_sync/client.py @@ -1,6 +1,7 @@ from __future__ import annotations from typing import Any, Dict, Optional, Union, cast +from warnings import warn from deprecation import deprecated from httpx import Headers, QueryParams, Timeout @@ -27,18 +28,42 @@ def __init__( *, schema: str = "public", headers: Dict[str, str] = DEFAULT_POSTGREST_CLIENT_HEADERS, - timeout: Union[int, float, Timeout] = DEFAULT_POSTGREST_CLIENT_TIMEOUT, - verify: bool = True, + timeout: Union[int, float, Timeout, None] = None, + verify: Optional[bool] = None, proxy: Optional[str] = None, http_client: Optional[SyncClient] = None, ) -> None: + if timeout is not None: + warn( + "The 'timeout' parameter is deprecated. Please configure it in the http client instead.", + DeprecationWarning, + stacklevel=2, + ) + if verify is not None: + warn( + "The 'verify' parameter is deprecated. Please configure it in the http client instead.", + DeprecationWarning, + stacklevel=2, + ) + if proxy is not None: + warn( + "The 'proxy' parameter is deprecated. Please configure it in the http client instead.", + DeprecationWarning, + stacklevel=2, + ) + + self.verify = bool(verify) if verify is not None else True + self.timeout = ( + int(abs(timeout)) if timeout is not None else DEFAULT_POSTGREST_CLIENT_TIMEOUT + ) + BasePostgrestClient.__init__( self, base_url, schema=schema, headers=headers, - timeout=timeout, - verify=verify, + timeout=self.timeout, + verify=self.verify, proxy=proxy, http_client=http_client, )