]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/api.py
Use a generic wrap_response with dynamic class instead. This is kind of scary but...
[z_archive/twitter.git] / twitter / api.py
index 3dd578d0b52d71c7a6bb2d5dc021025c16193910..c46b52d4d9ce64c53b58afa5fb5fcbe36db9cc74 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,6 +67,19 @@ class TwitterResponse(object):
         return int(self.headers.getheader('X-RateLimit-Reset'))
 
 
+def wrap_response(response, headers):
+    response_typ = type(response)
+    class WrappedTwitterResponse(TwitterResponse, response_typ):
+        __doc__ = TwitterResponse.__doc__
+
+        def __init__(self, response, headers):
+            response_typ.__init__(self, response)
+            TwitterResponse.__init__(self, headers)
+
+    return WrappedTwitterResponse(response, headers)
+
+
+
 class TwitterCall(object):
     def __init__(
         self, auth, format, domain, uri="", agent=None,
@@ -101,8 +107,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:
@@ -140,10 +146,10 @@ class TwitterCall(object):
         try:
             handle = urllib2.urlopen(req)
             if "json" == self.format:
-                msg_data = json.loads(handle.read())
+                res = json.loads(handle.read())
+                return wrap_response(res, handle.headers)
             else:
-                msg_data = handle.read()
-            return TwitterResponse(msg_data, handle.headers)
+                return wrap_response(handle.read(), handle.headers)
         except urllib2.HTTPError, e:
             if (e.code == 304):
                 return []
@@ -269,4 +275,4 @@ class Twitter(TwitterCall):
             secure=secure, uriparts=uriparts)
 
 
-__all__ = ["Twitter", "TwitterError", "TwitterHTTPError"]
+__all__ = ["Twitter", "TwitterError", "TwitterHTTPError", "TwitterResponse"]