Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_rate_limit_error(self, client):
"""Exception is raised on rate limit response."""
client.session = Mock()
response = client.session.get()
response.headers.get.return_value = "application/json"
response.status_code = 429
with pytest.raises(RateLimitError):
client._request("endpoint")
)
content_type = response.headers.get("Content-Type", "")
if "application/json" in content_type:
body = response.json()
else:
body = response.text
LOGGER.debug(
"API response received",
status_code=response.status_code,
content_type=content_type,
body=body,
)
if response.status_code == 429:
raise RateLimitError()
if response.status_code >= 400:
raise RequestFailure(response.status_code, body)
return body
}
url = "/".join([self.BASE_URL, self.API_VERSION, endpoint])
LOGGER.msg(
"Sending API request...", url=url, params=params, json=json, level="debug"
)
response = self.session.get(
url, headers=headers, timeout=self.timeout, params=params, json=json
)
if "application/json" in response.headers.get("Content-Type", ""):
body = response.json()
else:
body = response.text
if response.status_code == 429:
raise RateLimitError()
if response.status_code >= 400:
raise RequestFailure(response.status_code, body)
return body