Skip to content

Commit c4aea1e

Browse files
committed
Configure baseline timeouts for requests
These represent a good baseline for 99.99% of cases. The remainder can customize the `Session` argument.
1 parent bb6597c commit c4aea1e

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
### Fixes
1515

1616
- Missing optional `next_cursor` attribute in `CompletedItems` object
17+
- API requests configure appropriate timeouts to avoid connections hanging
1718

1819
## [2.1.7] - 2024-08-13
1920

todoist_api_python/http_requests.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,25 @@
1212

1313
Json = dict[str, "Json"] | list["Json"] | str | int | float | bool | None
1414

15+
# Timeouts for requests.
16+
#
17+
# 10 seconds for connecting is a recurring default and adheres to python-requests's
18+
# recommendation of picking a value slightly larger than a multiple of 3.
19+
#
20+
# 60 seconds for reading aligns with Todoist's own internal timeout. All requests are
21+
# forcefully terminated after this time, so there is no point waiting any longer.
22+
TIMEOUT = (10, 60)
23+
1524

1625
def get(
1726
session: Session,
1827
url: str,
1928
token: str | None = None,
2029
params: dict[str, Any] | None = None,
2130
) -> Json | bool:
22-
response = session.get(url, params=params, headers=create_headers(token=token))
31+
response = session.get(
32+
url, params=params, headers=create_headers(token=token), timeout=TIMEOUT
33+
)
2334

2435
if response.status_code == codes.OK:
2536
return response.json()
@@ -41,9 +52,7 @@ def post(
4152
)
4253

4354
response = session.post(
44-
url,
45-
headers=headers,
46-
data=json.dumps(data) if data else None,
55+
url, headers=headers, data=json.dumps(data) if data else None, timeout=TIMEOUT
4756
)
4857

4958
if response.status_code == codes.OK:
@@ -63,10 +72,7 @@ def delete(
6372

6473
headers = create_headers(token=token, request_id=request_id)
6574

66-
response = session.delete(
67-
url,
68-
headers=headers,
69-
)
75+
response = session.delete(url, headers=headers, timeout=TIMEOUT)
7076

7177
response.raise_for_status()
7278
return response.ok

0 commit comments

Comments
 (0)