Skip to content

Instantly share code, notes, and snippets.

@hwine
Created March 2, 2018 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hwine/e4384145127a915b96849537bef3b93d to your computer and use it in GitHub Desktop.
Save hwine/e4384145127a915b96849537bef3b93d to your computer and use it in GitHub Desktop.
Quick hack generator for GitHub paginated responses
"""
Quick hack to transparently get paginated responses from GitHub
"""
import copy # noqa: E402
class AG_Exception(Exception):
pass
def ag_call(func, *args, expected_rc=None, **kwargs):
"""
Wrap AGitHub calls with basic error detection.
Not smart, and hides any error information from caller.
But very convenient. :)
"""
if expected_rc is None:
expected_rc = [200, ]
rc, body = func(*args, **kwargs)
if rc not in expected_rc:
raise AG_Exception
return body
def ag_get_all(func, *orig_args, **orig_kwargs):
"""
Generator for multi-page GitHub responses
It hacks the "page" query parameter to each call to get the next page. This
is Not a general solution - it does not follow the links in the headers
like a good client should.
"""
kwargs = copy.deepcopy(orig_kwargs)
args = copy.deepcopy(orig_args)
kwargs["page"] = 1
while True:
body = ag_call(func, *args, **kwargs)
if len(body) >= 1:
for elem in body:
yield elem
else:
break
# fix up to get next page
kwargs["page"] += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment