Skip to content
This repository has been archived by the owner on May 31, 2022. It is now read-only.

Commit

Permalink
git-hub: warn when approaching rate limits
Browse files Browse the repository at this point in the history
  • Loading branch information
seveas committed Nov 2, 2015
1 parent 1328f6b commit 1ad3305
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/gitspindle/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ def _gist_contents(self, path, ref):
return Content(f)
github3.gists.Gist.contents = _gist_contents

from github3.session import GitHubSession
from gitspindle.ansi import wrap, fgcolor, attr
import time
warned = False
def request(self, *args, **kwargs):
global warned
r = self.orig_request(*args, **kwargs)
# Warn when approaching the rate limit
limit = int(r.headers.get('x-ratelimit-limit', 0))
remaining = int(r.headers.get('x-ratelimit-remaining', 0))
reset = int(r.headers.get('x-ratelimit-reset', 0))
if limit and (remaining < 100) and not warned:
msg = "You are approaching the API rate limit. Only %d requests remain until %s" % (remaining, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(reset)))
print(wrap(msg, fgcolor.red, attr.bright))
warned = True
return r
GitHubSession.orig_request = GitHubSession.request
GitHubSession.request = request

import gitspindle.glapi as glapi
glapi.Project.spindle = 'gitlab'
glapi.UserProject.spindle = 'gitlab'
Expand Down

0 comments on commit 1ad3305

Please sign in to comment.