diff --git a/doc/ReferenceOfClasses.md b/doc/ReferenceOfClasses.md index 1dc80326..c4fa8b4e 100644 --- a/doc/ReferenceOfClasses.md +++ b/doc/ReferenceOfClasses.md @@ -2,7 +2,7 @@ You don't normaly create instances of any class but `Github`. You obtain instances through calls to `get_` and `create_` methods. Class `Github` ============== -* Constructed from user's login and password +* Constructed from user's login and password or OAuth token * `get_user()`: `AuthenticatedUser` * `get_user( login )`: `NamedUser` * `get_organization( login )`: `Organization` diff --git a/setup.py b/setup.py index 53bce9fa..47e6c657 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ import textwrap setup( name = 'PyGithub', - version = '0.6', + version = '0.7', description = 'Use the full Github API v3', author = 'Vincent Jacques', author_email = 'vincent@vincent-jacques.net', @@ -26,6 +26,14 @@ setup( print repo.name repo.edit( has_wiki = False ) + You can also create a Github instance without authentication:: + + g = Github( "user", "password" ) + + Or with an OAuth token:: + + g = Github( token ) + Reference documentation ======================= diff --git a/src/github/Github.py b/src/github/Github.py index 58ec518b..d7dee881 100644 --- a/src/github/Github.py +++ b/src/github/Github.py @@ -7,8 +7,8 @@ import PaginatedList from GithubObject import LazyCompletion, ImmediateCompletion class Github( object ): - def __init__( self, login, password ): - self.__requester = Requester( login, password ) + def __init__( self, login_or_token = None, password = None ): + self.__requester = Requester( login_or_token, password ) @property def rate_limiting( self ): diff --git a/src/github/Requester.py b/src/github/Requester.py index ddc9e7cd..c7b55915 100644 --- a/src/github/Requester.py +++ b/src/github/Requester.py @@ -7,8 +7,15 @@ class UnknownGithubObject( Exception ): pass class Requester: - def __init__( self, login, password ): - self.__authorizationHeader = "Basic " + base64.b64encode( login + ":" + password ).replace( '\n', '' ) + def __init__( self, login_or_token, password ): + if password is not None: + login = login_or_token + self.__authorizationHeader = "Basic " + base64.b64encode( login + ":" + password ).replace( '\n', '' ) + elif login_or_token is not None: + token = login_or_token + self.__authorizationHeader = "token " + token + else: + self.__authorizationHeader = None self.rate_limiting = ( 5000, 5000 ) def request( self, verb, url, parameters, input ): @@ -16,12 +23,16 @@ class Requester: assert url.startswith( "https://api.github.com" ) url = url[ len( "https://api.github.com" ) : ] + headers = dict() + if self.__authorizationHeader is not None: + headers[ "Authorization" ] = self.__authorizationHeader + cnx = httplib.HTTPSConnection( "api.github.com", strict = True ) cnx.request( verb, self.__completeUrl( url, parameters ), json.dumps( input ), - { "Authorization" : self.__authorizationHeader } + headers ) response = cnx.getresponse()