Iterators

class restfly.iterator.APIIterator(api, **kw)[source]

The API iterator provides a scalable way to work through result sets of any size. The iterator will walk through each page of data, returning one record at a time. If it reaches the end of a page of records, then it will request the next page of information and then continue to return records from the next page (and the next, and the next) until the counter reaches the total number of records that the API has reported.

Note that this Iterator is used as a base model for all of the iterators, and while the mechanics of each iterator may vary, they should all behave to the user in a similar manner.

_api

The APISession object that will be used for querying for the data.

Type:restfly.session.APISession
count

The current number of records that have been returned

Type:int
max_items

The maximum number of items to return before stopping iteration.

Type:int
max_pages

The maximum number of pages to request before throwing stopping iteration.

Type:int
num_pages

The number of pages that have been requested.

Type:int
page

The current page of data being walked through. pages will be cycled through as the iterator requests more information from the API.

Type:list
page_count

The number of record returned from the current page.

Type:int
total

The total number of records that exist for the current request.

Type:int
_get_page()[source]

A method to be overloaded in order to instruct the iterator how to retrieve the next page of data.

Examples

>>> class ExampleIterator(APIIterator):
...    def _get_page(self):
...        self.total = 100
...        self.page = [{'id': i + self._offset} for i in range(10)]
...        self._offset += self._limit
get(index, default=None)[source]

Retrieves an item from the the current page based off the index provided.

Parameters:
  • index (int) – The index of the item to retrieve.
  • default (obj) – The returned object if the item does not exist.

Examples

>>> a = APIIterator()
>>> a.get(2)
None
next()[source]

Ask for the next record