Skip to content

Commit 77743ac

Browse files
committed
Concatenate paths in _get_base_url_with_base_path rather than use urljoin
The current implementation treats the base_path as a relative URL reference and resolves it against the base URL, so it ignores any path segments already set in base_url. We have customers which use a gateway in front of our API and have a setup where the base url contains a path segment
1 parent 197a140 commit 77743ac

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

cognite/client/_api_client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
cast,
2020
overload,
2121
)
22-
from urllib.parse import urljoin
2322

2423
import requests.utils
2524
from requests import Response
@@ -301,7 +300,7 @@ def _get_base_url_with_base_path(self) -> str:
301300
base_path = ""
302301
if self._api_version:
303302
base_path = f"/api/{self._api_version}/projects/{self._config.project}"
304-
return urljoin(self._config.base_url, base_path)
303+
return f"{self._config.base_url.rstrip('/')}/{base_path.lstrip('/')}"
305304

306305
def _is_retryable(self, method: str, path: str) -> bool:
307306
valid_methods = ["GET", "POST", "PUT", "DELETE", "PATCH"]

tests/tests_unit/test_api_client.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,21 @@ def test__do_request_raises_unmodified_exception(self, api_client_with_token):
199199
exc_msg = exc_info.value.args[0]
200200
assert "contain NaN(s) or +/- Inf!" not in exc_msg
201201

202+
def test_client_with_base_url_including_path_segments(self, cognite_client):
203+
client = APIClient(
204+
ClientConfig(
205+
client_name="any",
206+
project="test-project",
207+
base_url="https://bla.bla.com/extra",
208+
max_workers=1,
209+
headers={"x-cdp-app": "python-sdk-integration-tests"},
210+
credentials=Token(lambda: "abc"),
211+
),
212+
api_version="v1",
213+
cognite_client=cognite_client,
214+
)
215+
assert client._get_base_url_with_base_path() == "https://bla.bla.com/extra/api/v1/projects/test-project"
216+
202217

203218
class SomeUpdate(CogniteUpdate):
204219
@property

0 commit comments

Comments
 (0)