Skip to content

Instantly share code, notes, and snippets.

@fermayo
Created February 23, 2018 02:54
Show Gist options
  • Save fermayo/9d0706ccbefe7425c643ee0c271d949b to your computer and use it in GitHub Desktop.
Save fermayo/9d0706ccbefe7425c643ee0c271d949b to your computer and use it in GitHub Desktop.
Little function to repaginate APIs that use 'Link' headers (i.e. GitHub API v3)
import requests
def depaginate(initial_url, **kwargs):
r = requests.get(initial_url, **kwargs)
r.raise_for_status()
yield r.json()
while r.links.get('next'):
r = requests.get(r.links.get('next'), **kwargs)
r.raise_for_status()
yield r.json()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment