]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/api.py
Ran 2to3.
[z_archive/twitter.git] / twitter / api.py
index ddbe27f5eec29c1469898a233b34f7a571189c6a..9803c4b103cc4d6466193b0a7d83cf89ccaa2565 100644 (file)
@@ -1,6 +1,4 @@
-import urllib2
-
-from exceptions import Exception
+import urllib.request, urllib.error, urllib.parse
 
 from twitter.twitter_globals import POST_ACTIONS
 from twitter.auth import NoAuth
@@ -49,19 +47,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)
-
-    def __iter__(self):
-        return iter(self._real_response)
-
     @property
     def rate_limit_remaining(self):
         """
@@ -77,6 +65,24 @@ class TwitterResponse(object):
         return int(self.headers.getheader('X-RateLimit-Reset'))
 
 
+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):
+            if response_typ is not int:
+                response_typ.__init__(self, response)
+            TwitterResponse.__init__(self, headers)
+
+    return WrappedTwitterResponse(response)
+
+
+
 class TwitterCall(object):
     def __init__(
         self, auth, format, domain, uri="", agent=None,
@@ -104,7 +110,7 @@ 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))
+            uriparts.append(str(kwargs.pop(uripart, uripart)))
         uri = '/'.join(uriparts)
 
         method = "GET"
@@ -138,16 +144,16 @@ class TwitterCall(object):
             else:
                 body = arg_data
 
-        req = urllib2.Request(uriBase, body, headers)
+        req = urllib.request.Request(uriBase, body, headers)
 
         try:
-            handle = urllib2.urlopen(req)
+            handle = urllib.request.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)
-        except urllib2.HTTPError, e:
+                return wrap_response(str(handle.read()), handle.headers)
+        except urllib.error.HTTPError as e:
             if (e.code == 304):
                 return []
             else:
@@ -272,4 +278,4 @@ class Twitter(TwitterCall):
             secure=secure, uriparts=uriparts)
 
 
-__all__ = ["Twitter", "TwitterError", "TwitterHTTPError"]
+__all__ = ["Twitter", "TwitterError", "TwitterHTTPError", "TwitterResponse"]