Skip to content

Commit 12b38cb

Browse files
committed
Change Record._endpoint_from_url() to remove api.base_url and then determine app and endpoint
1 parent 57755cb commit 12b38cb

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

pynetbox/core/response.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,11 @@ def list_parser(list_item):
280280
setattr(self, k, v)
281281

282282
def _endpoint_from_url(self, url):
283-
app, name = urlsplit(url).path.split("/")[-4:-2]
283+
# Remove the base URL from the beginning
284+
url = url[len(self.api.base_url):]
285+
if url.startswith("/"):
286+
url = url[1:]
287+
app, name = urlsplit(url).path.split("/")[:2]
284288
return getattr(pynetbox.core.app.App(self.api, app), name)
285289

286290
def full_details(self):

tests/unit/test_response.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,30 @@ def test_nested_write_with_directory_in_base_url(self):
254254
)
255255

256256
def test_endpoint_from_url(self):
257+
api = Mock()
258+
api.base_url = "http://localhost:8080/api"
257259
test = Record(
258260
{
259261
"id": 123,
260262
"name": "test",
261263
"url": "http://localhost:8080/api/test-app/test-endpoint/1/",
262264
},
263-
Mock(),
265+
api,
266+
None,
267+
)
268+
ret = test._endpoint_from_url(test.url)
269+
self.assertEqual(ret.name, "test-endpoint")
270+
271+
def test_endpoint_from_url_with_directory_in_base_url(self):
272+
api = Mock()
273+
api.base_url = "http://localhost:8080/testing/api"
274+
test = Record(
275+
{
276+
"id": 123,
277+
"name": "test",
278+
"url": "http://localhost:8080/testing/api/test-app/test-endpoint/1/",
279+
},
280+
api,
264281
None,
265282
)
266283
ret = test._endpoint_from_url(test.url)

0 commit comments

Comments
 (0)