Skip to content

Commit

Permalink
Merge pull request #8 from macadmins/add-end-page
Browse files Browse the repository at this point in the history
Add `end_page` arg to Pro API Paginator
  • Loading branch information
brysontyrrell committed Sep 14, 2023
2 parents 74c1789 + 57f916e commit fb3cb9e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/jamf_pro_sdk/__about__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__title__ = "jamf-pro-sdk"
__version__ = "0.2a1"
__version__ = "0.2a2"
6 changes: 6 additions & 0 deletions src/jamf_pro_sdk/clients/pro_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def get_computer_inventory_v1(
self,
sections: List[str] = None,
start_page: int = 0,
end_page: int = None,
page_size: int = 100,
sort_expression: SortExpression = None,
filter_expression: FilterExpression = None,
Expand All @@ -45,6 +46,10 @@ def get_computer_inventory_v1(
:class:`Paginator` for more information.
:type start_page: int
:param end_page: (optional) The page to end returning results at. See :class:`Paginator` for
more information.
:type start_page: int
:param page_size: (optional) The number of results to include in each requested page. See
:class:`Paginator` for more information.
:type page_size: int
Expand Down Expand Up @@ -192,6 +197,7 @@ def get_computer_inventory_v1(
resource_path="v1/computers-inventory",
return_model=Computer,
start_page=start_page,
end_page=end_page,
page_size=page_size,
sort_expression=sort_expression,
filter_expression=filter_expression,
Expand Down
23 changes: 21 additions & 2 deletions src/jamf_pro_sdk/clients/pro_api/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def __init__(
resource_path: str,
return_model: Type[BaseModel] = None,
start_page: int = 0,
end_page: int = None,
page_size: int = 100,
sort_expression: SortExpression = None,
filter_expression: FilterExpression = None,
Expand All @@ -161,6 +162,17 @@ def __init__(
:param start_page: (optional) The page to begin returning results from. Generally this value
should be left at the default (``0``).
.. note::
Pages in the Pro API are zero-indexed. In a response that includes 10 pages the first
page is ``0`` and the last page is ``9``.
:type start_page: int
:param end_page: (optional) The page number to stop pagination on. The ``end_page`` argument
allows for retrieving page ranges (e.g. 2 - 4) or a single page result by using the same
number for both start and end values.
:type start_page: int
:param page_size: (optional) The number of results to include in each requested page. The
Expand All @@ -184,6 +196,7 @@ def __init__(
self.resource_path = resource_path
self.return_model = return_model
self.start_page = start_page
self.end_page = end_page
self.page_size = page_size
self.sort_expression = sort_expression
self.filter_expression = filter_expression
Expand Down Expand Up @@ -215,13 +228,19 @@ def _request(self) -> Iterator[Page]:
first_page = self._paginated_request(page=self.start_page)
yield first_page

if (total_count := first_page.total_count) > (results_count := len(first_page.results)):
total_count = (
min(first_page.total_count, (self.end_page + 1) * self.page_size)
if self.end_page
else first_page.total_count
)

if total_count > (results_count := len(first_page.results)):
for page in self._api_client.concurrent_api_requests(
self._paginated_request,
[
{"page": i}
for i in range(
self.start_page + 1,
self.start_page,
math.ceil((total_count - results_count) / self.page_size) + 1,
)
],
Expand Down

0 comments on commit fb3cb9e

Please sign in to comment.