From 5bb654d26dd014d36794acd1e6ecf3736f12aad7 Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Fri, 25 May 2012 14:10:54 +0200 Subject: [PATCH 1/2] Implement the three authentication schemes --- github/Github.py | 4 ++-- github/Requester.py | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/github/Github.py b/github/Github.py index e6373d6e..8c834887 100644 --- a/github/Github.py +++ b/github/Github.py @@ -2,8 +2,8 @@ from Requester import Requester from GithubObjects import * class Github: - def __init__( self, login, password, debugFile = None ): - self.__requester = Requester( login, password ) + def __init__( self, login_or_token = None, password = None, debugFile = None ): + self.__requester = Requester( login_or_token, password ) self.__debugFile = debugFile def _dataRequest( self, verb, url, parameters, data ): diff --git a/github/Requester.py b/github/Requester.py index fac044b8..0034ec87 100644 --- a/github/Requester.py +++ b/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 def dataRequest( self, verb, url, parameters, input ): if parameters is None: @@ -46,12 +53,16 @@ class Requester: def __rawRequest( self, verb, url, parameters, input ): assert verb in [ "HEAD", "GET", "POST", "PATCH", "PUT", "DELETE" ] + 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() From 0cec0d25e606c023a62a4fc7cdc815309ebf6d16 Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Fri, 25 May 2012 19:13:33 +0200 Subject: [PATCH 2/2] Publish version 0.7 --- ReferenceOfClasses.md | 2 +- setup.py | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ReferenceOfClasses.md b/ReferenceOfClasses.md index af9d0955..c1602a73 100644 --- a/ReferenceOfClasses.md +++ b/ReferenceOfClasses.md @@ -3,7 +3,7 @@ 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 =======================