Skip to main content

Pagination

When invoking the list() method on a resource, you will receive a pagination object that contains the first page of results. You can then use the next() and previous() methods to navigate through the pages. There are two types of pagination objects: PagePaginatedResource and CursorPaginatedResource.

PagePaginatedResource

This type of pagination is used for resources that are not frequently created and deleted, such as Agent.

class PagePaginatedResource:
"""A paginated resource that uses page-based pagination."""

total_count: int
current_page: int
results: List

def next(self) -> Optional[PagePaginatedResource]:
"""Returns the next page of results, or None if there is no next page."""

def previous(self) -> Optional[PagePaginatedResource]:
"""Returns the previous page of results, or None if there is no previous page."""

# Example:
agents = swarmnode.Agent.list()
next_page_agents = agents.next()

CursorPaginatedResource

This type of pagination is used for resources that are frequently created and deleted, such as Execution. Cursor pagination ensures that users will not see the same item more than once, even if new data is added or existing data is modified during pagination.

class CursorPaginatedResource:
"""A paginated resource that uses cursor-based pagination."""

results: List

def next(self) -> Optional[CursorPaginatedResource]:
"""Returns the next page of results, or None if there is no next page."""

def previous(self) -> Optional[CursorPaginatedResource]:
"""Returns the previous page of results, or None if there is no previous page."""

# Example:
executions = swarmnode.Execution.list()
next_page_executions = executions.next()