From 2b31f2e9c361968ca34c69668a4c80609ce993ea Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Mon, 13 Feb 2012 21:43:45 +0100 Subject: [PATCH] AuthenticatedUser.add_following and .remove_following --- IntegrationTest.py | 3 +++ Reference.md | 4 ++-- github/Github.py | 17 +++++++++++++++-- github/GithubObject.UnitTest.py | 1 - github/GithubObject.py | 2 +- github/GithubObjects.py | 6 +++++- 6 files changed, 26 insertions(+), 7 deletions(-) diff --git a/IntegrationTest.py b/IntegrationTest.py index 219a316d..cc2c20f2 100644 --- a/IntegrationTest.py +++ b/IntegrationTest.py @@ -44,5 +44,8 @@ dumpRepository( g.get_user( "nvie" ).get_repos()[ 0 ] ) # u.edit( bio = u.bio + "(Edited by PyGithub)" ) # print "Bio after:", u.bio +# g.get_user().remove_following( g.get_user( "Lyloa" ) ) +# g.get_user().add_following( g.get_user( "Lyloa" ) ) + # o = g.get_organization( "BeaverSoftware" ) # o.edit( location = "Paris, France" ) diff --git a/Reference.md b/Reference.md index 0a329954..c2592139 100644 --- a/Reference.md +++ b/Reference.md @@ -408,8 +408,8 @@ /user/following/:user ===================== - GET: `` (TODO) - - PUT: `` (TODO) - - DELETE: `` (TODO) + - PUT: `AuthenticatedUser.add_following( user )` + - DELETE: `AuthenticatedUser.remove_following( user )` /user/keys ========== diff --git a/github/Github.py b/github/Github.py index dfdbc232..3527835e 100644 --- a/github/Github.py +++ b/github/Github.py @@ -18,18 +18,31 @@ class Github: return self.__verb def _dataRequest( self, verb, url, data = None ): + return json.load( self.__rawRequest( verb, url, json.dumps( data ) ) ) + + def _statusRequest( self, verb, url, data = None ): + try: + print verb, url, data + self.__rawRequest( verb, url, json.dumps( data ) ) + print "Got HTTP status 200" + return 200 + except urllib2.HTTPError as e: + print "Got HTTP status", e.code + return e.code + + def __rawRequest( self, verb, url, data ): assert( verb in [ "HEAD", "GET", "POST", "PATCH", "PUT", "DELETE" ] ) # print verb, url, data - req = Github._Request( verb, "https://api.github.com" + url, json.dumps( data ) ) + req = Github._Request( verb, "https://api.github.com" + url, data ) b64_userpass = base64.b64encode( '%s:%s' % ( self.__login, self.__password ) ) b64_userpass = b64_userpass.replace( '\n', '' ) req.add_header( "Authorization", "Basic %s" % b64_userpass ) - return json.load( urllib2.urlopen( req ) ) + return urllib2.urlopen( req ) def get_user( self, login = None ): if login is None: diff --git a/github/GithubObject.UnitTest.py b/github/GithubObject.UnitTest.py index f8bcacfb..4ffa70b8 100644 --- a/github/GithubObject.UnitTest.py +++ b/github/GithubObject.UnitTest.py @@ -33,7 +33,6 @@ class TestCaseWithGithubTestObject( unittest.TestCase ): def expectDelete( self, url ): return self.g.expect._statusRequest( "DELETE", url ) - return self.g.expect._dataRequest( "DELETE", url ) class GithubObjectWithOnlySimpleScalarAttributes( TestCaseWithGithubTestObject ): GithubTestObject = GithubObject( diff --git a/github/GithubObject.py b/github/GithubObject.py index d4b55565..64d3ab6e 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -85,7 +85,7 @@ class ExtendedListAttribute: def __call__( self, toBeAdded ): assert( isinstance( toBeAdded, self.__type ) ) - self.__obj._github._dataRequest( "PUT", self.__obj._baseUrl + "/" + self.__attributeName + "/" + toBeAdded._identity ) + self.__obj._github._statusRequest( "PUT", self.__obj._baseUrl + "/" + self.__attributeName + "/" + toBeAdded._identity ) class AddDefinition: def __init__( self, attributeName, addName, type ): diff --git a/github/GithubObjects.py b/github/GithubObjects.py index bf3b2881..1dee3d81 100644 --- a/github/GithubObjects.py +++ b/github/GithubObjects.py @@ -3,6 +3,7 @@ from GithubObject import * AuthenticatedUser = GithubObject( "AuthenticatedUser", BaseUrl( lambda obj: "/user" ), + Identity( lambda obj: obj.login ), SimpleScalarAttributes( "login", "id", "avatar_url", "gravatar_id", "url", "name", "company", "blog", "location", "email", "hireable", "bio", "public_repos", @@ -16,6 +17,7 @@ AuthenticatedUser = GithubObject( NamedUser = GithubObject( "NamedUser", BaseUrl( lambda obj: "/users/" + obj.login ), + Identity( lambda obj: obj.login ), SimpleScalarAttributes( "login", "id", "avatar_url", "gravatar_id", "url", "name", "company", "blog", "location", "email", "hireable", "bio", "public_repos", @@ -29,12 +31,13 @@ NamedUser = GithubObject( AuthenticatedUser._addAttributePolicy( ExtendedListAttribute( "followers", NamedUser ) ) NamedUser._addAttributePolicy( ExtendedListAttribute( "followers", NamedUser ) ) -AuthenticatedUser._addAttributePolicy( ExtendedListAttribute( "following", NamedUser ) ) +AuthenticatedUser._addAttributePolicy( ExtendedListAttribute( "following", NamedUser, True, True ) ) NamedUser._addAttributePolicy( ExtendedListAttribute( "following", NamedUser ) ) Organization = GithubObject( "Organization", BaseUrl( lambda obj: "/orgs/" + obj.login ), + Identity( lambda obj: obj.login ), SimpleScalarAttributes( "login", "id", "url", "avatar_url", "name", "company", "blog", "location", "email", "public_repos", "public_gists", "followers", @@ -54,6 +57,7 @@ NamedUser._addAttributePolicy( ExtendedListAttribute( "orgs", Organization ) ) Repository = GithubObject( "Repository", BaseUrl( lambda obj: "/repos/" + obj.owner.login + "/" + obj.name ), + Identity( lambda obj: obj.owner.login + "/" + obj.name ), SimpleScalarAttributes( "url", "html_url", "clone_url", "git_url", "ssh_url", "svn_url", "name", "description", "homepage", "language", "private",