Client Side Auto-Pagination

In this example, we will show how to use the client side auto-pagination feature of the Peak-SDK. This feature allows you to iterate over a list of resources without having to worry about the pagination logic.

For this example we will use the Press-App client, but the same logic applies to all the other clients with list methods that accept a page_size and page_number parameters, the feature can be toggled on or off by setting the return_iterator parameter to True or False respectively.

By default, the return_iterator parameter is set to True, and using page_size and page_number parameters will have no effect when return_iterator is set to True.

Import the Press-App module and Instantiate a Press-App Client

[ ]:
from collections.abc import Iterator
from typing import Any

from peak.press import apps

client: apps.App = apps.get_client()

List created app-spec(s) and iterate over them using auto-pagination with return_iterator=True

[ ]:
spec_iterator: Iterator[dict[str, Any]] = client.list_specs(return_iterator=True)
existing_specs: dict[str, Any] = next(spec_iterator)

List created app-spec(s) with auto-pagination disabled

[ ]:
spec_iterator: dict[str, Any] = client.list_specs(page_size=50, page_number=1, return_iterator=False)
first_50_specs: dict[str, Any] = spec_iterator