diff --git a/github/GithubObject.py b/github/GithubObject.py index 816e6535..dfcba1fb 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -195,17 +195,18 @@ def GithubObject( className, *attributePolicies ): @staticmethod def _addAttribute( attributeName, attributeDefinition ): - if attributeName in GithubObject.__attributeDefinitions: - raise BadGithubObjectException( "Same attribute defined by two policies" ) - else: - GithubObject.__attributeDefinitions[ attributeName ] = attributeDefinition + GithubObject.__checkAttributeName( attributeName ) + GithubObject.__attributeDefinitions[ attributeName ] = attributeDefinition @staticmethod def _addMethod( methodName, methodDefinition ): - if methodName in GithubObject.__methodDefinitions: - raise BadGithubObjectException( "Same method defined by two policies" ) - else: - GithubObject.__methodDefinitions[ methodName ] = methodDefinition + GithubObject.__checkAttributeName( methodName ) + GithubObject.__methodDefinitions[ methodName ] = methodDefinition + + @staticmethod + def __checkAttributeName( attributeName ): + if attributeName in GithubObject.__attributeDefinitions or attributeName in GithubObject.__methodDefinitions: + raise BadGithubObjectException( "Same attribute defined by two policies" ) def __init__( self, github, attributes, lazy ): self._github = github diff --git a/github/Requester.py b/github/Requester.py index 031c947b..a7689617 100644 --- a/github/Requester.py +++ b/github/Requester.py @@ -7,27 +7,28 @@ class UnknownGithubObject( Exception ): class Requester: def __init__( self, login, password ): - self.__login = login - self.__password = password + self.__authorizationHeader = "Basic " + base64.b64encode( login + ":" + password ).replace( '\n', '' ) def dataRequest( self, verb, url, parameters, input ): + if parameters is None: + parameters = dict() + + headers, output = self.__statusCheckedRequest( verb, url, parameters, input ) + + page = 2 + while "link" in headers and "next" in headers[ "link" ]: + parameters[ "page" ] = page + headers, newOutput = self.__statusCheckedRequest( verb, url, parameters, input ) + output += newOutput + page += 1 + + return output + + def __statusCheckedRequest( self, verb, url, parameters, input ): status, headers, output = self.__rawRequest( verb, url, parameters, input ) - if 200 <= status < 300: - page = 2 - while "link" in headers and "next" in headers[ "link" ]: - if parameters is None: - parameters = dict() - parameters[ "page" ] = page - newStatus, newHeaders, newOutput = self.__rawRequest( verb, url, parameters, input ) - if 200 <= newStatus < 300: - headers = newHeaders - page += 1 - output += newOutput - else: - raise UnknownGithubObject() - return output - else: + if status < 200 or status >= 300: raise UnknownGithubObject() + return headers, output def statusRequest( self, verb, url, parameters, input ): status, headers, output = self.__rawRequest( verb, url, parameters, input ) @@ -36,26 +37,33 @@ class Requester: def __rawRequest( self, verb, url, parameters, input ): assert verb in [ "HEAD", "GET", "POST", "PATCH", "PUT", "DELETE" ] - b64_userpass = base64.b64encode( '%s:%s' % ( self.__login, self.__password ) ) - b64_userpass = b64_userpass.replace( '\n', '' ) - - if parameters is not None and len( parameters ) != 0: - url += "?" + "&".join( str( key ) + "=" + str( value ) for key, value in parameters.iteritems() ) - - input = json.dumps( input ) - cnx = httplib.HTTPSConnection( "api.github.com", strict = True ) - cnx.request( verb, url, input, { "Authorization" : "Basic " + b64_userpass } ) + cnx.request( + verb, + self.__completeUrl( url, parameters ), + json.dumps( input ), + { "Authorization" : self.__authorizationHeader } + ) response = cnx.getresponse() status = response.status headers = dict( response.getheaders() ) - output = response.read() - if len( output ) == 0: - output = None - else: - output = json.loads( output ) + output = self.__strucutredFromJson( response.read() ) + cnx.close() # print verb, url, input, "==>", status, str( headers )[ :30 ], str( output )[ :30 ] return status, headers, output + + def __completeUrl( self, url, parameters ): + if parameters is None or len( parameters ) == 0: + return url + else: + ### @todo urlencode + return url + "?" + "&".join( str( key ) + "=" + str( value ) for key, value in parameters.iteritems() ) + + def __strucutredFromJson( self, data ): + if len( data ) == 0: + return None + else: + return json.loads( data )