def test_after_three_attempts_rate_limit_errors(): """ Tests that if three rate limiting error codes are encountered in a row (with backoff), a custom exception will be thrown """ bad_response = Response() bad_response.status_code = 429 response_lambda = (lambda t, a: bad_response) try: check_for_rate_limiting(bad_response, response_lambda) assert False except RateLimitingException: assert True
def test_if_rate_limiting_returns_on_resolve(): """ Tests that, if a rate limiting http code is returned and the client retries the request after sleeping and a success code is returned, no rate limiting exception will be raised """ bad_response = Response() bad_response.status_code = 429 good_response = Response() good_response.status_code = 200 response_lambda = (lambda t, a: bad_response if a < 1 else good_response) try: check_for_rate_limiting(bad_response, response_lambda) assert True except RateLimitingException: assert False
def test_success_does_not_trigger_rate_limiting_retry(): """ Tests that the retry lambda is not called if the original request is successful """ response = Response() response.status_code = 200 response_lambda = (lambda t, a: 1/0) # will throw exception checked_resp = check_for_rate_limiting(response, response_lambda) assert response == checked_resp
def _get(self, route, headers=None, failure_message=None): """ Execute a post request and return the result :param headers: :return: """ headers = self._get_headers(headers) response_lambda = (lambda: requests.get( self._get_qualified_route(route), headers=headers, verify=False)) response = check_for_rate_limiting(response_lambda(), response_lambda) return self._handle_response(response, failure_message)
def _patch(self, route, data, headers=None, failure_message=None): """ Execute a patch request and return the result """ headers = self._get_headers(headers) response_lambda = ( lambda: requests.patch(self._get_qualified_route(route), headers=headers, data=data, verify=False, proxies=self.proxies)) response = check_for_rate_limiting(response_lambda(), response_lambda) return self._handle_response(response, failure_message)