+ fmt = ("." + self.format) if self.format else ""
+ return (
+ "Twitter sent status %i for URL: %s%s using parameters: "
+ "(%s)\ndetails: %s" % (
+ self.e.code, self.uri, fmt, self.uriparts,
+ self.response_data))
+
+
+class TwitterResponse(object):
+ """
+ Response from a twitter request. Behaves like a list or a string
+ (depending on requested format) but it has a few other interesting
+ attributes.
+
+ `headers` gives you access to the response headers as an
+ httplib.HTTPHeaders instance. You can do
+ `response.headers.get('h')` to retrieve a header.
+ """
+
+ @property
+ def rate_limit_remaining(self):
+ """
+ Remaining requests in the current rate-limit.
+ """
+ return int(self.headers.get('X-Rate-Limit-Remaining', "0"))
+
+ @property
+ def rate_limit_limit(self):
+ """
+ The rate limit ceiling for that given request.
+ """
+ return int(self.headers.get('X-Rate-Limit-Limit', "0"))
+
+ @property
+ def rate_limit_reset(self):
+ """
+ Time in UTC epoch seconds when the rate limit will reset.
+ """
+ return int(self.headers.get('X-Rate-Limit-Reset', "0"))
+
+
+class TwitterDictResponse(dict, TwitterResponse):
+ pass
+
+
+class TwitterListResponse(list, TwitterResponse):
+ pass
+
+
+def wrap_response(response, headers):
+ response_typ = type(response)
+ if response_typ is dict:
+ res = TwitterDictResponse(response)
+ res.headers = headers
+ elif response_typ is list:
+ res = TwitterListResponse(response)
+ res.headers = headers
+ else:
+ res = response
+ return res
+