diff --git a/infisical_sdk/client.py b/infisical_sdk/client.py index 9889d3e..992150e 100644 --- a/infisical_sdk/client.py +++ b/infisical_sdk/client.py @@ -1,3 +1,5 @@ +from typing import Optional + from .infisical_requests import InfisicalRequests from infisical_sdk.resources import Auth @@ -10,19 +12,20 @@ from infisical_sdk.util import SecretsCache class InfisicalSDKClient: - def __init__(self, host: str, token: str = None, cache_ttl: int = 60): + def __init__(self, host: str, token: str = None, cache_ttl: int = 60, timeout: Optional[float] = 30): """ Initialize the Infisical SDK client. :param str host: The host URL for your Infisical instance. Will default to `https://app.infisical.com` if not specified. :param str token: The authentication token for the client. If not specified, you can use the `auth` methods to authenticate. :param int cache_ttl: The time to live for the secrets cache. This is the number of seconds that secrets fetched from the API will be cached for. Set to `None` to disable caching. Defaults to `60` seconds. + :param float timeout: The number of seconds to wait for the connection and for the response, applied to each attempt. Set to `None` to disable the timeout. Defaults to `30` seconds. """ self.host = host self.access_token = token - self.api = InfisicalRequests(host=host, token=token) + self.api = InfisicalRequests(host=host, token=token, timeout=timeout) self.cache = SecretsCache(cache_ttl) self.auth = Auth(self.api, self.set_token) self.secrets = V3RawSecrets(self.api, self.cache) diff --git a/infisical_sdk/infisical_requests.py b/infisical_sdk/infisical_requests.py index bbb29fb..c03d94a 100644 --- a/infisical_sdk/infisical_requests.py +++ b/infisical_sdk/infisical_requests.py @@ -110,9 +110,10 @@ def wrapper(*args, **kwargs): class InfisicalRequests: - def __init__(self, host: str, token: Optional[str] = None): + def __init__(self, host: str, token: Optional[str] = None, timeout: Optional[float] = 30): self.host = host.rstrip("/") self.session = requests.Session() + self.timeout = timeout # Set common headers self.session.headers.update({ @@ -168,7 +169,7 @@ def get( model: model class to parse response into params: Optional query parameters """ - response = self.session.get(self._build_url(path), params=params) + response = self.session.get(self._build_url(path), params=params, timeout=self.timeout) data = self._handle_response(response) parsed_data = model.from_dict(data) if hasattr(model, 'from_dict') else data @@ -194,7 +195,7 @@ def post( # Filter out None values json = {k: v for k, v in json.items() if v is not None} - response = self.session.post(self._build_url(path), json=json, params=params) + response = self.session.post(self._build_url(path), json=json, params=params, timeout=self.timeout) data = self._handle_response(response) parsed_data = model.from_dict(data) if hasattr(model, 'from_dict') else data @@ -221,7 +222,7 @@ def post_once( # Filter out None values json = {k: v for k, v in json.items() if v is not None} - response = self.session.post(self._build_url(path), json=json, params=params) + response = self.session.post(self._build_url(path), json=json, params=params, timeout=self.timeout) data = self._handle_response(response) parsed_data = model.from_dict(data) if hasattr(model, 'from_dict') else data @@ -247,7 +248,7 @@ def patch( # Filter out None values json = {k: v for k, v in json.items() if v is not None or k in (nullable_keys or [])} - response = self.session.patch(self._build_url(path), json=json) + response = self.session.patch(self._build_url(path), json=json, timeout=self.timeout) data = self._handle_response(response) parsed_data = model.from_dict(data) if hasattr(model, 'from_dict') else data @@ -273,7 +274,7 @@ def put( # Filter out None values json = {k: v for k, v in json.items() if v is not None} - response = self.session.put(self._build_url(path), json=json, params=params) + response = self.session.put(self._build_url(path), json=json, params=params, timeout=self.timeout) data = self._handle_response(response) parsed_data = model.from_dict(data) if hasattr(model, 'from_dict') else data @@ -299,7 +300,7 @@ def delete( # Filter out None values json = {k: v for k, v in json.items() if v is not None} - response = self.session.delete(self._build_url(path), json=json, params=params) + response = self.session.delete(self._build_url(path), json=json, params=params, timeout=self.timeout) data = self._handle_response(response) parsed_data = model.from_dict(data) if hasattr(model, 'from_dict') else data @@ -325,7 +326,7 @@ def delete_once( # Filter out None values json = {k: v for k, v in json.items() if v is not None} - response = self.session.delete(self._build_url(path), json=json, params=params) + response = self.session.delete(self._build_url(path), json=json, params=params, timeout=self.timeout) data = self._handle_response(response) parsed_data = model.from_dict(data) if hasattr(model, 'from_dict') else data diff --git a/tests/test_infisical_requests.py b/tests/test_infisical_requests.py index dfa747e..3025ce4 100644 --- a/tests/test_infisical_requests.py +++ b/tests/test_infisical_requests.py @@ -1,6 +1,11 @@ +from unittest import mock + import pytest import requests +from infisical_sdk import InfisicalSDKClient +from infisical_sdk.infisical_requests import InfisicalRequests + def test_patch_leaves_out_none_values(api, json_response): api.session.patch.return_value = json_response({}) @@ -54,3 +59,24 @@ def test_post_once_does_not_retry_once_the_request_may_have_reached_the_server(a with pytest.raises(error): api.post_once(path="/api/v1/example", model=dict) assert api.session.post.call_count == 1 + + +@pytest.mark.parametrize("method", ["get", "post", "post_once", "patch", "put", "delete", "delete_once"]) +def test_request_carries_default_timeout(api, json_response, method): + send = getattr(api.session, method.replace("_once", "")) + send.return_value = json_response({}) + getattr(api, method)(path="/api/v1/example", model=dict) + assert send.call_args.kwargs["timeout"] == 30 + + +def test_request_carries_configured_timeout(json_response): + api = InfisicalRequests(host="https://infisical.example.com", timeout=5) + api.session = mock.Mock() + api.session.get.return_value = json_response({}) + api.get(path="/api/v1/example", model=dict) + assert api.session.get.call_args.kwargs["timeout"] == 5 + + +def test_client_timeout_reaches_the_request(): + with InfisicalSDKClient(host="https://infisical.example.com", timeout=5) as client: + assert client.api.timeout == 5