How to use the sparkpost.exceptions.SparkPostAPIException function in sparkpost

To help you get started, we’ve selected a few sparkpost 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 SparkPost / python-sparkpost / test / test_templates.py View on Github external
def test_fail_create():
    responses.add(
        responses.POST,
        'https://api.sparkpost.com/api/v1/templates',
        status=500,
        content_type='application/json',
        body="""
        {"errors": [{"message": "You failed", "description": "More Info"}]}
        """
    )
    with pytest.raises(SparkPostAPIException):
        sp = SparkPost('fake-key')
        sp.templates.create()
github SparkPost / python-sparkpost / test / test_transmissions.py View on Github external
def test_fail_get():
    responses.add(
        responses.GET,
        'https://api.sparkpost.com/api/v1/transmissions/foobar',
        status=404,
        content_type='application/json',
        body="""
        {"errors": [{"message": "cant find", "description": "where you go"}]}
        """
    )
    with pytest.raises(SparkPostAPIException):
        sp = SparkPost('fake-key')
        sp.transmission.get('foobar')
github SparkPost / python-sparkpost / test / test_base.py View on Github external
def test_fail_request():
    responses.add(
        responses.GET,
        fake_uri,
        status=500,
        content_type='application/json',
        body='{"errors": [{"message": "failure", "description": "desc"}]}'
    )
    resource = create_resource()
    with pytest.raises(SparkPostAPIException):
        resource.request('GET', resource.uri)
github SparkPost / python-sparkpost / test / test_suppression_list.py View on Github external
def test_fail_get():
    responses.add(
        responses.GET,
        'https://api.sparkpost.com/api/v1/suppression-list/foobar',
        status=404,
        content_type='application/json',
        body="""
        {"errors": [{"message": "cant find", "description": "where you go"}]}
        """
    )
    with pytest.raises(SparkPostAPIException):
        sp = SparkPost('fake-key')
        sp.suppression_list.get('foobar')
github SparkPost / python-sparkpost / test / test_templates.py View on Github external
def test_fail_preview():
    responses.add(
        responses.POST,
        'https://api.sparkpost.com/api/v1/templates/foobar/preview',
        status=404,
        content_type='application/json',
        body="""
        {"errors": [{"message": "cant find", "description": "where you go"}]}
        """
    )
    with pytest.raises(SparkPostAPIException):
        sp = SparkPost('fake-key')
        sp.templates.preview('foobar', {})
github anymail / django-anymail / tests / test_sparkpost_backend.py View on Github external
def set_mock_failure(self, status_code=400, raw=b'{"errors":[{"message":"test error"}]}', encoding='utf-8'):
        from sparkpost.exceptions import SparkPostAPIException
        # Need to build a real(-ish) requests.Response for SparkPostAPIException
        response = requests.Response()
        response.status_code = status_code
        response.encoding = encoding
        response.raw = six.BytesIO(raw)
        response.url = "/mock/send"
        self.mock_send.side_effect = SparkPostAPIException(response)
github SparkPost / python-sparkpost / test / test_sending_domains.py View on Github external
def test_fail_get():
    responses.add(
        responses.GET,
        '%s/fake.com' % base_uri,
        status=404,
        content_type='application/json',
        body='{"errors": [{"message": "cannot find"}]}'
    )
    with pytest.raises(SparkPostAPIException):
        sp = SparkPost('fake-key')
        sp.sending_domains.get('fake.com')
github SparkPost / python-sparkpost / test / test_metrics.py View on Github external
def test_fail_domains():
    responses.add(
        responses.GET,
        'https://api.sparkpost.com/api/v1/metrics/domains',
        status=500,
        content_type='application/json',
        body="""
        {"errors": [{"message": "You failed", "description": "More Info"}]}
        """
    )
    with pytest.raises(SparkPostAPIException):
        sp = SparkPost('fake-key')
        sp.metrics.domains.list()
github SparkPost / python-sparkpost / sparkpost / exceptions.py View on Github external
# TODO: select exception to catch here
        except:  # noqa: E722
            errors = [response.text or ""]
        self.status = response.status_code
        self.response = response
        self.errors = errors
        message = """Call to {uri} returned {status_code}, errors:

        {errors}

        """.format(
            uri=response.url,
            status_code=response.status_code,
            errors='\n'.join(errors)
        )
        super(SparkPostAPIException, self).__init__(message, *args, **kwargs)
github SparkPost / python-sparkpost / sparkpost / base.py View on Github external
def request(self, method, uri, headers, **kwargs):
        response = self.sess.request(method, uri, headers=headers, **kwargs)
        if response.status_code == 204:
            return True
        if not response.ok:
            raise SparkPostAPIException(response)
        if 'results' in response.json():
            return response.json()['results']
        return response.json()