Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ip pools, inline images #98

Merged
merged 3 commits into from
Mar 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading
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