Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions infisical_sdk/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from .infisical_requests import InfisicalRequests

from infisical_sdk.resources import Auth
Expand All @@ -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)
Expand Down
17 changes: 9 additions & 8 deletions infisical_sdk/infisical_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions tests/test_infisical_requests.py
Original file line number Diff line number Diff line change
@@ -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({})
Expand Down Expand Up @@ -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