diff --git a/CHANGELOG.md b/CHANGELOG.md index 8529b10..97ea249 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * Removed support for file system credentials caching. * S3 download URLs are now validated. +* Improved request timeout handling. ## 4.0.0 - 2026-01-22 diff --git a/okdata/sdk/auth/credentials/password_grant.py b/okdata/sdk/auth/credentials/password_grant.py index 4f0740e..f6a36b7 100644 --- a/okdata/sdk/auth/credentials/password_grant.py +++ b/okdata/sdk/auth/credentials/password_grant.py @@ -35,5 +35,5 @@ def refresh_token(self, refresh_token): def new_token(self): payload = {"username": self.username, "password": self.password} - response = requests.post(url=self.token_service_url, json=payload) + response = requests.post(url=self.token_service_url, json=payload, timeout=10) return response.json() diff --git a/okdata/sdk/data/download.py b/okdata/sdk/data/download.py index 3102631..0c2581f 100644 --- a/okdata/sdk/data/download.py +++ b/okdata/sdk/data/download.py @@ -41,7 +41,7 @@ def download(self, dataset_id, version, edition, output_path, retries=0): if not file_url.startswith(base_url): raise DownloadURLAssertionError(file_url, base_url) - file_content_response = requests.get(file_url, stream=True) + file_content_response = requests.get(file_url, stream=True, timeout=10) file_content_response.raise_for_status() write_file_content(file_name, output_path, file_content_response.raw.read()) diff --git a/okdata/sdk/sdk.py b/okdata/sdk/sdk.py index ef66d58..0d4648a 100644 --- a/okdata/sdk/sdk.py +++ b/okdata/sdk/sdk.py @@ -9,6 +9,14 @@ log = logging.getLogger() +class TimeoutHTTPAdapter(HTTPAdapter): + """HTTP adapter with a default timeout.""" + + def send(self, request, *args, **kwargs): + kwargs["timeout"] = 10 # Defualt timeout (seconds) + return super().send(request, *args, **kwargs) + + class SDK: def __init__(self, config=None, auth=None, env=None): self.config = config @@ -72,7 +80,7 @@ def prepared_request_with_retries(retries): "TRACE", ], ) - adapter = HTTPAdapter(max_retries=retry_strategy) + adapter = TimeoutHTTPAdapter(max_retries=retry_strategy) session = requests.Session() session.mount("https://", adapter) session.mount("http://", adapter)