Skip to content

Commit 7f5589b

Browse files
Increase default read timeout
- Default timeout remains 10 seconds, except for 120 seconds for read, for when pdfRest takes longer to process a file. - Timeout is configurable by the customer both at the client and individual call level. Assisted-by: Codex
1 parent 3c0bb15 commit 7f5589b

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

src/pdfrest/client.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
DEFAULT_BASE_URL = "https://api.pdfrest.com"
4848
API_KEY_ENV_VAR = "PDFREST_API_KEY"
4949
API_KEY_HEADER_NAME = "Api-Key"
50-
DEFAULT_TIMEOUT_SECONDS = 10.0
50+
DEFAULT_GENERAL_TIMEOUT_SECONDS = 10.0
51+
DEFAULT_READ_TIMEOUT_SECONDS = 120.0
5152
FILE_UPLOAD_FIELD_NAME = "file"
5253
DEFAULT_FILE_INFO_CONCURRENCY = 8
5354

@@ -76,6 +77,13 @@
7677
DestinationPath = str | PathLike[str]
7778

7879

80+
def _default_timeout() -> httpx.Timeout:
81+
return httpx.Timeout(
82+
timeout=DEFAULT_GENERAL_TIMEOUT_SECONDS,
83+
read=DEFAULT_READ_TIMEOUT_SECONDS,
84+
)
85+
86+
7987
def _extract_uploaded_file_ids(payload: Any) -> list[str]:
8088
try:
8189
files_payload = payload["files"]
@@ -230,7 +238,7 @@ class _ClientConfig(BaseModel):
230238

231239
base_url: URL
232240
api_key: str | None = None
233-
timeout: TimeoutTypes = DEFAULT_TIMEOUT_SECONDS
241+
timeout: TimeoutTypes = Field(default_factory=_default_timeout)
234242
headers: dict[str, str] = Field(default_factory=dict)
235243

236244
model_config = ConfigDict(arbitrary_types_allowed=True)
@@ -273,7 +281,7 @@ def _validate_headers(cls, value: Any) -> dict[str, str]:
273281
@classmethod
274282
def _validate_timeout(cls, value: Any) -> TimeoutTypes:
275283
if value is None:
276-
return DEFAULT_TIMEOUT_SECONDS
284+
return _default_timeout()
277285
if isinstance(value, (int, float)):
278286
return float(value)
279287
if isinstance(value, httpx.Timeout):
@@ -317,7 +325,7 @@ def __init__(
317325
*,
318326
api_key: str | None = None,
319327
base_url: str | URL | None = None,
320-
timeout: TimeoutTypes = DEFAULT_TIMEOUT_SECONDS,
328+
timeout: TimeoutTypes | None = None,
321329
headers: AnyMapping | None = None,
322330
) -> None:
323331
raw_api_key = api_key if api_key is not None else os.getenv(API_KEY_ENV_VAR)
@@ -357,7 +365,7 @@ def __init__(
357365
self._config = _ClientConfig(
358366
base_url=resolved_base_url,
359367
api_key=resolved_api_key,
360-
timeout=timeout,
368+
timeout=timeout if timeout is not None else _default_timeout(),
361369
headers=default_headers,
362370
)
363371
except PdfRestConfigurationError:
@@ -541,7 +549,7 @@ def __init__(
541549
*,
542550
api_key: str | None = None,
543551
base_url: str | URL | None = None,
544-
timeout: TimeoutTypes = DEFAULT_TIMEOUT_SECONDS,
552+
timeout: TimeoutTypes | None = None,
545553
headers: AnyMapping | None = None,
546554
http_client: httpx.Client | None = None,
547555
transport: httpx.BaseTransport | None = None,
@@ -659,7 +667,7 @@ def __init__(
659667
*,
660668
api_key: str | None = None,
661669
base_url: str | URL | None = None,
662-
timeout: TimeoutTypes = DEFAULT_TIMEOUT_SECONDS,
670+
timeout: TimeoutTypes | None = None,
663671
headers: AnyMapping | None = None,
664672
http_client: httpx.AsyncClient | None = None,
665673
transport: httpx.AsyncBaseTransport | None = None,
@@ -1309,7 +1317,7 @@ def __init__(
13091317
*,
13101318
api_key: str | None = None,
13111319
base_url: str | URL | None = None,
1312-
timeout: TimeoutTypes = DEFAULT_TIMEOUT_SECONDS,
1320+
timeout: TimeoutTypes | None = None,
13131321
headers: AnyMapping | None = None,
13141322
http_client: httpx.Client | None = None,
13151323
transport: httpx.BaseTransport | None = None,
@@ -1617,7 +1625,7 @@ def __init__(
16171625
*,
16181626
api_key: str | None = None,
16191627
base_url: str | URL | None = None,
1620-
timeout: TimeoutTypes = DEFAULT_TIMEOUT_SECONDS,
1628+
timeout: TimeoutTypes | None = None,
16211629
headers: AnyMapping | None = None,
16221630
http_client: httpx.AsyncClient | None = None,
16231631
transport: httpx.AsyncBaseTransport | None = None,

0 commit comments

Comments
 (0)