Skip to content

feat: allow injection of httpx client #591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 37 additions & 4 deletions postgrest/_async/client.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand All @@ -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,
Expand Down
41 changes: 37 additions & 4 deletions postgrest/_sync/client.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand All @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion postgrest/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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()

Expand Down