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_exceptions_for_recipients():
t = Transmissions('uri', 'key')
with pytest.raises(SparkPostException):
t._translate_keys(recipients='test')
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
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
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.