Skip to content

Commit

Permalink
Retrieve rate limiting information
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquev6 committed May 21, 2012
1 parent d5ba645 commit ca97469
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions codegen/templates/ReferenceOfClasses.md
Expand Up @@ -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 }}`
Expand Down
1 change: 1 addition & 0 deletions doc/ReferenceOfClasses.md
Expand Up @@ -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`
=========================
Expand Down
6 changes: 5 additions & 1 deletion src/github/Github.py
Expand Up @@ -6,10 +6,14 @@
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 = {
Expand Down
3 changes: 3 additions & 0 deletions src/github/Requester.py
Expand Up @@ -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" ]
Expand All @@ -30,6 +31,8 @@ def request( self, verb, url, parameters, input ):

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

Expand Down
1 change: 1 addition & 0 deletions test/IntegrationTest.py
Expand Up @@ -10,5 +10,6 @@
from NamedUser import *
from Hook import *
from Gist import *
from RateLimiting import *

Framework.main()
7 changes: 7 additions & 0 deletions 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 ) )
5 changes: 5 additions & 0 deletions 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":""}

0 comments on commit ca97469

Please sign in to comment.