From dcce34d9d75ca2a011813b4877dd4f1893f2c0f0 Mon Sep 17 00:00:00 2001 From: Josh Roesslein Date: Fri, 6 Nov 2009 16:39:45 -0600 Subject: [PATCH] Use versioned API and the api.twitter.com domain. --- CHANGES | 1 + tests.py | 11 ++++---- tweepy/api.py | 73 ++++++++++++++++++++++-------------------------- tweepy/auth.py | 8 +++--- tweepy/binder.py | 15 +++++----- 5 files changed, 53 insertions(+), 55 deletions(-) diff --git a/CHANGES b/CHANGES index 91e2277..1331c73 100644 --- a/CHANGES +++ b/CHANGES @@ -12,6 +12,7 @@ during upgrade will be listed here. + Removed 'email' parameter from API.update_profile. No longer supported. + API.auth_handler -> API.auth + Moved memcache implementation to tweepy-more repository. ++ Tweepy now uses the versioned API and the new api.twitter.com subdomain 1.1 -> 1.2 [Current] ===================== diff --git a/tests.py b/tests.py index ee008ea..ec5c225 100644 --- a/tests.py +++ b/tests.py @@ -108,9 +108,9 @@ class TweepyAPITests(unittest.TestCase): self.api.followers_ids(username) def testverifycredentials(self): - self.assertEqual(self.api.verify_credentials(), True) + self.assertNotEqual(self.api.verify_credentials(), False) - api = API.new('basic', 'bad', 'password') + api = API(BasicAuthHandler('bad', 'password')) self.assertEqual(api.verify_credentials(), False) def testratelimitstatus(self): @@ -139,24 +139,25 @@ class TweepyAPITests(unittest.TestCase): self.assertEqual(updated.profile_sidebar_fill_color, '000') self.assertEqual(updated.profile_sidebar_border_color, '000') + """ def testupateprofileimage(self): self.api.update_profile_image('examples/profile.png') def testupdateprofilebg(self): self.api.update_profile_background_image('examples/bg.png') + """ def testupdateprofile(self): original = self.api.me() profile = { 'name': 'Tweepy test 123', - 'email': 'test@example.com', 'url': 'http://www.example.com', 'location': 'pytopia', 'description': 'just testing things out' } updated = self.api.update_profile(**profile) self.api.update_profile( - name = original.name, email = 'hi@example.com', url = original.url, + name = original.name, url = original.url, location = original.location, description = original.description ) @@ -248,7 +249,7 @@ class TweepyAuthTests(unittest.TestCase): # test getting access token auth_url = auth.get_authorization_url() - self.assert_(auth_url.startswith('http://twitter.com/oauth/authorize?')) + self.assert_(auth_url.startswith('http://api.twitter.com/oauth/authorize?')) print 'Please authorize: ' + auth_url verifier = raw_input('PIN: ').strip() self.assert_(len(verifier) > 0) diff --git a/tweepy/api.py b/tweepy/api.py index b9337ac..9f90817 100644 --- a/tweepy/api.py +++ b/tweepy/api.py @@ -13,13 +13,16 @@ from tweepy.parsers import * class API(object): """Twitter API""" - def __init__(self, auth_handler=None, host='twitter.com', cache=None, - secure=False, api_root='', + def __init__(self, auth_handler=None, + host='api.twitter.com', search_host='search.twitter.com', + cache=None, secure=False, api_root='/1', search_root='', retry_count=0, retry_delay=0, retry_errors=None): # you may access these freely self.auth = auth_handler self.host = host + self.search_host = search_host self.api_root = api_root + self.search_root = search_root self.cache = cache self.secure = secure self.retry_count = retry_count @@ -572,53 +575,45 @@ class API(object): return False """ search """ - - def search(self, *args, **kargs): - return bind_api( - host = 'search.' + self.host, - path = '/search.json', - parser = parse_search_results, - allowed_param = ['q', 'lang', 'locale', 'rpp', 'page', 'since_id', 'geocode', 'show_user'], - )(self, *args, **kargs) - search.pagination_mode = 'page' + search = bind_api( + search_api = True, + path = '/search.json', + parser = parse_search_results, + allowed_param = ['q', 'lang', 'locale', 'rpp', 'page', 'since_id', 'geocode', 'show_user'] + ) """ trends """ - def trends(self): - return bind_api( - host = 'search.' + self.host, - path = '/trends.json', - parser = parse_json - )(self) + trends = bind_api( + search_api = True, + path = '/trends.json', + parser = parse_json + ) """ trends/current """ - def trends_current(self, *args, **kargs): - return bind_api( - host = 'search.' + self.host, - path = '/trends/current.json', - parser = parse_json, - allowed_param = ['exclude'] - )(self, *args, **kargs) + trends_current = bind_api( + search_api = True, + path = '/trends/current.json', + parser = parse_json, + allowed_param = ['exclude'] + ) """ trends/daily """ - def trends_daily(self, *args, **kargs): - return bind_api( - host = "search." + self.host, - path = '/trends/daily.json', - parser = parse_json, - allowed_param = ['date', 'exclude'] - )(self, *args, **kargs) + trends_daily = bind_api( + search_api = True, + path = '/trends/daily.json', + parser = parse_json, + allowed_param = ['date', 'exclude'] + ) """ trends/weekly """ - def trends_weekly(self, *args, **kargs): - return bind_api( - host = "search." + self.host, - path = '/trends/weekly.json', - parser = parse_json, - allowed_param = ['date', 'exclude'] - )(self, *args, **kargs) + trends_weekly = bind_api( + search_api = True, + path = '/trends/weekly.json', + parser = parse_json, + allowed_param = ['date', 'exclude'] + ) """ Internal use only """ - @staticmethod def _pack_image(filename, max_size): """Pack image from file into multipart-formdata post body""" diff --git a/tweepy/auth.py b/tweepy/auth.py index 0a3c299..ee82efa 100644 --- a/tweepy/auth.py +++ b/tweepy/auth.py @@ -37,10 +37,10 @@ class BasicAuthHandler(AuthHandler): class OAuthHandler(AuthHandler): """OAuth authentication handler""" - REQUEST_TOKEN_URL = 'http://twitter.com/oauth/request_token' - AUTHORIZATION_URL = 'http://twitter.com/oauth/authorize' - AUTHENTICATE_URL = 'http://twitter.com/oauth/authenticate' - ACCESS_TOKEN_URL = 'http://twitter.com/oauth/access_token' + REQUEST_TOKEN_URL = 'http://api.twitter.com/oauth/request_token' + AUTHORIZATION_URL = 'http://api.twitter.com/oauth/authorize' + AUTHENTICATE_URL = 'http://api.twitter.com/oauth/authenticate' + ACCESS_TOKEN_URL = 'http://api.twitter.com/oauth/access_token' def __init__(self, consumer_key, consumer_secret, callback=None): self._consumer = oauth.OAuthConsumer(consumer_key, consumer_secret) diff --git a/tweepy/binder.py b/tweepy/binder.py index 052af22..2df6e54 100644 --- a/tweepy/binder.py +++ b/tweepy/binder.py @@ -22,7 +22,7 @@ except ImportError: def bind_api(path, parser, allowed_param=[], method='GET', require_auth=False, - timeout=None, host=None): + timeout=None, search_api = False): def _call(api, *args, **kargs): # If require auth, throw exception if credentials not provided @@ -66,10 +66,11 @@ def bind_api(path, parser, allowed_param=[], method='GET', require_auth=False, parameters = None # Build url with parameters + api_root = api.api_root if search_api is False else api.search_root if parameters: - url = '%s?%s' % (api.api_root + path, urllib.urlencode(parameters)) + url = '%s?%s' % (api_root + path, urllib.urlencode(parameters)) else: - url = api.api_root + path + url = api_root + path # Check cache if caching enabled and method is GET if api.cache and method == 'GET': @@ -89,7 +90,7 @@ def bind_api(path, parser, allowed_param=[], method='GET', require_auth=False, scheme = 'https://' else: scheme = 'http://' - _host = host or api.host + host = api.host if search_api is False else api.search_host # Continue attempting request until successful # or maximum number of retries is reached. @@ -98,14 +99,14 @@ def bind_api(path, parser, allowed_param=[], method='GET', require_auth=False, # Open connection # FIXME: add timeout if api.secure: - conn = httplib.HTTPSConnection(_host) + conn = httplib.HTTPSConnection(host) else: - conn = httplib.HTTPConnection(_host) + conn = httplib.HTTPConnection(host) # Apply authentication if api.auth: api.auth.apply_auth( - scheme + _host + url, + scheme + host + url, method, headers, parameters ) -- 2.25.1