Skip to content

Commit 9493d97

Browse files
Recompute base URL api-suffix per URL build call
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 3bb2827 commit 9493d97

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

hyperbrowser/client/base.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ def __init__(
6464
raise HyperbrowserError("API key must be provided")
6565

6666
self.config = config
67-
parsed_base_url = urlparse(self.config.base_url)
68-
self._base_url_has_api_suffix = parsed_base_url.path.rstrip("/").endswith(
69-
"/api"
70-
)
7167
self.transport = transport(config.api_key, headers=config.headers)
7268

7369
def _build_url(self, path: str) -> str:
@@ -82,13 +78,16 @@ def _build_url(self, path: str) -> str:
8278
if parsed_path.scheme:
8379
raise HyperbrowserError("path must be a relative API path")
8480
normalized_path = f"/{stripped_path.lstrip('/')}"
81+
base_has_api_suffix = (
82+
urlparse(self.config.base_url).path.rstrip("/").endswith("/api")
83+
)
8584

8685
if normalized_path == "/api" or normalized_path.startswith("/api/"):
87-
if self._base_url_has_api_suffix:
86+
if base_has_api_suffix:
8887
deduped_path = normalized_path[len("/api") :]
8988
return f"{self.config.base_url}{deduped_path}"
9089
return f"{self.config.base_url}{normalized_path}"
9190

92-
if self._base_url_has_api_suffix:
91+
if base_has_api_suffix:
9392
return f"{self.config.base_url}{normalized_path}"
9493
return f"{self.config.base_url}/api{normalized_path}"

tests/test_url_building.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ def test_client_build_url_handles_nested_api_base_paths():
7171
client.close()
7272

7373

74+
def test_client_build_url_reflects_runtime_base_url_changes():
75+
client = Hyperbrowser(config=ClientConfig(api_key="test-key"))
76+
try:
77+
client.config.base_url = "https://example.local/api"
78+
assert client._build_url("/session") == "https://example.local/api/session"
79+
finally:
80+
client.close()
81+
82+
7483
def test_client_build_url_rejects_empty_or_non_string_paths():
7584
client = Hyperbrowser(config=ClientConfig(api_key="test-key"))
7685
try:

0 commit comments

Comments
 (0)