diff --git a/codegen/templates/ReferenceOfClasses.md b/codegen/templates/ReferenceOfClasses.md index 2a2ed667..8331d694 100644 --- a/codegen/templates/ReferenceOfClasses.md +++ b/codegen/templates/ReferenceOfClasses.md @@ -9,6 +9,7 @@ Class `Github` * `get_organization( login )`: `Organization` * `get_gist( id )`: `Gist` * `get_gists()`: list of `Gist` +* `rate_limiting`: tuple of two integers: remaining and limit, as explained in [Rate Limiting](http://developer.github.com/v3/#rate-limiting) {% for class in classes|dictsort:"name" %} Class `{{ class.name }}` diff --git a/doc/ReferenceOfClasses.md b/doc/ReferenceOfClasses.md index 526682eb..767b3bb4 100644 --- a/doc/ReferenceOfClasses.md +++ b/doc/ReferenceOfClasses.md @@ -8,6 +8,7 @@ Class `Github` * `get_organization( login )`: `Organization` * `get_gist( id )`: `Gist` * `get_gists()`: list of `Gist` +* `rate_limiting`: tuple of two integers: remaining and limit, as explained in [Rate Limiting](http://developer.github.com/v3/#rate-limiting) Class `AuthenticatedUser` ========================= diff --git a/src/github/Github.py b/src/github/Github.py index 09ea6312..58ec518b 100644 --- a/src/github/Github.py +++ b/src/github/Github.py @@ -6,10 +6,14 @@ import Gist import PaginatedList from GithubObject import LazyCompletion, ImmediateCompletion -class Github: +class Github( object ): def __init__( self, login, password ): self.__requester = Requester( login, password ) + @property + def rate_limiting( self ): + return self.__requester.rate_limiting + def get_user( self, login = None ): if login is None: attributes = { diff --git a/src/github/Requester.py b/src/github/Requester.py index cc29c163..ddc9e7cd 100644 --- a/src/github/Requester.py +++ b/src/github/Requester.py @@ -9,6 +9,7 @@ class UnknownGithubObject( Exception ): class Requester: def __init__( self, login, password ): self.__authorizationHeader = "Basic " + base64.b64encode( login + ":" + password ).replace( '\n', '' ) + self.rate_limiting = ( 5000, 5000 ) def request( self, verb, url, parameters, input ): assert verb in [ "HEAD", "GET", "POST", "PATCH", "PUT", "DELETE" ] @@ -30,6 +31,8 @@ class Requester: cnx.close() + self.rate_limiting = ( int( headers[ "x-ratelimit-remaining" ] ), int( headers[ "x-ratelimit-limit" ] ) ) + # print verb, url, parameters, input, "==>", status, str( headers )[ :30 ], str( output )[ :30 ] return status, headers, output diff --git a/test/IntegrationTest.py b/test/IntegrationTest.py index 3a3ba57b..108e3e3e 100644 --- a/test/IntegrationTest.py +++ b/test/IntegrationTest.py @@ -10,5 +10,6 @@ from MilestonesAndIssues import * from NamedUser import * from Hook import * from Gist import * +from RateLimiting import * Framework.main() diff --git a/test/RateLimiting.py b/test/RateLimiting.py new file mode 100644 index 00000000..012f699f --- /dev/null +++ b/test/RateLimiting.py @@ -0,0 +1,7 @@ +import Framework + +class RateLimiting( Framework.TestCase ): + def testRateLimiting( self ): + self.assertEqual( self.g.rate_limiting, ( 5000, 5000 ) ) + self.g.get_user( "jacquev6" ) + self.assertEqual( self.g.rate_limiting, ( 4999, 5000 ) ) diff --git a/test/ReplayData/RateLimiting.testRateLimiting.txt b/test/ReplayData/RateLimiting.testRateLimiting.txt new file mode 100644 index 00000000..a833b074 --- /dev/null +++ b/test/ReplayData/RateLimiting.testRateLimiting.txt @@ -0,0 +1,5 @@ +GET /users/jacquev6 {} null +200 +[('status', '200 OK'), ('x-ratelimit-remaining', '4999'), ('content-length', '801'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"8cd01ddcd0adee5501abc607e6a640b0"'), ('date', 'Mon, 21 May 2012 11:08:21 GMT'), ('content-type', 'application/json; charset=utf-8')] +{"total_private_repos":5,"public_gists":1,"type":"User","owned_private_repos":5,"private_gists":5,"company":"Criteo","url":"https://api.github.com/users/jacquev6","gravatar_id":"b68de5ae38616c296fa345d2b9df2225","avatar_url":"https://secure.gravatar.com/avatar/b68de5ae38616c296fa345d2b9df2225?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","plan":{"private_repos":5,"collaborators":1,"name":"micro","space":614400},"followers":13,"blog":"http://vincent-jacques.net","collaborators":0,"login":"jacquev6","email":"vincent@vincent-jacques.net","disk_usage":16812,"public_repos":11,"html_url":"https://github.com/jacquev6","name":"Vincent Jacques","hireable":false,"created_at":"2010-07-09T06:10:06Z","location":"Paris, France","id":327146,"following":24,"bio":""} +