We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 73a6310 + ae82084 commit c416c1cCopy full SHA for c416c1c
2 files changed
tests/test_rate_limit.py
@@ -231,3 +231,32 @@ def testLimitsViaHeadersWithSleep(self):
231
self.assertEqual(api.rate_limit.get_limit('/search/tweets').limit, 63)
232
self.assertEqual(api.rate_limit.get_limit('/search/tweets').remaining, 63)
233
self.assertEqual(api.rate_limit.get_limit('/search/tweets').reset, 626672700)
234
+
235
+ @responses.activate
236
+ def testLimitsViaHeadersWithSleepLimitReached(self):
237
+ api = twitter.Api(
238
+ consumer_key='test',
239
+ consumer_secret='test',
240
+ access_token_key='test',
241
+ access_token_secret='test',
242
+ sleep_on_rate_limit=True)
243
244
+ # Add handler for ratelimit check - this forces the codepath which goes through the time.sleep call
245
+ url = '%s/application/rate_limit_status.json?tweet_mode=compat' % api.base_url
246
+ responses.add(
247
+ method=responses.GET, url=url,
248
+ body='{"resources": {"search": {"/search/tweets": {"limit": 1, "remaining": 0, "reset": 1}}}}',
249
+ match_querystring=True)
250
251
+ # Get initial rate limit data to populate api.rate_limit object
252
+ url = "https://api.twitter.com/1.1/search/tweets.json?tweet_mode=compat&q=test&count=15&result_type=mixed"
253
254
+ method=responses.GET,
255
+ url=url,
256
+ body='{}',
257
+ match_querystring=True,
258
+ adding_headers=HEADERS)
259
260
+ resp = api.GetSearch(term='test')
261
+ self.assertTrue(api.rate_limit)
262
+ self.assertEqual(resp, [])
twitter/api.py
@@ -26,6 +26,7 @@
26
import time
27
import base64
28
import re
29
+import logging
30
import requests
31
from requests_oauthlib import OAuth1, OAuth2
32
import io
@@ -77,6 +78,7 @@
77
78
# A singleton representing a lazily instantiated FileCache.
79
DEFAULT_CACHE = object()
80
81
+logger = logging.getLogger(__name__)
82
83
class Api(object):
84
"""A python interface into the Twitter API
@@ -230,7 +232,7 @@ def __init__(self,
230
self._debugHTTP = debugHTTP
self._shortlink_size = 19
if timeout and timeout < 30:
- warn("Warning: The Twitter streaming API sends 30s keepalives, the given timeout is shorter!")
+ warnings.warn("Warning: The Twitter streaming API sends 30s keepalives, the given timeout is shorter!")
self._timeout = timeout
self.__auth = None
@@ -275,7 +277,6 @@ def __init__(self,
275
277
application_only_auth)
276
278
279
if debugHTTP:
- import logging
280
try:
281
import http.client as http_client # python3
282
except ImportError:
@@ -5051,7 +5052,9 @@ def _RequestUrl(self, url, verb, data=None, json=None, enforce_auth=True):
5051
5052
5053
if limit.remaining == 0:
5054
- time.sleep(max(int(limit.reset - time.time()) + 2, 0))
5055
+ stime = max(int(limit.reset - time.time()) + 2, 0)
5056
+ logger.debug('Rate limited requesting [%s], sleeping for [%s]', url, stime)
5057
+ time.sleep(stime)
5058
except ValueError:
5059
pass
5060
0 commit comments