How to use the sparkpost.exceptions.SparkPostException 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_transmissions.py View on Github external
def test_exceptions_for_recipients():
    t = Transmissions('uri', 'key')
    with pytest.raises(SparkPostException):
        t._translate_keys(recipients='test')
github SparkPost / python-sparkpost / sparkpost / exceptions.py View on Github external
class SparkPostException(Exception):
    pass


class SparkPostAPIException(SparkPostException):
    "Handle 4xx and 5xx errors from the SparkPost API"
    def __init__(self, response, *args, **kwargs):
        # noinspection PyBroadException
        try:
            errors = response.json()['errors']
            error_template = "{message} Code: {code} Description: {desc} \n"
            errors = [error_template.format(message=e.get('message', ''),
                                            code=e.get('code', 'none'),
                                            desc=e.get('description', 'none'))
                      for e in errors]
        # TODO: select exception to catch here
        except:  # noqa: E722
            errors = [response.text or ""]
        self.status = response.status_code
        self.response = response
        self.errors = errors
github SparkPost / python-sparkpost / sparkpost / transmissions.py View on Github external
def _extract_recipients(self, recipients):

        if not (isinstance(recipients, (list, dict))):
            raise SparkPostException('recipients must be a list or dict')

        formatted_recipients = []
        for recip in recipients:
            if isinstance(recip, string_types):
                formatted_recipients.append({
                    'address': self._parse_address(recip)
                })
            else:
                formatted_recipients.append(recip)
        return formatted_recipients
github SparkPost / python-sparkpost / sparkpost / __init__.py View on Github external
def __init__(self, api_key=None, base_uri=US_API,
                 version='1'):
        "Set up the SparkPost API client"
        if not api_key:
            api_key = self.get_api_key()
            if not api_key:
                raise SparkPostException("No API key. Improve message.")

        self.base_uri = 'https://' + base_uri + '/api/v' + version
        self.api_key = api_key

        self.metrics = Metrics(self.base_uri, self.api_key,
                               self.TRANSPORT_CLASS)
        self.recipient_lists = RecipientLists(self.base_uri, self.api_key,
                                              self.TRANSPORT_CLASS)
        self.suppression_list = SuppressionList(self.base_uri, self.api_key,
                                                self.TRANSPORT_CLASS)
        self.templates = Templates(self.base_uri, self.api_key,
                                   self.TRANSPORT_CLASS)
        self.transmissions = Transmissions(self.base_uri, self.api_key,
                                           self.TRANSPORT_CLASS)
        # Keeping self.transmission for backwards compatibility.
        # Will be removed in a future release.