How to use the greynoise.exceptions.RequestFailure function in greynoise

To help you get started, we’ve selected a few greynoise examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github GreyNoise-Intelligence / pygreynoise / tests / cli / test_subcommand.py View on Github external
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
github GreyNoise-Intelligence / pygreynoise / tests / test_api.py View on Github external
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")
github GreyNoise-Intelligence / pygreynoise / tests / cli / test_subcommand.py View on Github external
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
github GreyNoise-Intelligence / pygreynoise / src / greynoise / api.py View on Github external
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
github GreyNoise-Intelligence / pygreynoise / src / greynoise / api / __init__.py View on Github external
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
github GreyNoise-Intelligence / pygreynoise / src / greynoise / cli / decorator.py View on Github external
def wrapper(api_client, *args, **kwargs):
        command_name = function.__name__
        try:
            api_client.not_implemented(command_name)
        except RequestFailure:
            raise SubcommandNotImplemented(command_name)