]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/api.py
Merge git://github.com/chexov/twitter into list_action_merge
[z_archive/twitter.git] / twitter / api.py
index f07b77d4a3c37d240ee165d9fbb522f9e575ec75..19a561570463f0545674a01dff3b37c999dff866 100644 (file)
@@ -49,16 +49,9 @@ class TwitterResponse(object):
     httplib.HTTPHeaders instance. You can do
     `response.headers.getheader('h')` to retrieve a header.
     """
-    def __init__(self, real_response, headers):
-        self._real_response = real_response
+    def __init__(self, headers):
         self.headers = headers
 
-    def __getattr__(self, k):
-        try:
-            return object.__getattr__(self, k)
-        except AttributeError:
-            return getattr(self._real_response, k)
-
     @property
     def rate_limit_remaining(self):
         """
@@ -74,15 +67,21 @@ class TwitterResponse(object):
         return int(self.headers.getheader('X-RateLimit-Reset'))
 
 
-# Multiple inheritance makes my inner Java nerd cry. Why can't I just
-# add arbitrary attributes to list or str objects?! Guido, we need to
-# talk.
-class TwitterJsonResponse(list, TwitterResponse):
-    __doc__ = """Twitter JSON Response
-    """ + TwitterResponse.__doc__
-class TwitterXmlResponse(str, TwitterResponse):
-    __doc__ = """Twitter XML Response
-    """ + TwitterResponse.__doc__
+def wrap_response(response, headers):
+    response_typ = type(response)
+    if response_typ is bool:
+        # HURF DURF MY NAME IS PYTHON AND I CAN'T SUBCLASS bool.
+        response_typ = int
+
+    class WrappedTwitterResponse(response_typ, TwitterResponse):
+        __doc__ = TwitterResponse.__doc__
+
+        def __init__(self, response):
+            response_typ.__init__(self, response)
+            TwitterResponse.__init__(self, headers)
+
+    return WrappedTwitterResponse(response)
+
 
 
 class TwitterCall(object):
@@ -112,8 +111,8 @@ class TwitterCall(object):
         for uripart in self.uriparts:
             # If this part matches a keyword argument, use the
             # supplied value otherwise, just use the part.
-            uriparts.append(kwargs.pop(uripart, uripart))
-        uri = '/'.join(uriparts)
+            uriparts.append(unicode(kwargs.pop(uripart, uripart)))
+        uri = u'/'.join(uriparts)
 
         method = "GET"
         for action in POST_ACTIONS:
@@ -151,9 +150,10 @@ class TwitterCall(object):
         try:
             handle = urllib2.urlopen(req)
             if "json" == self.format:
-                return TwitterJsonResponse(json.loads(handle.read()))
+                res = json.loads(handle.read())
+                return wrap_response(res, handle.headers)
             else:
-                return TwitterXmlResponse(handle.read())
+                return wrap_response(str(handle.read()), handle.headers)
         except urllib2.HTTPError, e:
             if (e.code == 304):
                 return []
@@ -279,5 +279,4 @@ class Twitter(TwitterCall):
             secure=secure, uriparts=uriparts)
 
 
-__all__ = ["Twitter", "TwitterError", "TwitterHTTPError", "TwitterJsonResponse",
-           "TwitterXmlResponse"]
+__all__ = ["Twitter", "TwitterError", "TwitterHTTPError", "TwitterResponse"]