Skip to content

Commit f48e5ca

Browse files
authored
Merge pull request #124 from ImagingDataCommons/feature/timeout
Add timeout to all requests
2 parents 975e4dd + 6e9fccd commit f48e5ca

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
@@ -226,7 +226,8 @@ def __init__(
226226
headers: Optional[Dict[str, str]] = None,
227227
callback: Optional[Callable] = None,
228228
chunk_size: int = 10**6,
229-
permissive_uid: bool = False
229+
permissive_uid: bool = False,
230+
timeout: float | tuple = 20,
230231
) -> None:
231232
"""Instatiate client.
232233
@@ -269,6 +270,10 @@ def __init__(
269270
this flag is **not** recommended, since non-conformant UIDs may
270271
lead to unexpected errors downstream, e.g., rejection by a DICOMweb
271272
server, etc.
273+
timeout: float | tuple, optional
274+
Timeout parameter used for all requests in the format used by the
275+
requests library. May provide a single number or a tuple of
276+
(connect timeout, read timeout), using seconds in both cases.
272277
273278
Warning
274279
-------
@@ -306,6 +311,7 @@ def __init__(
306311
'Argument "delete_url_prefix" must not be a zero length string.'
307312
)
308313
self.delete_url_prefix = delete_url_prefix
314+
self._timeout = timeout
309315

310316
# This regular expression extracts the scheme and host name from the URL
311317
# and optionally the port number and prefix:
@@ -558,7 +564,12 @@ def _invoke_get_request(
558564
# encoding. The iter_content() method can be used to iterate over
559565
# chunks. If stream is not set, iter_content() will return the
560566
# full payload at once.
561-
return self._session.get(url=url, headers=headers, stream=stream)
567+
return self._session.get(
568+
url=url,
569+
headers=headers,
570+
stream=stream,
571+
timeout=self._timeout,
572+
)
562573

563574
if headers is None:
564575
headers = {}
@@ -1543,7 +1554,12 @@ def _invoke_post_request(
15431554
headers: Optional[Dict[str, str]] = None
15441555
) -> requests.models.Response:
15451556
logger.debug(f'POST: {url} {headers}')
1546-
return self._session.post(url, data=data, headers=headers)
1557+
return self._session.post(
1558+
url,
1559+
data=data,
1560+
headers=headers,
1561+
timeout=self._timeout,
1562+
)
15471563

15481564
if len(data) > self._chunk_size:
15491565
logger.info('store data in chunks using chunked transfer encoding')
@@ -1644,7 +1660,7 @@ def _http_delete(self, url: str):
16441660
stop_max_attempt_number=self._max_attempts
16451661
)
16461662
def _invoke_delete_request(url: str) -> requests.models.Response:
1647-
return self._session.delete(url)
1663+
return self._session.delete(url, timeout=self._timeout)
16481664

16491665
response = _invoke_delete_request(url)
16501666
if response.status_code == HTTPStatus.METHOD_NOT_ALLOWED:

0 commit comments

Comments
 (0)