Skip to content

Commit

Permalink
Add a new response class that handles json dicts.
Browse files Browse the repository at this point in the history
  • Loading branch information
sixohsix committed Jul 28, 2010
1 parent aef72b3 commit 452d1d1
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions twitter/api.py
Expand Up @@ -70,12 +70,18 @@ def rate_limit_reset(self):
# 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(TwitterResponse, list):
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
Expand Down Expand Up @@ -148,8 +154,11 @@ def __call__(self, **kwargs):
try:
handle = urllib2.urlopen(req)
if "json" == self.format:
return TwitterJsonResponse(json.loads(handle.read()),
handle.headers)
res = json.loads(handle.read())
response_cls = (
TwitterJsonListResponse if type(res) is list
else TwitterJsonDictResponse)
return response_cls(res, handle.headers)
else:
r = TwitterXmlResponse(handle.read())
r.headers = handle.headers
Expand Down Expand Up @@ -279,5 +288,6 @@ def __init__(
secure=secure, uriparts=uriparts)


__all__ = ["Twitter", "TwitterError", "TwitterHTTPError", "TwitterJsonResponse",
__all__ = ["Twitter", "TwitterError", "TwitterHTTPError",
"TwitterJsonListResponse", "TwitterJsonDictResponse",
"TwitterXmlResponse"]

0 comments on commit 452d1d1

Please sign in to comment.