Page

class trustar.models.page.Page(items=None, page_number=None, page_size=None, total_elements=None, has_next=None)

This class models a page of items that would be found in the body of a response from an endpoint that uses pagination. Not all paginated endpoints will use page_number. For instance, the get_reports_page method requires pagination to be performed by continuously adjusting the from and to parameters.

Variables:
  • items – The list of items of the page; i.e. a list of indicators, reports, etc.
  • page_number – The number of the page out of all total pages, indexed from 0. i.e. if there are 4 total pages of size 25, then page 0 will contain the first 25 elements, page 1 will contain the next 25, etc.
  • page_size – The size of the page that was request. Note that, if this is the last page, then this might not equal len(items). For instance, if pages of size 25 were requested, there are 107 total elements, and this is the last page, then page_size will be 25 even though the page only contains 7 elements.
  • total_elements – The total number of elements on the server, e.g. the total number of elements across all pages. Note that it is possible for this value to change between pages, since data can change between queries.
static from_dict(page, content_type=None)

Create a Page object from a dictionary. This method is intended for internal use, to construct a Page object from the body of a response json from a paginated endpoint.

Parameters:
  • page – The dictionary.
  • content_type – The class that the contents should be deserialized into.
Returns:

The resulting Page object.

classmethod get_generator(page_generator)

Gets a generator for retrieving all results from a paginated endpoint. Pass exactly one of page_generator or func. This method is intended for internal use.

Parameters:page_generator – A generator to be used to generate each successive Page.
Returns:A generator that generates each successive element.
static get_page_generator(func, start_page=0, page_size=None)

Constructs a generator for retrieving pages from a paginated endpoint. This method is intended for internal use.

Parameters:
  • func – Should take parameters page_number and page_size and return the corresponding Page object.
  • start_page – The page to start on.
  • page_size – The size of each page.
Returns:

A generator that generates each successive page.

get_total_pages()
Returns:The total number of pages on the server.
has_more_pages()
Returns:True if there are more pages available on the server.
to_dict(remove_nones=False)

Creates a dictionary representation of the page.

Parameters:remove_nones – Whether None values should be filtered out of the dictionary. Defaults to False.
Returns:A dictionary representation of the page.