Skip to content

Commit 5751fb6

Browse files
authored
Merge pull request #153 from release-engineering/add_timeout_session
Add timeout to StarmapSession
2 parents 18a574f + fd87264 commit 5751fb6

3 files changed

Lines changed: 15 additions & 3 deletions

File tree

starmap_client/session.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
import re
44
from abc import ABC, abstractmethod
5-
from typing import Any, Dict, Union
5+
from typing import Any, Dict, Tuple, Union
66

77
import requests
88
import 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."""

tests/test_session.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def _assert_requested_with(self, method: str, path: str, **kwargs: Any) -> None:
2525
url=f"{self.starmap_url}/api/{self.starmap_api_version}/{path}",
2626
headers=headers,
2727
verify=True,
28+
timeout=self.session.timeout,
2829
**kwargs,
2930
)
3031

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ deps =
6666
black
6767
isort
6868
commands =
69-
black -S -t py10 -l 100 starmap_client tests
69+
black -S -t py310 -l 100 starmap_client tests
7070
isort -l 100 --profile black starmap_client tests
7171

7272
[testenv:coverage]

0 commit comments

Comments
 (0)