Skip to content

Commit 1ea965a

Browse files
authored
feat: add timeout as a parameter of clients (#75)
* feat: add timeout as a parameter of clients This feature is for evicting the use of the default timeout of httpx. * feat: use union and constants default value for timeout
1 parent 7897d97 commit 1ea965a

File tree

4 files changed

+59
-15
lines changed

4 files changed

+59
-15
lines changed

postgrest_py/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
__version__ = "0.7.1"
44

5+
from httpx import Timeout
6+
57
from ._async.client import AsyncPostgrestClient
68
from ._async.request_builder import (
79
AsyncFilterRequestBuilder,

postgrest_py/_async/client.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
from __future__ import annotations
22

3-
from typing import Dict, cast
3+
from typing import Dict, Union, cast
44

55
from deprecation import deprecated
6-
from httpx import Response
6+
from httpx import Response, Timeout
77

88
from .. import __version__
9-
from ..base_client import DEFAULT_POSTGREST_CLIENT_HEADERS, BasePostgrestClient
9+
from ..base_client import (
10+
DEFAULT_POSTGREST_CLIENT_HEADERS,
11+
DEFAULT_POSTGREST_CLIENT_TIMEOUT,
12+
BasePostgrestClient,
13+
)
1014
from ..utils import AsyncClient
1115
from .request_builder import AsyncRequestBuilder
1216

@@ -20,16 +24,28 @@ def __init__(
2024
*,
2125
schema: str = "public",
2226
headers: Dict[str, str] = DEFAULT_POSTGREST_CLIENT_HEADERS,
27+
timeout: Union[int, float, Timeout] = DEFAULT_POSTGREST_CLIENT_TIMEOUT,
2328
) -> None:
24-
BasePostgrestClient.__init__(self, base_url, schema=schema, headers=headers)
29+
BasePostgrestClient.__init__(
30+
self,
31+
base_url,
32+
schema=schema,
33+
headers=headers,
34+
timeout=timeout,
35+
)
2536
self.session = cast(AsyncClient, self.session)
2637

2738
def create_session(
2839
self,
2940
base_url: str,
3041
headers: Dict[str, str],
42+
timeout: Union[int, float, Timeout],
3143
) -> AsyncClient:
32-
return AsyncClient(base_url=base_url, headers=headers)
44+
return AsyncClient(
45+
base_url=base_url,
46+
headers=headers,
47+
timeout=timeout,
48+
)
3349

3450
async def __aenter__(self) -> "AsyncPostgrestClient":
3551
return self
@@ -44,7 +60,7 @@ def from_(self, table: str) -> AsyncRequestBuilder:
4460
"""Perform a table operation."""
4561
base_url = str(self.session.base_url)
4662
headers = dict(self.session.headers.items())
47-
session = self.create_session(base_url, headers)
63+
session = self.create_session(base_url, headers, self.session.timeout)
4864
session.auth = self.session.auth
4965
return AsyncRequestBuilder(session, f"/{table}")
5066

postgrest_py/_sync/client.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
from __future__ import annotations
22

3-
from typing import Dict, cast
3+
from typing import Dict, Union, cast
44

55
from deprecation import deprecated
6-
from httpx import Response
6+
from httpx import Response, Timeout
77

88
from .. import __version__
9-
from ..base_client import DEFAULT_POSTGREST_CLIENT_HEADERS, BasePostgrestClient
9+
from ..base_client import (
10+
DEFAULT_POSTGREST_CLIENT_HEADERS,
11+
DEFAULT_POSTGREST_CLIENT_TIMEOUT,
12+
BasePostgrestClient,
13+
)
1014
from ..utils import SyncClient
1115
from .request_builder import SyncRequestBuilder
1216

@@ -20,16 +24,28 @@ def __init__(
2024
*,
2125
schema: str = "public",
2226
headers: Dict[str, str] = DEFAULT_POSTGREST_CLIENT_HEADERS,
27+
timeout: Union[int, float, Timeout] = DEFAULT_POSTGREST_CLIENT_TIMEOUT,
2328
) -> None:
24-
BasePostgrestClient.__init__(self, base_url, schema=schema, headers=headers)
29+
BasePostgrestClient.__init__(
30+
self,
31+
base_url,
32+
schema=schema,
33+
headers=headers,
34+
timeout=timeout,
35+
)
2536
self.session = cast(SyncClient, self.session)
2637

2738
def create_session(
2839
self,
2940
base_url: str,
3041
headers: Dict[str, str],
42+
timeout: Union[int, float, Timeout],
3143
) -> SyncClient:
32-
return SyncClient(base_url=base_url, headers=headers)
44+
return SyncClient(
45+
base_url=base_url,
46+
headers=headers,
47+
timeout=timeout,
48+
)
3349

3450
def __enter__(self) -> SyncPostgrestClient:
3551
return self
@@ -44,7 +60,7 @@ def from_(self, table: str) -> SyncRequestBuilder:
4460
"""Perform a table operation."""
4561
base_url = str(self.session.base_url)
4662
headers = dict(self.session.headers.items())
47-
session = self.create_session(base_url, headers)
63+
session = self.create_session(base_url, headers, self.session.timeout)
4864
session.auth = self.session.auth
4965
return SyncRequestBuilder(session, f"/{table}")
5066

postgrest_py/base_client.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from abc import ABC, abstractmethod
44
from typing import Dict, Optional, Union
55

6-
from httpx import BasicAuth
6+
from httpx import BasicAuth, Timeout
77

88
from .utils import AsyncClient, SyncClient
99

@@ -12,23 +12,33 @@
1212
"Content-Type": "application/json",
1313
}
1414

15+
DEFAULT_POSTGREST_CLIENT_TIMEOUT: int = 5
16+
1517

1618
class BasePostgrestClient(ABC):
1719
"""Base PostgREST client."""
1820

19-
def __init__(self, base_url: str, *, schema: str, headers: Dict[str, str]) -> None:
21+
def __init__(
22+
self,
23+
base_url: str,
24+
*,
25+
schema: str,
26+
headers: Dict[str, str],
27+
timeout: Union[int, float, Timeout],
28+
) -> None:
2029
headers = {
2130
**headers,
2231
"Accept-Profile": schema,
2332
"Content-Profile": schema,
2433
}
25-
self.session = self.create_session(base_url, headers)
34+
self.session = self.create_session(base_url, headers, timeout)
2635

2736
@abstractmethod
2837
def create_session(
2938
self,
3039
base_url: str,
3140
headers: Dict[str, str],
41+
timeout: Union[int, float, Timeout],
3242
) -> Union[SyncClient, AsyncClient]:
3343
raise NotImplementedError()
3444

0 commit comments

Comments
 (0)