]> jfr.im git - z_archive/twitter.git/commitdiff
Old sanity tests now pass with the new api adapter.
authorMike Verdone <redacted>
Wed, 14 Nov 2012 21:32:25 +0000 (22:32 +0100)
committerMike Verdone <redacted>
Wed, 14 Nov 2012 21:32:25 +0000 (22:32 +0100)
tests/test_api2.py
tests/test_sanity.py
twitter/api.py
twitter/api2.py

index 0ca8f98a62af64135681a558e7543f9727af5913..d44dc1c0490835fb84bb27fefe883c95e82ebcf3 100644 (file)
@@ -6,23 +6,23 @@ from .test_sanity import oauth, get_random_str
 
 
 api = TwitterAPI(auth=oauth, secure=True)
-stream_api = TwitterAPI(host="stream.twitter.com", auth=oauth, stream=True,
+stream_api = TwitterAPI(domain="stream.twitter.com", auth=oauth, stream=True,
                         secure=True)
 
 
-def test_can_get_public_tweet_json():
-    updates = get('statuses/public_timeline')
-    assert updates
-    assert updates[0]['text']
+#def test_can_get_public_tweet_json():
+#    updates = get('statuses/public_timeline')
+#    assert updates
+#    assert updates[0]['text']
 
 
 def test_can_get_specific_status():
-    update = get('statuses/show/:id', id=230400277440770048)
+    update = api.get('statuses/show/:id', id=230400277440770048)
     assert update
 
 
 def test_can_perform_a_search():
-    results = TwitterAPI(host="search.twitter.com", api_ver=None).get(
+    results = TwitterAPI(domain="search.twitter.com", api_version=None).get(
         'search', q="hello")
     assert results
 
@@ -54,7 +54,7 @@ def test_handle_not_authenticated():
 
 
 def test_post_a_new_tweet():
-    api.post("statuses/update", status=get_random_str() + "ïõ")
+    api.post("statuses/update", status=get_random_str() + u"ïõ")
 
 
 def test_can_get_raw_response():
index 941d2870767551e5dad351508888ee5d1da048ee..aa9690c01bcf70ea3bb2c59503f4746998562193 100644 (file)
@@ -16,7 +16,7 @@ twitter = Twitter(domain='api.twitter.com',
 twitter11 = Twitter(domain='api.twitter.com',
                     auth=oauth,
                     api_version='1.1')
-twitter_na = Twitter(domain='api.twitter.com', auth=noauth, api_version='1')
+twitter_na = Twitter(domain='api.twitter.com', auth=noauth, api_version='1.1')
 
 
 AZaz = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
index e3f9ba72bb8c42c18acbefaddcf169b345dd3683..c2ac50f18357a55e8d562d723bd2821f86f2cfcc 100644 (file)
@@ -221,7 +221,7 @@ class TwitterCall(object):
             else:
                 raise TwitterHTTPError(e, uri, self.format, arg_data)
 
-class Twitter(TwitterCall):
+class OldTwitter(TwitterCall):
     """
     Get RESTful data by accessing members of this class. The result
     is decoded python objects (lists and dicts).
@@ -354,41 +354,48 @@ class Twitter(TwitterCall):
             callable_cls=TwitterCall,
             secure=secure, uriparts=uriparts)
 
-class TwitterData(object):
-    def __init__(
-        self, format="json",
-        domain="api.twitter.com", secure=True, auth=None,
-        api_version=_DEFAULT):
-
-        if not auth:
-            auth = NoAuth()
-
-        if (format not in ("json", "xml", "")):
-            raise ValueError("Unknown data format '%s'" %(format))
-
-        if api_version is _DEFAULT:
-            if domain == 'api.twitter.com':
-                api_version = '1.1'
-            else:
-                api_version = None
-
-        self.auth = auth
-        self.format = format
-        self.domain = domain
-        self.api_version = api_version
-        self.secure = secure
 
+from api2 import TwitterAPI
 
 class TwitterExtender(object):
-    def __init__(self, data, urlparts):
-        self.data = data
+    def __init__(self, api, urlparts):
+        self.api = api
         self.urlparts = urlparts
 
     def __getattr__(self, k):
         try:
             return object.__getattr__(self, k)
         except AttributeError:
-            return TwitterExtender(self.data, urlparts + (k,))
+            return TwitterExtender(self.api, self.urlparts + (k,))
+
+    def __call__(self, **kwargs):
+        meth = (self.api.post
+                if kwargs.pop('_method', 'GET').lower() == 'post'
+                   or (self.urlparts and self.urlparts[-1] in POST_ACTIONS)
+                else self.api.get)
+        id = kwargs.pop('_id', None)
+        if id:
+            kwargs['id'] = id
+
+        urlparts = list(self.urlparts)
+        for arg in list(kwargs.keys()):
+            if arg.startswith('_') and arg in urlparts:
+                urlparts[urlparts.index(arg)] = str(kwargs.pop(arg))
+        uri = "/".join(urlparts)
+
+        res = meth(uri, **kwargs)
+        return wrap_response(json.loads(res.content), res.headers)
+
+    def _(self, arg):
+        return getattr(self, str(arg))
+
+
+def Twitter(**kwargs):
+    if kwargs.get('domain') == 'search.twitter.com' and kwargs.get('api_version') is None:
+        kwargs['api_version'] = None
+    api = TwitterAPI(return_raw_response=True, **kwargs)
+    return TwitterExtender(api, ())
+Twitter.__doc__ = OldTwitter.__doc__
 
 
 __all__ = ["Twitter", "TwitterError", "TwitterHTTPError", "TwitterResponse"]
index 4d9d093b5f6b498812ea833d97ff287e3bf9ce4d..6ca1201d3688212e7094ff1a23cc78e9b942191f 100644 (file)
@@ -98,14 +98,14 @@ class TwitterAPIError(TwitterError):
 
 
 class TwitterAPI(object):
-    def __init__(self, host='api.twitter.com', api_ver='1', auth=NoAuth(),
+    def __init__(self, domain='api.twitter.com', api_version='1.1', auth=NoAuth(),
                  secure=True, stream=False, return_raw_response=False):
         """
         `host`        host to connect to (api.twitter.com)
         `api_ver`     API version
         """
-        self.host = host
-        self.api_ver = api_ver
+        self.domain = domain
+        self.api_ver = api_version
         self.auth = auth
         self.secure = secure
         self.stream = stream
@@ -113,7 +113,7 @@ class TwitterAPI(object):
 
 
     def get(self, path, **kwargs):
-        url, remaining_params = make_url(self.secure, self.host, self.api_ver,
+        url, remaining_params = make_url(self.secure, self.domain, self.api_ver,
                                          path, kwargs)
         data = self.auth.encode_params(url, 'GET', remaining_params)
         headers = self.auth.generate_headers()
@@ -122,7 +122,7 @@ class TwitterAPI(object):
 
 
     def post(self, path, **kwargs):
-        url, remaining_params = make_url(self.secure, self.host, self.api_ver,
+        url, remaining_params = make_url(self.secure, self.domain, self.api_ver,
                                          path, kwargs)
         data = self.auth.encode_params(url, 'POST', remaining_params)
         headers = self.auth.generate_headers()
@@ -135,7 +135,7 @@ _default_api = TwitterAPI()
 get = _default_api.get
 
 
-_search_api = TwitterAPI(host="search.twitter.com", api_ver=None)
+_search_api = TwitterAPI(domain="search.twitter.com", api_version=None)
 
 def search(q, **kwargs):
     return _search_api.get("search", q=q, **kwargs)
@@ -174,3 +174,4 @@ def handle_res(res, return_raw_response, stream):
     else:
         result = res.json
     return result
+