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_not_implemented(self, api_client):
"""Not implemented error message returned."""
runner = CliRunner()
expected_output = "Error: 'alerts' subcommand is not implemented yet.\n"
api_client.not_implemented.side_effect = RequestFailure(501)
result = runner.invoke(subcommand.alerts)
api_client.not_implemented.assert_called_with("alerts")
assert result.exit_code == 1
assert result.output == expected_output
def test_status_code_failure(self, client, status_code):
"""Exception is raised on response status code failure."""
client.session = Mock()
response = client.session.get()
response.status_code = status_code
response.headers.get.return_value = "application/json"
with pytest.raises(RequestFailure):
client._request("endpoint")
def test_request_failure(self, api_client):
"""Error is displayed on API request failure."""
runner = CliRunner()
api_client.filter.side_effect = RequestFailure(
401, {"error": "forbidden", "status": "error"}
)
expected = "API error: forbidden\n"
result = runner.invoke(subcommand.filter, input="some text")
assert result.exit_code == -1
assert result.output == expected
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
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
def wrapper(api_client, *args, **kwargs):
command_name = function.__name__
try:
api_client.not_implemented(command_name)
except RequestFailure:
raise SubcommandNotImplemented(command_name)