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..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,44 @@ 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, ) self.session = cast(AsyncClient, self.session) @@ -49,7 +76,13 @@ def create_session( timeout: Union[int, float, Timeout], verify: bool = True, proxy: Optional[str] = None, + http_client: Optional[AsyncClient] = None, ) -> AsyncClient: + if http_client is not None: + http_client.base_url = base_url + http_client.headers.update({**headers}) + return http_client + return AsyncClient( base_url=base_url, headers=headers, diff --git a/postgrest/_sync/client.py b/postgrest/_sync/client.py index a9dd307..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,44 @@ 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, ) self.session = cast(SyncClient, self.session) @@ -49,7 +76,13 @@ def create_session( timeout: Union[int, float, Timeout], verify: bool = True, proxy: Optional[str] = None, + http_client: Optional[SyncClient] = None, ) -> SyncClient: + if http_client is not None: + http_client.base_url = base_url + http_client.headers.update({**headers}) + return http_client + return SyncClient( base_url=base_url, headers=headers, diff --git a/postgrest/base_client.py b/postgrest/base_client.py index 2c9756a..92805b3 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, + 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") @@ -33,8 +34,14 @@ def __init__( self.timeout = timeout self.verify = verify self.proxy = proxy + self.http_client = http_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.http_client, ) @abstractmethod @@ -45,6 +52,7 @@ def create_session( timeout: Union[int, float, Timeout], verify: bool = True, proxy: Optional[str] = None, + http_client: Union[SyncClient, AsyncClient, None] = None, ) -> Union[SyncClient, AsyncClient]: raise NotImplementedError()