Skip to content

Rogdham/unpaginate

Repository files navigation

Unpaginate

Chain calls of paginated APIs

GitHub build status Release on PyPI Code coverage Mypy type checker MIT License


📖 Documentation   |   📃 Changelog


API endpoints are often paginated, meaning that you must chain requests to get the content in full. Unpaginate provides a decorator to make that task easy:

>>> from unpaginate import unpaginate

>>> @unpaginate
... def get_cities(pagination, country):
...     return requests.post(
...         "https://api.example.org/cities",
...         json={"country": country, "page": pagination.page},
...     ).json()["items"]

Calling the decorated function allows to iterate over all items of all pages:

>>> iterator = get_cities("France")  # the 'pagination' parameter is added by the decorator
>>> iterator
<generator object get_cities ...>

>>> next(iterator)
'Paris'
>>> next(iterator)
'Lyon'
>>> next(iterator)
'Marseille'

All pagination schemes are supported: