Skip to content

Commit 3bb2827

Browse files
Cache base URL api-suffix detection in client base
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent da04ea1 commit 3bb2827

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

hyperbrowser/client/base.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ 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+
)
6771
self.transport = transport(config.api_key, headers=config.headers)
6872

6973
def _build_url(self, path: str) -> str:
@@ -78,16 +82,13 @@ def _build_url(self, path: str) -> str:
7882
if parsed_path.scheme:
7983
raise HyperbrowserError("path must be a relative API path")
8084
normalized_path = f"/{stripped_path.lstrip('/')}"
81-
parsed_base_url = urlparse(self.config.base_url)
82-
base_path = parsed_base_url.path.rstrip("/")
83-
base_has_api_suffix = base_path.endswith("/api")
8485

8586
if normalized_path == "/api" or normalized_path.startswith("/api/"):
86-
if base_has_api_suffix:
87+
if self._base_url_has_api_suffix:
8788
deduped_path = normalized_path[len("/api") :]
8889
return f"{self.config.base_url}{deduped_path}"
8990
return f"{self.config.base_url}{normalized_path}"
9091

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

0 commit comments

Comments
 (0)