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):
"""
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 TwitterJsonListResponse(TwitterResponse, list):
+ __doc__ = """Twitter JSON Response
+ """ + TwitterResponse.__doc__
+ def __init__(self, lst, headers):
+ TwitterResponse.__init__(self, headers)
+ list.__init__(self, lst)
+class TwitterJsonDictResponse(TwitterResponse, dict):
+ __doc__ = """Twitter JSON Response
+ """ + TwitterResponse.__doc__
+ def __init__(self, d, headers):
+ TwitterResponse.__init__(self, headers)
+ dict.__init__(self, d)
+
+class TwitterXmlResponse(TwitterResponse, str):
+ __doc__ = """Twitter XML Response
+ """ + TwitterResponse.__doc__
+
+
class TwitterCall(object):
def __init__(
self, auth, format, domain, uri="", agent=None,
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:
try:
handle = urllib2.urlopen(req)
if "json" == self.format:
- msg_data = json.loads(handle.read())
+ res = json.loads(handle.read())
+ response_cls = (
+ TwitterJsonListResponse if type(res) is list
+ else TwitterJsonDictResponse)
+ return response_cls(res, handle.headers)
else:
- msg_data = handle.read()
- return TwitterResponse(msg_data, handle.headers)
+ r = TwitterXmlResponse(handle.read())
+ r.headers = handle.headers
+ return r
except urllib2.HTTPError, e:
if (e.code == 304):
return []
secure=secure, uriparts=uriparts)
-__all__ = ["Twitter", "TwitterError", "TwitterHTTPError"]
+__all__ = ["Twitter", "TwitterError", "TwitterHTTPError",
+ "TwitterJsonListResponse", "TwitterJsonDictResponse",
+ "TwitterXmlResponse"]