RFE: Make headers available from API calls?

36 views
Skip to first unread message

Johnson Earls

unread,
Jul 23, 2010, 9:20:26 AM7/23/10
to tweepy
Hello,

Perhaps this is already available somewhere and I just missed it in
the code.

I'm looking for a way to see the headers that Twitter sends back with
its response.

In particular, Twitter sends back X-RateLimit-* headers that would be
nice to see, as opposed to having to go back and send another
rate_limit_status request.

I've made a patch for my own copy of tweepy, which stores the headers
in the API object; however, I'm not sure if this model would work in
general, as it's not going to be thread-safe (and I'm not sure if the
rest of tweepy is threadsafe or not).

In any case, here's my patch for this.

--- api.py.ORIG 2010-07-23 06:01:01.205377000 -0700
+++ api.py 2010-07-23 06:04:45.805750000 -0700
@@ -30,6 +30,7 @@
self.retry_delay = retry_delay
self.retry_errors = retry_errors
self.parser = parser or ModelParser()
+ self._headers = {}

""" statuses/public_timeline """
public_timeline = bind_api(
@@ -692,6 +693,18 @@
allowed_param = ['id']
)

+ """ Return the first value of a header received from the last API
call. Return None if no such header. """
+ def header(self, header):
+ if self._headers.has_key(header):
+ return self._headers[header][0]
+ return None
+
+ """ Return the list of values of a header received from the last
API call. Return None if no such header. """
+ def headers(self, header):
+ if self._headers.has_key(header):
+ return self._headers[header]
+ return None
+
""" Internal use only """
@staticmethod
def _pack_image(filename, max_size):

--- binder.py.ORIG 2010-06-15 23:48:38.000000000 -0700
+++ binder.py 2010-07-23 06:00:07.192910000 -0700
@@ -117,6 +117,7 @@
result._api = self.api
else:
cache_result._api = self.api
+ self.api._headers = {} # headers are invalid
after cache!
return cache_result

# Continue attempting request until successful
@@ -165,6 +166,13 @@

# Parse the response payload
result = self.api.parser.parse(self, resp.read())
+ headers = {}
+ for (header,value) in resp.getheaders():
+ if headers.has_key(header):
+ headers[header] = headers[header] + (value,)
+ else:
+ headers[header] = (value,)
+ self.api._headers = headers

conn.close()

Reply all
Reply to author
Forward
0 new messages