Skip to content

Commit bf66cda

Browse files
committed
Add timeout to all requests
1 parent bcd7719 commit bf66cda

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

src/dicomweb_client/web.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ def __init__(
205205
headers: Optional[Dict[str, str]] = None,
206206
callback: Optional[Callable] = None,
207207
chunk_size: int = 10**6,
208-
permissive_uid: bool = False
208+
permissive_uid: bool = False,
209+
timeout: float | tuple = 20,
209210
) -> None:
210211
"""Instatiate client.
211212
@@ -248,6 +249,10 @@ def __init__(
248249
this flag is **not** recommended, since non-conformant UIDs may
249250
lead to unexpected errors downstream, e.g., rejection by a DICOMweb
250251
server, etc.
252+
timeout: float | tuple, optional
253+
Timeout parameter used for all requests in the format used by the
254+
requests library. May provide a single number or a tuple of
255+
(connect timeout, read timeout).
251256
252257
Warning
253258
-------
@@ -285,6 +290,7 @@ def __init__(
285290
'Argument "delete_url_prefix" must not be a zero length string.'
286291
)
287292
self.delete_url_prefix = delete_url_prefix
293+
self._timeout = timeout
288294

289295
# This regular expression extracts the scheme and host name from the URL
290296
# and optionally the port number and prefix:
@@ -537,7 +543,12 @@ def _invoke_get_request(
537543
# encoding. The iter_content() method can be used to iterate over
538544
# chunks. If stream is not set, iter_content() will return the
539545
# full payload at once.
540-
return self._session.get(url=url, headers=headers, stream=stream)
546+
return self._session.get(
547+
url=url,
548+
headers=headers,
549+
stream=stream,
550+
timeout=self._timeout,
551+
)
541552

542553
if headers is None:
543554
headers = {}
@@ -1522,7 +1533,12 @@ def _invoke_post_request(
15221533
headers: Optional[Dict[str, str]] = None
15231534
) -> requests.models.Response:
15241535
logger.debug(f'POST: {url} {headers}')
1525-
return self._session.post(url, data=data, headers=headers)
1536+
return self._session.post(
1537+
url,
1538+
data=data,
1539+
headers=headers,
1540+
timeout=self._timeout,
1541+
)
15261542

15271543
if len(data) > self._chunk_size:
15281544
logger.info('store data in chunks using chunked transfer encoding')
@@ -1623,7 +1639,7 @@ def _http_delete(self, url: str):
16231639
stop_max_attempt_number=self._max_attempts
16241640
)
16251641
def _invoke_delete_request(url: str) -> requests.models.Response:
1626-
return self._session.delete(url)
1642+
return self._session.delete(url, timeout=self._timeout)
16271643

16281644
response = _invoke_delete_request(url)
16291645
if response.status_code == HTTPStatus.METHOD_NOT_ALLOWED:

0 commit comments

Comments
 (0)