Skip to content

Commit

Permalink
Merge pull request #127 from SparkPost/tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
richleland committed Nov 13, 2016
2 parents b78b2df + bfc4cf7 commit 99873b5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions sparkpost/suppression_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def list(self, **kwargs):
'from_date': 'from',
'to_date': 'to',
'types': 'types',
'sources': 'sources',
'limit': 'limit'
}
params = dict([(key_map[i], kwargs[i]) for i in list(key_map.keys())
Expand Down
18 changes: 15 additions & 3 deletions sparkpost/transmissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def _translate_keys(self, **kwargs):
model['recipients']['list_id'] = recipient_list
else:
recipients = kwargs.get('recipients', [])
recipients = self._extract_recipients(recipients)
cc = kwargs.get('cc')
bcc = kwargs.get('bcc')

Expand All @@ -72,7 +73,7 @@ def _translate_keys(self, **kwargs):
bcc_copies = self._format_copies(recipients, bcc)
recipients = recipients + bcc_copies

model['recipients'] = self._extract_recipients(recipients)
model['recipients'] = recipients

attachments = kwargs.get('attachments', [])
model['content']['attachments'] = self._extract_attachments(
Expand All @@ -90,9 +91,19 @@ def _format_copies(self, recipients, copies):
if len(recipients) > 0:
formatted_copies = self._extract_recipients(copies)
for recipient in formatted_copies:
recipient['address'].update({'header_to': recipients[0]})
recipient['address'].update({
'header_to': self._format_header_to(recipients[0])
})
return formatted_copies

def _format_header_to(self, recipient):
if 'name' in recipient['address']:
return '"{name}" <{email}>'.format(
name=recipient['address']['name'],
email=recipient['address']['email']
)
return recipient['address']['email']

def _extract_attachments(self, attachments):
formatted_attachments = []
for attachment in attachments:
Expand Down Expand Up @@ -230,7 +241,8 @@ def send(self, **kwargs):
"""

payload = self._translate_keys(**kwargs)
results = self.request('POST', self.uri, data=json.dumps(payload))
results = self.request('POST', self.uri,
data=json.dumps(payload, ensure_ascii=False))
return results

def _fetch_get(self, transmission_id):
Expand Down
36 changes: 36 additions & 0 deletions test/test_transmissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,42 @@ def test_translate_keys_for_from_email():
}


def test_format_header_to():
t = Transmissions('uri', 'key')
formatted = t._format_header_to(recipient={
'address': {'email': 'primary@example.com'}
})
assert formatted == 'primary@example.com'

formatted = t._format_header_to(recipient={
'address': {'name': 'Testing', 'email': 'primary@example.com'}
})
assert formatted == '"Testing" <primary@example.com>'


def test_cc_with_sub_data():
t = Transmissions('uri', 'key')
results = t._translate_keys(
recipients=[{
'address': {'email': 'primary@example.com'},
'substitution_data': {'fake': 'data'}
}],
cc=['ccone@example.com']
)
assert results['recipients'] == [
{
'address': {'email': 'primary@example.com'},
'substitution_data': {'fake': 'data'}
},
{
'address': {
'email': 'ccone@example.com',
'header_to': 'primary@example.com'
}
}
]


def test_translate_keys_with_cc():
t = Transmissions('uri', 'key')
results = t._translate_keys(recipients=['primary@example.com'],
Expand Down

0 comments on commit 99873b5

Please sign in to comment.