Skip to content

Commit

Permalink
Compute endpoint on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
srfwx committed Dec 5, 2024
1 parent b2900a0 commit 0f9e41b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
43 changes: 22 additions & 21 deletions pynetbox/core/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,8 @@ def __init__(self, values, api, endpoint):
self._init_cache = []
self.api = api
self.default_ret = Record
self.endpoint = (
self._endpoint_from_url(values["url"])
if values and "url" in values and values["url"]
else endpoint
)
self.url = values.get("url", None) if values else None
self._endpoint = endpoint
if values:
self._parse_values(values)

Expand Down Expand Up @@ -336,6 +333,22 @@ def __eq__(self, other):
return self.__key__() == other.__key__()
return NotImplemented

@property
def endpoint(self):
if self._endpoint is None:
self._endpoint = self._endpoint_from_url()
return self._endpoint

def _endpoint_from_url(self):
url_path = self.url.replace(self.api.base_url, "").split("/")
is_plugin = url_path and url_path[1] == "plugins"
start = 2 if is_plugin else 1
app, name = [i.replace("-", "_") for i in url_path[start : start + 2]]
if is_plugin:
return getattr(getattr(self.api.plugins, app), name)
else:
return getattr(getattr(self.api, app), name)

def _parse_values(self, values):
"""Parses values init arg.
Expand All @@ -349,7 +362,7 @@ def dict_parser(key_name, value):
lookup = getattr(self.__class__, key_name, None)
if lookup is None or not issubclass(lookup, JsonField):
# If we have a custom model field, use it, otherwise use a default Record model
args = [value, self.api, self.endpoint]
args = [value, self.api, None]
value = lookup(*args) if lookup else self.default_ret(*args)
return value, get_return(value)
return value, marshal.loads(marshal.dumps(value))
Expand All @@ -359,7 +372,7 @@ def list_item_parser(list_item):

lookup = list_item["object_type"]
if model := CONTENT_TYPE_MAPPER.get(lookup, None):
return model(list_item["object"], self.api, self.endpoint)
return model(list_item["object"], self.api, None)
return list_item

def list_parser(key_name, value):
Expand All @@ -378,12 +391,10 @@ def list_parser(key_name, value):
if not isinstance(lookup, list):
# This is *list_parser*, so if the custom model field is not
# a list (or is not defined), just return the default model
value = [
self.default_ret(i, self.api, self.endpoint) for i in value
]
value = [self.default_ret(i, self.api, None) for i in value]
else:
model = lookup[0]
value = [model(i, self.api, self.endpoint) for i in value]
value = [model(i, self.api, None) for i in value]
return value, [*value]

def parse_value(key_name, value):
Expand All @@ -398,16 +409,6 @@ def parse_value(key_name, value):

self._init_cache = [(k, parse_value(k, v)) for k, v in values.items()]

def _endpoint_from_url(self, url):
url_path = url.replace(self.api.base_url, "").split("/")
is_plugin = url_path and url_path[1] == "plugins"
start = 2 if is_plugin else 1
app, name = [i.replace("-", "_") for i in url_path[start : start + 2]]
if is_plugin:
return getattr(getattr(self.api.plugins, app), name)
else:
return getattr(getattr(self.api, app), name)

def full_details(self):
"""Queries the hyperlinked endpoint if 'url' is defined.
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def test_endpoint_from_url(self):
api,
None,
)
ret = test._endpoint_from_url(test.url)
ret = test._endpoint_from_url()
self.assertEqual(ret.name, "test-endpoint")

def test_endpoint_from_url_with_directory_in_base_url(self):
Expand All @@ -309,7 +309,7 @@ def test_endpoint_from_url_with_directory_in_base_url(self):
api,
None,
)
ret = test._endpoint_from_url(test.url)
ret = test._endpoint_from_url()
self.assertEqual(ret.name, "test-endpoint")

def test_endpoint_from_url_with_plugins(self):
Expand All @@ -328,7 +328,7 @@ def test_endpoint_from_url_with_plugins(self):
api,
None,
)
ret = test._endpoint_from_url(test.url)
ret = test._endpoint_from_url()
self.assertEqual(ret.name, "test-endpoint")

def test_endpoint_from_url_with_plugins_and_directory_in_base_url(self):
Expand All @@ -347,7 +347,7 @@ def test_endpoint_from_url_with_plugins_and_directory_in_base_url(self):
api,
None,
)
ret = test._endpoint_from_url(test.url)
ret = test._endpoint_from_url()
self.assertEqual(ret.name, "test-endpoint")

def test_serialize_tag_list_order(self):
Expand Down

0 comments on commit 0f9e41b

Please sign in to comment.