requests-paginator
A generator for iterating over paginated API responses
Installation
pip install requests-paginator
Usage
Instantiate RequestsPaginator with:
- A URL to page 1 of the API output
- A function (or lambda)
get_nextpage(page)which describes how to get the next page:- Return
Noneto stop iteration. pageis an instance ofrequests.models.Response
- Return
Examples:
from requests_paginator import RequestsPaginator
BASE = 'https://galaxy.ansible.com'
def get_next_page(page):
body = page.json()
if body['next_link']:
return BASE + body['next_link']
return None
# instantiate the paginator
pages = RequestsPaginator(
BASE + '/api/v1/categories/?page=1',
get_next_page
)
# iterate over the pages
for page in pages:
print("calling %s" % (page.url))
page.raise_for_status()
print("found %s results" % (len(page.json()['results'])))
from requests_paginator import RequestsPaginator
def get_next_page(page):
links = {}
if "Link" in page.headers:
linkHeaders = page.headers["Link"].split(", ")
for linkHeader in linkHeaders:
(url, rel) = linkHeader.split("; ")
url = url[1:-1]
rel = rel[5:-1]
if rel == 'next':
return url
return None
# instantiate the paginator
pages = RequestsPaginator(
'https://api.github.com/users/github/repos?page=1',
get_next_page
)
# iterate over the pages
for page in pages:
print("calling %s" % (page.url))
page.raise_for_status()
print("found %s results" % (len(page.json())))
Metadata
Release files for requests-paginator 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| requests-paginator-0.2.0.tar.gz | 2.1 kB | Without provenance | Details for requests-paginator-0.2.0.tar.gz |
Release files / requests-paginator-0.2.0.tar.gz
| Download URL | requests-paginator-0.2.0.tar.gz |
|---|---|
| Size | 2.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3ca5709e8c6249f805a00850d0ae10e3d163c51ec5af1fc0f75ba32a0a9cc490
|
|
BLAKE2b-256 checksum How to use checksums |
cf32a08732040bf2184ba6d44ef48a0b016544dae7c0117784245800347a4fea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7
|
Security for requests-paginator 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.