Skip to content
This repository was archived by the owner on Jun 27, 2025. It is now read-only.

Commit 787b3c7

Browse files
committed
cleanup tying, pagination to list
1 parent bd9274a commit 787b3c7

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

netbox_python/baseapi.py

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,69 @@
1+
from typing import Any, Callable, Dict, List
2+
3+
from .rest import Result
4+
5+
16
class baseapi:
27
def __init__(self, client):
38
self.client = client
49
super().__init__()
510

611

712
class CreateableAPIResource:
8-
def create(self, *args, **kwargs):
13+
def create(self, *args, **kwargs) -> Result:
914
return self.client.post(self.path, json=args[0] if args else kwargs)
1015

1116

1217
class DeletableAPIResource:
13-
def delete(self, obj):
14-
if obj and isinstance(obj, list):
18+
def delete(self, obj: List[Any] | str | int) -> Result:
19+
if isinstance(obj, list):
1520
return self.client.delete(self.path, json=obj)
21+
1622
return self.client.delete(f"{self.path}{obj}/")
1723

1824

1925
class ListableAPIResource:
20-
def list(self, **kwargs):
26+
def paginate(self, result: Result) -> Result:
27+
next_token = result.pagination["next"]
28+
yield result
29+
while next_token:
30+
result = self.client.get(
31+
self.path, url_override=next_token, params=result.params
32+
)
33+
yield result
34+
next_token = result.pagination["next"]
35+
36+
def list(self, **kwargs) -> Result:
2137
return self.client.get(self.path, params=kwargs)
2238

2339
def all(self, **kwargs):
24-
ret = self.client.get(self.path, params=kwargs)
25-
pagination = ret.pagination
26-
while pagination["next"]:
27-
partial = self.client.get(
28-
self.path, url_override=pagination["next"], params=kwargs
29-
)
30-
ret.data.append(partial.data)
31-
pagination = partial.pagination
40+
result = None
41+
for page in self.paginate(self.client.get(self.path, params=kwargs)):
42+
if not result:
43+
result = page
44+
else:
45+
result.data.append(page.data)
3246

33-
ret.pagination["next"] = None
34-
ret.pagination["previous"] = None
35-
return ret
47+
result.pagination["next"] = None
48+
result.pagination["previous"] = None
49+
return result
3650

3751

3852
class RetrievableAPIResource:
39-
def get(self, id):
53+
def get(self, id: str | int) -> Result:
4054
return self.client.get(f"{self.path}{id}/")
4155

4256

4357
class RetrievableRootAPIResource:
44-
def get(self, **kwargs):
58+
def get(self, **kwargs) -> Result:
4559
return self.client.get(self.path, params=kwargs)
4660

4761

4862
class UpdateableAPIResource:
49-
def update(self, obj, **kwargs):
50-
if obj and isinstance(obj, list):
63+
def update(self, obj: List[Any] | str | int, **kwargs) -> Result:
64+
if isinstance(obj, list):
5165
return self.client.patch(self.path, json=obj)
66+
5267
return self.client.patch(f"{self.path}{obj}/", json=kwargs)
5368

5469

netbox_python/rest.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from netbox_python.exceptions import NetBoxException
99

10-
JSONType = Union[None, bool, int, float, str, List[Any], Dict[str, Any]]
10+
# JSONType = Union[None, bool, int, float, str, List[Any], Dict[str, Any]]
1111

1212

1313
class Result:
@@ -17,6 +17,7 @@ def __init__(
1717
headers: CaseInsensitiveDict,
1818
message: str = "",
1919
pagination: dict = None,
20+
params: dict = None,
2021
data: List[Dict] = None,
2122
):
2223
"""
@@ -30,6 +31,7 @@ def __init__(
3031
self.message = str(message)
3132
self.data = data if data else []
3233
self.pagination = pagination
34+
self.params = params
3335

3436

3537
class RestClient:
@@ -49,7 +51,7 @@ def __exit__(self, *_):
4951
def close(self):
5052
return self._session.close()
5153

52-
def request(self, method: str, path: str, **kwargs) -> JSONType:
54+
def request(self, method: str, path: str, **kwargs) -> Result:
5355
url = kwargs.pop("url_override", None)
5456
if not url:
5557
url = f"{self.base_url}/{path}"
@@ -94,6 +96,7 @@ def request(self, method: str, path: str, **kwargs) -> JSONType:
9496
headers=response.headers,
9597
message=response.reason,
9698
pagination=pagination,
99+
params=kwargs.get("params", None),
97100
data=data_out,
98101
)
99102
# self._logger.error(msg=log_line)

0 commit comments

Comments
 (0)