Skip to content

Commit 3d6e0ef

Browse files
Normalize and validate client config values
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 0e6127a commit 3d6e0ef

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

hyperbrowser/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ class ClientConfig:
1313
base_url: str = "https://api.hyperbrowser.ai"
1414
headers: Optional[Dict[str, str]] = None
1515

16+
def __post_init__(self) -> None:
17+
if not isinstance(self.api_key, str):
18+
raise HyperbrowserError("api_key must be a string")
19+
if not isinstance(self.base_url, str):
20+
raise HyperbrowserError("base_url must be a string")
21+
self.api_key = self.api_key.strip()
22+
self.base_url = self.base_url.strip().rstrip("/")
23+
1624
@classmethod
1725
def from_env(cls) -> "ClientConfig":
1826
api_key = os.environ.get("HYPERBROWSER_API_KEY")

tests/test_config.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,18 @@ def test_client_config_from_env_reads_api_key_and_base_url(monkeypatch):
2626

2727
assert config.api_key == "test-key"
2828
assert config.base_url == "https://example.local"
29+
30+
31+
def test_client_config_normalizes_whitespace_and_trailing_slash():
32+
config = ClientConfig(api_key=" test-key ", base_url=" https://example.local/ ")
33+
34+
assert config.api_key == "test-key"
35+
assert config.base_url == "https://example.local"
36+
37+
38+
def test_client_config_rejects_non_string_values():
39+
with pytest.raises(HyperbrowserError, match="api_key must be a string"):
40+
ClientConfig(api_key=None) # type: ignore[arg-type]
41+
42+
with pytest.raises(HyperbrowserError, match="base_url must be a string"):
43+
ClientConfig(api_key="test-key", base_url=None) # type: ignore[arg-type]

0 commit comments

Comments
 (0)