barrybrianbarrios / python-twitter

Automatically exported from code.google.com/p/python-twitter
Apache License 2.0
0 stars 0 forks source link

raise more specific exception when rate-limited / throttled #148

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. call twitter.Api.GetUserTimeline(user) frequently
2. get rate throttled
3. observe the exception thrown by twitter.py to be urllib2.HTTPError: HTTP 
Error 400: Bad Request

What is the expected output? What do you see instead?

I'd like to see a TwitterError exception thrown that contains the details of 
the rate throtteling (including the time left until one is unthrottled etc.).

This might make use of 
http://code.google.com/p/python-twitter/issues/detail?id=125

It is of course possible that one encounters a 400 error under other 
circumstances, but it'd be nice if twitter.py could catch the HTTPError, check 
if it's 400, check the RateLimit (as per Issue 125) and if that indicates that 
one is being throttled, raise an appropriate TwitterError.

A dumb way of doing this is along the lines of:

    json = ""
    try:
      json = self._FetchUrl(url, parameters=parameters)
    except urllib2.HTTPError, e:
      url = 'http://api.twitter.com/1/account/rate_limit_status.json'
      json = self._FetchUrl(url)
      data = simplejson.loads(json)
      raise TwitterError(data)

wherever _FetchUrl is called.

The caller can then catch this exception and react on the values (such as 
reset_time_in_seconds and sleep until then, for example).

Original issue reported on code.google.com by jscha...@netmeister.org on 18 Jul 2010 at 11:26