The underlying http object used by SimpleWrapper is now exposed as a member variable called http.

Added support for getting the following count and follower count to the SimpleWrapper
Tweaked performance of test_services by going from fetching one result to 0.
This commit is contained in:
ade@google.com
2011-01-11 18:15:57 +00:00
parent f93b13d30a
commit e45ea6ccc4
3 changed files with 50 additions and 8 deletions

View File

@@ -12,14 +12,14 @@ class SimpleWrapper(object):
"Simple client that exposes the bare minimum set of common Buzz operations"
def __init__(self, api_key=None, credentials=None):
self.http = httplib2.Http()
if credentials:
logging.debug('Using api_client with credentials')
http = httplib2.Http()
http = credentials.authorize(http)
self.api_client = apiclient.discovery.build('buzz', 'v1', http=http, developerKey=api_key)
self.http = credentials.authorize(self.http)
self.api_client = apiclient.discovery.build('buzz', 'v1', http=self.http, developerKey=api_key)
else:
logging.debug('Using api_client that doesn\'t have credentials')
self.api_client = apiclient.discovery.build('buzz', 'v1', developerKey=api_key)
self.api_client = apiclient.discovery.build('buzz', 'v1', http=self.http, developerKey=api_key)
def search(self, query, user_token=None, max_results=10):
if query is None or query.strip() is '':
@@ -52,3 +52,19 @@ class SimpleWrapper(object):
def get_profile(self, user_id='@me'):
user_profile_data = self.api_client.people().get(userId=user_id).execute()
return user_profile_data
def get_follower_count(self, user_id='@me'):
return self.__get_group_count(user_id, '@followers')
def get_following_count(self, user_id='@me'):
return self.__get_group_count(user_id, '@following')
def __get_group_count(self, user_id, group_id):
# Fetching 0 results is a performance optimisation that minimises the
# amount of data that's getting retrieved from the server
cmd = self.api_client.people().list(userId=user_id, groupId=group_id,
max_results=0)
members = cmd.execute()
if 'totalResults' not in members.keys():
return -1
return members['totalResults']

View File

@@ -41,6 +41,24 @@ class SimpleWrapperTest(unittest.TestCase):
def test_wrapper_rejects_search_with_none(self):
wrapper = SimpleWrapper()
self.assertEquals(None, wrapper.search(None))
def test_wrapper_returns_minus_one_for_hidden_follower_count(self):
wrapper = SimpleWrapper()
self.assertEquals(-1, wrapper.get_follower_count(user_id='108242092577082601423'))
def test_wrapper_returns_positive_value_for_visible_follower_count(self):
wrapper = SimpleWrapper()
count = wrapper.get_follower_count(user_id='googlebuzz')
self.assertTrue(count > 0, "Got %s instead" % count)
def test_wrapper_returns_minus_one_for_hidden_following_count(self):
wrapper = SimpleWrapper()
self.assertEquals(-1, wrapper.get_following_count(user_id='108242092577082601423'))
def test_wrapper_returns_positive_value_for_visible_following_count(self):
wrapper = SimpleWrapper()
count = wrapper.get_following_count(user_id='googlebuzz')
self.assertTrue(count > 0, "Got %s instead" % count)
class SimpleWrapperRemoteTest(unittest.TestCase):
# These tests make remote calls
@@ -88,5 +106,13 @@ class SimpleWrapperRemoteTest(unittest.TestCase):
self.assertTrue(url is not None)
self.assertTrue(url.startswith('http://www.google.com/buzz/'))
def test_wrapper_returns_positive_value_for_hidden_follower_count_when_authorised(self):
count = self.wrapper.get_follower_count(user_id='108242092577082601423')
self.assertTrue(count > 0, "Got %s instead" % count)
def test_wrapper_returns_positive_value_for_hidden_following_count_when_authorised(self):
count = self.wrapper.get_following_count(user_id='108242092577082601423')
self.assertTrue(count > 0, "Got %s instead" % count)
if __name__ == '__main__':
unittest.main()

View File

@@ -123,19 +123,19 @@ class BuzzFunctionalTest(unittest.TestCase):
buzz = build('buzz', 'v1')
# Restricting max_results to 1 means only a tiny amount of data comes back but the totalResults still has the total.
following = buzz.people().list(userId='googlebuzz', groupId='@followers',
followers = buzz.people().list(userId='googlebuzz', groupId='@followers',
max_results='1').execute()
# @googlebuzz has a large but fluctuating number of followers
# It is sufficient if the result is bigger than 10, 000
follower_count = following['totalResults']
follower_count = followers['totalResults']
self.assertTrue(follower_count > 10000, follower_count)
def test_follower_count_is_missing_for_user_with_hidden_follower_count(self):
buzz = build('buzz', 'v1')
following = buzz.people().list(userId='adewale', groupId='@followers').execute()
followers = buzz.people().list(userId='adewale', groupId='@followers').execute()
self.assertFalse('totalResults' in following)
self.assertFalse('totalResults' in followers)
class BuzzAuthenticatedFunctionalTest(unittest.TestCase):