Created
May 31, 2017 14:30
-
-
Save PlasmaPower/1a3a3834ce492729a844b54441c0533f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| diff --git a/github_api/__init__.py b/github_api/__init__.py | |
| index 47c0cf8..b345355 100644 | |
| --- a/github_api/__init__.py | |
| +++ b/github_api/__init__.py | |
| @@ -61,41 +61,47 @@ class API(object): | |
| self._reset = 0 | |
| def __call__(self, method, path, **kwargs): | |
| - # sleep for a cooldown period, so we don't exhaust our api requests in | |
| - # the middle of doing something important | |
| - now = time.time() | |
| - reset_in = max(self._reset - now, 0) | |
| - cooldown = compute_api_cooldown(self._remaining, reset_in) | |
| - log.debug("requests remaining: %s, reset in: %ds, cooldown sleep: %0.2fs", | |
| - self._remaining, reset_in, cooldown) | |
| - time.sleep(cooldown) | |
| - | |
| - url = self.BASE_URL + path | |
| - if re.match("https?://", path): | |
| - url = path | |
| - | |
| - headers = self.BASE_HEADERS.copy() | |
| - | |
| - log.info("requesting %s to %r", method.upper(), path) | |
| - resp = requests.request(method, url, headers=headers, auth=self._auth, | |
| - **kwargs) | |
| - | |
| - h = resp.headers | |
| - | |
| - # keep our rate limit details up-to-date | |
| - try: | |
| - self._remaining = int(h["X-RateLimit-Remaining"]) | |
| - self._reset = int(h["X-RateLimit-Reset"]) | |
| - # on error, we won't receive these headers | |
| - except KeyError: | |
| - pass | |
| - | |
| - resp.raise_for_status() | |
| - | |
| - # not all requests return json, and this will raise for those | |
| - try: | |
| - data = resp.json() | |
| - except: | |
| - data = None | |
| - | |
| - return data | |
| + # loop for 202 responses | |
| + while True: | |
| + # sleep for a cooldown period, so we don't exhaust our api requests in | |
| + # the middle of doing something important | |
| + now = time.time() | |
| + reset_in = max(self._reset - now, 0) | |
| + cooldown = compute_api_cooldown(self._remaining, reset_in) | |
| + log.debug("requests remaining: %s, reset in: %ds, cooldown sleep: %0.2fs", | |
| + self._remaining, reset_in, cooldown) | |
| + time.sleep(cooldown) | |
| + | |
| + url = self.BASE_URL + path | |
| + if re.match("https?://", path): | |
| + url = path | |
| + | |
| + headers = self.BASE_HEADERS.copy() | |
| + | |
| + log.info("requesting %s to %r", method.upper(), path) | |
| + resp = requests.request(method, url, headers=headers, auth=self._auth, | |
| + **kwargs) | |
| + | |
| + h = resp.headers | |
| + | |
| + # keep our rate limit details up-to-date | |
| + try: | |
| + self._remaining = int(h["X-RateLimit-Remaining"]) | |
| + self._reset = int(h["X-RateLimit-Reset"]) | |
| + # on error, we won't receive these headers | |
| + except KeyError: | |
| + pass | |
| + | |
| + if resp.status_code == 202: | |
| + time.sleep(5) | |
| + continue | |
| + | |
| + resp.raise_for_status() | |
| + | |
| + # not all requests return json, and this will raise for those | |
| + try: | |
| + data = resp.json() | |
| + except: | |
| + data = None | |
| + | |
| + return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment