Skip to content

Commit

Permalink
Add request_timeout to APIClient
Browse files Browse the repository at this point in the history
Add a timeout to all HTTP requests as otherwise they may hang
indefinitely if the API is unresponsive.
  • Loading branch information
s-hamann committed Jun 24, 2024
1 parent a22f040 commit 4efea61
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion desec.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ class APIClient:
Args:
token: API authorization token
request_timeout: HTTP request timeout in seconds. Note that the timeout is applied
to individual HTTP requests and the methods of this class may make multiple
requests. Set to `None` to disable.
retry_limit: Number of retries when hitting the API's rate limit.
Set to 0 to disable.
logger: Logger instance to send HTTP debug information to. Defaults to the named
Expand All @@ -406,10 +409,12 @@ class APIClient:
def __init__(
self,
token: str,
request_timeout: int | None = 15,
retry_limit: int = 3,
logger: logging.Logger = logging.getLogger("desec.client"),
):
self._token_auth = TokenAuth(token)
self._request_timeout = request_timeout
self._retry_limit = retry_limit
self.logger = logger
"Logger instance to send HTTP debug information to."
Expand Down Expand Up @@ -501,6 +506,8 @@ def query(
The request hit the API's rate limit. Retries up to the configured limit
were made, but also hit the rate limit.
APIError: The API returned an unexpected error.
requests.Timeout: The API failed to reply to an HTTP request within the time
limit.
"""
if method == "GET" or method == "DELETE":
Expand All @@ -527,7 +534,12 @@ def query(
extra=dict(method=method, url=url, params=params, body=body),
)
r = requests.request(
method, next_url, auth=self._token_auth, params=params, json=body
method,
next_url,
auth=self._token_auth,
params=params,
json=body,
timeout=self._request_timeout,
)
self.logger.debug(
f"Response: {r.status_code} for {method} {url}",
Expand Down

0 comments on commit 4efea61

Please sign in to comment.