Skip to content

Commit 5173efd

Browse files
author
Zach Moody
authored
Merge pull request #308 from markkuleinio/master
Fixes #304: Change Record._endpoint_from_url() to count URL fields from the end
2 parents 4c3b0f8 + 12b38cb commit 5173efd

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-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("/")[2:4]
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: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,57 @@ def test_nested_write(self):
227227
"http://localhost:8080/api/test-app/test-endpoint/321/",
228228
)
229229

230+
def test_nested_write_with_directory_in_base_url(self):
231+
app = Mock()
232+
app.token = "abc123"
233+
app.base_url = "http://localhost:8080/testing/api"
234+
endpoint = Mock()
235+
endpoint.name = "test-endpoint"
236+
test = Record(
237+
{
238+
"id": 123,
239+
"name": "test",
240+
"child": {
241+
"id": 321,
242+
"name": "test123",
243+
"url": "http://localhost:8080/testing/api/test-app/test-endpoint/321/",
244+
},
245+
},
246+
app,
247+
endpoint,
248+
)
249+
test.child.name = "test321"
250+
test.child.save()
251+
self.assertEqual(
252+
app.http_session.patch.call_args[0][0],
253+
"http://localhost:8080/testing/api/test-app/test-endpoint/321/",
254+
)
255+
230256
def test_endpoint_from_url(self):
257+
api = Mock()
258+
api.base_url = "http://localhost:8080/api"
231259
test = Record(
232260
{
233261
"id": 123,
234262
"name": "test",
235263
"url": "http://localhost:8080/api/test-app/test-endpoint/1/",
236264
},
237-
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,
238281
None,
239282
)
240283
ret = test._endpoint_from_url(test.url)

0 commit comments

Comments
 (0)