From bf5133f4aaa7a5dc1504042fff1247c8a71438e4 Mon Sep 17 00:00:00 2001 From: Polliog <40077351+Polliog@users.noreply.github.com> Date: Fri, 17 Jul 2026 13:35:44 +0200 Subject: [PATCH] fix: don't require api_url/api_key in local mode The api_url check ran unconditionally and before the local_mode logic, so ClientOptions(local_mode=True) raised "api_url must be provided". That defeats the purpose of local mode: a client that never reaches the network had to be handed an endpoint it would never use. Gate both endpoint checks on local_mode being off. Also drop the duplicate guard in BaseClient.__init__, which raised RuntimeError for the missing-key case that __post_init__ already rejects with ValueError. --- logtide_sdk/_base_client.py | 5 ----- logtide_sdk/models.py | 11 ++++++----- tests/test_models.py | 10 ++++++++++ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/logtide_sdk/_base_client.py b/logtide_sdk/_base_client.py index 1452744..2e5405a 100644 --- a/logtide_sdk/_base_client.py +++ b/logtide_sdk/_base_client.py @@ -14,11 +14,6 @@ class BaseClient: """ def __init__(self, options: ClientOptions) -> None: - if not options.local_mode and not options.api_key: - raise RuntimeError( - f"Cannot instantiate {self.__class__.__name__} without api_key with disabled local_mode" - ) - self.options = options self._payload_limits = options.payload_limits or PayloadLimitsOptions() diff --git a/logtide_sdk/models.py b/logtide_sdk/models.py index 3678d9b..3479a81 100644 --- a/logtide_sdk/models.py +++ b/logtide_sdk/models.py @@ -87,9 +87,6 @@ def __post_init__(self) -> None: self.api_key = self.api_key if self.api_key else dsn_parts.api_key self.api_url = self.api_url if self.api_url else dsn_parts.api_url - if not self.api_url: - raise ValueError("api_url must be provided") - if not 0.0 <= self.sample_rate <= 1.0: raise ValueError("sample_rate must be between 0.0 and 1.0") @@ -102,8 +99,12 @@ def __post_init__(self) -> None: "Local mode cannot be positive value other than True or 'if_unset_api_key'" ) - if not self.local_mode and not self.api_key: - raise ValueError("api_key must be provided to options, unless local_mode configured") + # Local mode never reaches the network, so it needs no endpoint configured. + if not self.local_mode: + if not self.api_url: + raise ValueError("api_url must be provided") + if not self.api_key: + raise ValueError("api_key must be provided to options, unless local_mode configured") @dataclass diff --git a/tests/test_models.py b/tests/test_models.py index 4895fb0..2fde95b 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -13,6 +13,16 @@ def test_client_options_ignores_unset_api_key_if_set_unset_or_local_mode( ClientOptions(api_url="https://any.apiurl.dev", api_key=api_key, local_mode=local_mode) +@pytest.mark.parametrize("local_mode", ["if_unset_api_key", True]) +def test_client_options_needs_no_url_or_key_in_local_mode( + local_mode: Literal["if_unset_api_key"] | bool, +) -> None: + opts = ClientOptions(local_mode=local_mode) + + assert opts.api_url is None + assert opts.api_key is None + + def test_client_options_raises_no_api_key_and_without_local_mode(): with pytest.raises(ValueError, match="api_key must be provided"): ClientOptions(api_url="https://somenetwork:8080")