Created
March 2, 2018 22:00
-
-
Save hwine/e4384145127a915b96849537bef3b93d to your computer and use it in GitHub Desktop.
Quick hack generator for GitHub paginated responses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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