Use versioned API and the api.twitter.com domain.
authorJosh Roesslein <jroesslein@gmail.com>
Fri, 6 Nov 2009 22:39:45 +0000 (16:39 -0600)
committerJosh Roesslein <jroesslein@gmail.com>
Fri, 6 Nov 2009 22:39:45 +0000 (16:39 -0600)
CHANGES
tests.py
tweepy/api.py
tweepy/auth.py
tweepy/binder.py

diff --git a/CHANGES b/CHANGES
index 91e2277c7bef3e7342b5155cfee02464ca5644b6..1331c73b19c02482851c50b2571ff2eed39d01a0 100644 (file)
--- 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]
 =====================
index ee008ead9a25f917534470a6c6ec770f27bf30fc..ec5c22554e1e46e5063d7a63bbb2c6090fadf9e5 100644 (file)
--- 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)
index b9337ac54910b69d5fa77dbd7ccca22c9911e4c2..9f90817015f24a641eb3c84b72618ca85e8d82a5 100644 (file)
@@ -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"""
index 0a3c299bd43a249e4cb78c887d13acd61f7480dd..ee82efab0f0bd594669dd1d2a2e045a5142b1684 100644 (file)
@@ -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)
index 052af22634f5ca9f015b9f2da6cc5bb25da7e705..2df6e54d519769ac09ed0a48d65148717c072d0e 100644 (file)
@@ -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
                 )