Skip to content

Commit 210719a

Browse files
committed
adds better bath handling
1 parent 90f33a9 commit 210719a

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

netboxlabs/diode/sdk/client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ def __init__(
113113
self._client_id = _get_required_config_value(_CLIENT_ID_ENVVAR_NAME, client_id)
114114
self._client_secret = _get_required_config_value(_CLIENT_SECRET_ENVVAR_NAME, client_secret)
115115

116-
117116
self._metadata = (
118117
("platform", self._platform),
119118
("python-version", self._python_version),
@@ -289,7 +288,7 @@ def authenticate(self) -> str:
289288
"client_secret": self._client_secret,
290289
}
291290
)
292-
url = f"{self._path}/auth/token" if self._path and self._path != "/" else "/auth/token"
291+
url = self._get_auth_url()
293292
conn.request("POST", url, data, headers)
294293
response = conn.getresponse()
295294
if response.status != 200:
@@ -302,6 +301,12 @@ def authenticate(self) -> str:
302301
_LOGGER.debug(f"Access token obtained for client {self._client_id}")
303302
return access_token
304303

304+
def _get_auth_url(self) -> str:
305+
"""Construct the authentication URL, handling trailing slashes in the path."""
306+
# Ensure the path does not have trailing slashes
307+
path = self._path.rstrip('/') if self._path else ''
308+
return f"{path}/auth/token"
309+
305310

306311
class _ClientCallDetails(
307312
collections.namedtuple(

tests/test_client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,14 @@ def test_diode_authentication_failure(mock_diode_authentication):
588588
auth.authenticate()
589589
assert "Failed to obtain access token" in str(excinfo.value)
590590

591-
@pytest.mark.parametrize("path", ["/diode", "", None])
591+
@pytest.mark.parametrize("path", [
592+
"/diode",
593+
"",
594+
None,
595+
"/diode/",
596+
"diode",
597+
"diode/",
598+
])
592599
def test_diode_authentication_url_with_path(mock_diode_authentication, path):
593600
"""Test that the authentication URL is correctly formatted with a path."""
594601
auth = _DiodeAuthentication(
@@ -603,5 +610,5 @@ def test_diode_authentication_url_with_path(mock_diode_authentication, path):
603610
mock_conn_instance.getresponse.return_value.status = 200
604611
mock_conn_instance.getresponse.return_value.read.return_value = json.dumps({"access_token": "mocked_token"}).encode()
605612
auth.authenticate()
606-
mock_conn_instance.request.assert_called_once_with("POST", f"{path or ''}/auth/token", mock.ANY, mock.ANY)
613+
mock_conn_instance.request.assert_called_once_with("POST", f"{(path or '').rstrip('/')}/auth/token", mock.ANY, mock.ANY)
607614

0 commit comments

Comments
 (0)