Skip to content

Commit

Permalink
Merge pull request #98 from SparkPost/ISSUE-86
Browse files Browse the repository at this point in the history
Add support for ip pools, inline images
  • Loading branch information
richleland committed Mar 30, 2016
2 parents 89d9f13 + d5bc6a7 commit 7be6972
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
27 changes: 27 additions & 0 deletions sparkpost/transmissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def _translate_keys(self, **kwargs):
model['options']['transactional'] = kwargs.get('transactional')
model['options']['sandbox'] = kwargs.get('use_sandbox')
model['options']['skip_suppression'] = kwargs.get('skip_suppression')
model['options']['ip_pool'] = kwargs.get('ip_pool')
model['options']['inline_css'] = kwargs.get('inline_css')

model['content']['use_draft_template'] = \
Expand Down Expand Up @@ -77,6 +78,10 @@ def _translate_keys(self, **kwargs):
model['content']['attachments'] = self._extract_attachments(
attachments)

inline_images = kwargs.get('inline_images', [])
model['content']['inline_images'] = self._extract_attachments(
inline_images)

return model

def _format_copies(self, recipients, copies):
Expand Down Expand Up @@ -176,6 +181,26 @@ def send(self, **kwargs):
name='document.pdf',
filename='/full/path/to/document.pdf'
)
:param inline_images: List of dicts. For example:
.. code-block:: python
dict(
type='image/png',
name='imageCID',
data='base64 encoded string'
)
Replace `data` with `filename` if you want the library to perform
the base64 conversion. For example:
.. code-block:: python
dict(
type='image/png',
name='imageCID',
filename='/full/path/to/image.png'
)
:param str start_time: Delay generation of messages until this
datetime. Format YYYY-MM-DDTHH:MM:SS+-HH:MM. Example:
Expand All @@ -192,6 +217,8 @@ def send(self, **kwargs):
:param bool skip_suppression: Whether or not to ignore customer
suppression rules, for this transmission only. Only applicable if
your configuration supports this parameter. (SparkPost Elite only)
:param str ip_pool: The name of a dedicated IP pool associated with
your account
:param bool inline_css: Whether or not to perform CSS inlining
:param dict custom_headers: Used to set any headers associated with
transmission
Expand Down
Binary file added test/assets/sparkpostdev.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions test/test_transmissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,48 @@ def test_success_send_with_attachments():
os.unlink(temp_file_path)


@responses.activate
def test_success_send_with_inline_images():
current_dir = os.path.abspath(os.path.dirname(__file__))
image_path = os.path.join(current_dir, 'assets', 'sparkpostdev.png')

with open(image_path, "rb") as image:
encoded_image = base64.b64encode(image.read()).decode("ascii")

responses.add(
responses.POST,
'https://api.sparkpost.com/api/v1/transmissions',
status=200,
content_type='application/json',
body='{"results": "yay"}'
)
sp = SparkPost('fake-key')

image_data = {
"name": "sparkpostdev",
"type": "image/png",
"filename": image_path
}
results = sp.transmission.send(inline_images=[image_data])
request_params = json.loads(responses.calls[0].request.body)
content = request_params["content"]["inline_images"][0]["data"]

assert encoded_image == content
assert results == 'yay'

image_data = {
"name": "sparkpostdev",
"type": "image/png",
"data": encoded_image
}
results = sp.transmission.send(inline_images=[image_data])
request_params = json.loads(responses.calls[1].request.body)
content = request_params["content"]["inline_images"][0]["data"]

assert content == encoded_image
assert results == 'yay'


@responses.activate
def test_fail_send():
responses.add(
Expand Down

0 comments on commit 7be6972

Please sign in to comment.