Skip to content

Commit b53ba04

Browse files
author
redjax
committed
Add timeout param to http controller
1 parent 16fd18a commit b53ba04

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

libs/http-lib/src/http_lib/controllers.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def get_http_controller(
6262
cacheable_status_codes: list[int] | None = None,
6363
cache_allow_heuristics: bool = True,
6464
cache_allow_stale: bool = False,
65+
timeout: int | float = 30.0,
6566
) -> HttpxController:
6667
"""Return an initialized HttpxController class object.
6768
@@ -92,6 +93,7 @@ def get_http_controller(
9293
cache_allow_heuristics (bool): (default: True) Use heuristics to match objects in cache, improves performance &
9394
reliability of caching new objects.
9495
cache_allow_stale (bool): (default: False) When `True`, allow stale/expired responses from cache.
96+
timeout (int | float): (default: 30.0) Amount of time, in seconds, to wait for a response.
9597
9698
Returns:
9799
(HttpxController): Initialized HttpxController object to use for requests.
@@ -120,6 +122,7 @@ def get_http_controller(
120122
cacheable_status_codes=cacheable_status_codes,
121123
cache_allow_heuristics=cache_allow_heuristics,
122124
cache_allow_stale=cache_allow_stale,
125+
timeout=timeout,
123126
)
124127

125128
return http_ctl
@@ -196,6 +199,7 @@ class HttpxController(AbstractContextManager):
196199
cache_allow_heuristics (bool): (default: True) Use heuristics to match objects in cache, improves performance &
197200
reliability of caching new objects.
198201
cache_allow_stale (bool): (default: False) When `True`, allow stale/expired responses from cache.
202+
timeout (int | float): (default: 30.0) Amount of time, in seconds, to wait for a response.
199203
"""
200204

201205
def __init__(
@@ -221,11 +225,14 @@ def __init__(
221225
cacheable_status_codes: list[int] | None = [200, 201, 202, 301, 308],
222226
cache_allow_heuristics: bool = True,
223227
cache_allow_stale: bool = False,
228+
timeout: int | float = 30.0,
224229
) -> None:
225230
self.use_cache: bool = use_cache
226231
self.force_cache: bool = force_cache
227232
self.follow_redirects: bool = follow_redirects
228-
self.cache_type: str | None = cache_type.lower() if (cache_type and isinstance(cache_type, str)) else None
233+
self.cache_type: str | None = (
234+
cache_type.lower() if (cache_type and isinstance(cache_type, str)) else None
235+
)
229236
self.cache_file_dir: str | None = cache_file_dir
230237
self.cache_db_file: str = cache_db_file
231238
self.cache_ttl: int | None = cache_ttl
@@ -234,6 +241,7 @@ def __init__(
234241
self.cacheable_status_codes: list[int] | None = cacheable_status_codes
235242
self.cache_allow_heuristics: bool = cache_allow_heuristics
236243
self.cache_allow_stale: bool = cache_allow_stale
244+
self.timeout: int | float = timeout
237245

238246
## Placeholder for initialized httpx.Client
239247
self.client: httpx.Client | None = None
@@ -287,7 +295,7 @@ def _get_cache(self) -> t.Union[hishel.SQLiteStorage, hishel.FileStorage] | None
287295
"""Initialize hishel cache storage."""
288296
if not self.use_cache:
289297
return None
290-
298+
291299
match self.cache_type:
292300
case None:
293301
return None
@@ -308,7 +316,7 @@ def _get_cache(self) -> t.Union[hishel.SQLiteStorage, hishel.FileStorage] | None
308316
log.error(f"Unrecognized cache type: {self.cache_type}")
309317

310318
return None
311-
319+
312320
return _cache
313321

314322
def _get_cache_controller(self) -> hishel.Controller:
@@ -359,7 +367,9 @@ def _get_client(self) -> httpx.Client:
359367
if self.use_cache:
360368
transport: hishel.CacheTransport | None = self.cache_transport
361369
client = httpx.Client(
362-
transport=transport, follow_redirects=self.follow_redirects
370+
transport=transport,
371+
follow_redirects=self.follow_redirects,
372+
timeout=self.timeout,
363373
)
364374

365375
return client
@@ -387,7 +397,7 @@ def send_request(
387397
"""
388398
try:
389399
res: httpx.Response = self.client.send(request, stream=stream, auth=auth)
390-
400+
391401
return res
392402
except Exception as exc:
393403
msg = f"({type(exc)}) Error sending request. Details: {exc}"

0 commit comments

Comments
 (0)