]> jfr.im git - z_archive/twitter.git/commitdiff
Allow to set the number of retries
authorBeat Bolli <redacted>
Sat, 20 Sep 2014 12:16:00 +0000 (14:16 +0200)
committerBeat Bolli <redacted>
Sat, 20 Sep 2014 12:16:00 +0000 (14:16 +0200)
README
twitter/api.py

diff --git a/README b/README
index f871629cf60f9c9325aa1cca58d2b973e8091c29..521eff7af7b2ddb84ffb9aca12f763a22c924ede 100644 (file)
--- a/README
+++ b/README
@@ -146,6 +146,7 @@ Retrying after reaching the API rate limit
 
 Simply create the `Twitter` instance with the argument `retry=True`, then the
 HTTP error codes 429, 502, 503 and 504 will cause a retry of the last request.
+If retry is an integer, it defines the number of retries attempted.
 
 
 Using the data returned
index fe44efbb6ceec9c4f406d37e2692679f1c1ff497..4b9d459e74407a8db2bafb4e006cc813c35f3f1e 100644 (file)
@@ -306,7 +306,8 @@ class TwitterCall(object):
                 raise TwitterHTTPError(e, uri, self.format, arg_data)
 
     def _handle_response_with_retry(self, req, uri, arg_data, _timeout=None):
-        while True:
+        retry = self.retry
+        while retry:
             try:
                 return self._handle_response(req, uri, arg_data, _timeout)
             except TwitterHTTPError as e:
@@ -320,6 +321,10 @@ class TwitterCall(object):
                     print("Service unavailable; waiting for %ds..." % delay, file=sys.stderr)
                 else:
                     raise
+                if isinstance(retry, int):
+                    if retry <= 0:
+                        raise
+                    retry -= 1
                 sleep(delay)
 
 
@@ -453,7 +458,8 @@ class Twitter(TwitterCall):
 
         If `retry` is True, API rate limits will automatically be
         handled by waiting until the next reset, as indicated by
-        the X-Rate-Limit-Reset HTTP header.
+        the X-Rate-Limit-Reset HTTP header. If retry is an integer,
+        it defines the number of retries attempted.
         """
         if not auth:
             auth = NoAuth()