Skip to main content

A generator for iterating over paginated JSON API responses

Project description

json-paginator

PyPI Version License

A generator for iterating over paginated JSON API responses

Installation

pip install json-paginator

Usage

Instantiate JsonApiPaginator with:

  • A URL to page 1 of the API output
  • A function (or lambda) get_nextpage(url, body) which describes how to get the next page. Return None to stop iteration.

Example:

from json_paginator import JsonApiPaginator

BASE = 'https://galaxy.ansible.com'

def get_next_page(url, body):
    if body['next_link']:
        return BASE +  body['next_link']
    return None

# instantiate the paginator
pages = JsonApiPaginator(
    BASE + '/api/v1/categories/?page=1',
    get_next_page
)

# iterate over the pages
for url, body in pages:
    print("calling %s" % (url))
    print("found %s results" % (len(body['results'])))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json-paginator-0.1.0.tar.gz (1.8 kB view details)

Uploaded Source

File details

Details for the file json-paginator-0.1.0.tar.gz.

File metadata

  • Download URL: json-paginator-0.1.0.tar.gz
  • Upload date:
  • Size: 1.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for json-paginator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a9f7242b9fcdb38815cfe30842963715a3eac46c102ff5560dedadb9ddad2681
MD5 e32485c007c0431588888780229764e9
BLAKE2b-256 f9f01b8692954bb68fbaad537a09fc2ac61883d92f9666e6b7ccbc97611e3203

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page