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")