22import logging
33import re
44from abc import ABC , abstractmethod
5- from typing import Any , Dict , Union
5+ from typing import Any , Dict , Tuple , Union
66
77import requests
88import requests_mock
@@ -36,6 +36,7 @@ def __init__(
3636 api_version : str ,
3737 retries : int = 3 ,
3838 backoff_factor : float = 2.0 ,
39+ timeout : float | Tuple [float , float ] = 10.0 ,
3940 ):
4041 """
4142 Create the StarmapSession object.
@@ -49,10 +50,15 @@ def __init__(
4950 The number of request retries on failure
5051 backoff_factor (float, optional)
5152 The backoff factor to apply between attempts after the second try
53+ timeout (float | tuple[float, float], optional)
54+ The timeout in seconds for the request. If a tuple is provided, the first value
55+ is the connection timeout, and the second is the read timeout. Defaults to
56+ 10 seconds for both connection and read.
5257 """
5358 super (StarmapSession , self ).__init__ ()
5459 self .url = url
5560 self .api_version = api_version
61+ self .timeout = timeout
5662 self .session = requests .Session ()
5763 retry = Retry (
5864 total = retries ,
@@ -75,7 +81,12 @@ def _request(self, method: str, path: str, **kwargs: Any) -> requests.Response:
7581 url_elements = [self .url , f"/api/{ self .api_version } " , path ]
7682 url = "/" .join (arg .strip ("/" ) for arg in url_elements )
7783
78- return self .session .request (method , url = url , headers = headers , verify = self .verify , ** kwargs )
84+ # If timeout is not provided, use the default timeout
85+ timeout = kwargs .pop ("timeout" , self .timeout )
86+
87+ return self .session .request (
88+ method , url = url , headers = headers , verify = self .verify , timeout = timeout , ** kwargs
89+ )
7990
8091 def get (self , path : str , ** kwargs : Any ) -> requests .Response :
8192 """Perform a GET request on StArMap."""
0 commit comments