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

Added Recipient Lists resource lib #48

Merged
merged 6 commits into from
Sep 1, 2015
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions docs/api/recipient_lists.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. module:: sparkpost.recipient_lists

:mod:`sparkpost.recipient_lists`
=============================

.. autoclass:: RecipientLists
:members:
83 changes: 83 additions & 0 deletions docs/resources/recipient_lists.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
Recipient Lists
=============

Let's use the underlying `recipient_lists API`_ to create a recipient list:

.. code-block:: python

from sparkpost import SparkPost

sp = SparkPost()

response = sp.recipient_lists.create(
id='UNIQUE_TEST_ID',
name='Test Recipient list',
recipients=[
{
'address': {
'email': 'test1@test.com'
}
},
{
'address': {
'email': 'test2@test.com'
}
},
{
'address': {
'email': 'test3@test.com'
}
}
]
)

print response
# outputs {u'total_accepted_recipients': 3, u'id': u'UNIQUE_TEST_ID', u'total_rejected_recipients': 0, u'name':'Test Recipient list'}

.. _recipient_lists API: https://www.sparkpost.com/api#/reference/recipient-lists


Retrieve a recipient list
-----------------------

.. code-block:: python

from sparkpost import SparkPost

sp = SparkPost()

sp.recipient_lists.get('my-list-id')


List all recipient lists
----------------------

.. code-block:: python

from sparkpost import SparkPost

sp = SparkPost()

sp.recipient_lists.list()


API reference
-------------

:doc:`/api/recipient_lists`


Further examples
----------------

See the `python-sparkpost recipient_lists examples`_.

.. _python-sparkpost recipient_lists examples: https://github.com/SparkPost/python-sparkpost/tree/master/examples/recipient_lists


Additional documentation
------------------------

See the `SparkPost Recipient List API Reference`_.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This link is broken in the HTML output by make docs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's Recipient List vs. Recipient Lists


.. _SparkPost Recipient Lists API Reference: https://www.sparkpost.com/api#/reference/recipient_lists
25 changes: 25 additions & 0 deletions examples/recipient_lists/create_recipient_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from sparkpost import SparkPost

sp = SparkPost('YOUR API KEY')
response = sp.recipient_lists.create(
id='UNIQUE_TEST_ID',
name='Test Recipient list',
recipients=[
{
'address': {
'email': 'test1@test.com'
}
},
{
'address': {
'email': 'test2@test.com'
}
},
{
'address': {
'email': 'test3@test.com'
}
}
]
)
print response
5 changes: 5 additions & 0 deletions examples/recipient_lists/delete_recipient_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from sparkpost import SparkPost

sp = SparkPost('YOUR API KEY')
result = sp.recipient_lists.delete('list_id')
print result
5 changes: 5 additions & 0 deletions examples/recipient_lists/get_recipient_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from sparkpost import SparkPost

sp = SparkPost('YOUR API KEY')
recipient_list = sp.recipient_lists.get('list_id')
print recipient_list
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from sparkpost import SparkPost

sp = SparkPost('YOUR API KEY')
recipient_list = sp.recipient_lists.get('list_id', True)
print recipient_list
5 changes: 5 additions & 0 deletions examples/recipient_lists/list_recipient_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from sparkpost import SparkPost

sp = SparkPost('YOUR API KEY')
recipient_lists = sp.recipient_lists.list()
print recipient_lists
25 changes: 25 additions & 0 deletions examples/recipient_lists/update_recipient_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from sparkpost import SparkPost

sp = SparkPost('YOUR API KEY')
response = sp.recipient_lists.update(
'EXISTING_TEST_ID',
name='Test Recipient list',
recipients=[
{
'address': {
'email': 'test1@test.com'
}
},
{
'address': {
'email': 'test2@test.com'
}
},
{
'address': {
'email': 'test3@test.com'
}
}
]
)
print response
4 changes: 2 additions & 2 deletions examples/templates/delete_template.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sparkpost import SparkPost

sp = SparkPost('YOUR API KEY')
template = sp.transmission.delete('template_id')
print template
result = sp.templates.delete('template_id')
print result
2 changes: 1 addition & 1 deletion examples/templates/get_template.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sparkpost import SparkPost

sp = SparkPost('YOUR API KEY')
template = sp.transmission.get('template_id')
template = sp.templates.get('template_id')
print template
2 changes: 1 addition & 1 deletion examples/templates/preview_draft_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
'first_name': 'John',
'last_name': 'Doe'
}
template = sp.transmission.preview('template_id', sub_data, True)
template = sp.templates.preview('template_id', sub_data, True)
print template
2 changes: 1 addition & 1 deletion examples/templates/preview_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
'first_name': 'John',
'last_name': 'Doe'
}
template = sp.transmission.preview('template_id', sub_data)
template = sp.templates.preview('template_id', sub_data)
print template
2 changes: 2 additions & 0 deletions sparkpost/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .exceptions import SparkPostException
from .metrics import Metrics
from .recipient_lists import RecipientLists
from .templates import Templates
from .transmissions import Transmissions

Expand All @@ -27,6 +28,7 @@ def __init__(self, api_key=None, base_uri='https://api.sparkpost.com',
self.api_key = api_key

self.metrics = Metrics(self.base_uri, self.api_key)
self.recipient_lists = RecipientLists(self.base_uri, self.api_key)
self.templates = Templates(self.base_uri, self.api_key)
self.transmissions = Transmissions(self.base_uri, self.api_key)
# Keeping self.transmission for backwards compatibility.
Expand Down
105 changes: 105 additions & 0 deletions sparkpost/recipient_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import json

from .base import Resource


class RecipientLists(Resource):
"""
RecipientLists class used to create, update, delete, list and get recipient
lists. For
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's with the weird line wrap?

detailed request and response formats, see the `Recipient Lists API
documentation <https://www.sparkpost.com/api#/reference/recipient-lists>`_.
"""

key = 'recipient-lists'

def _translate_keys(self, **kwargs):
model = {}

if 'id' in kwargs:
model['id'] = kwargs.get('id')
model['name'] = kwargs.get('name')
model['description'] = kwargs.get('description')
model['attributes'] = kwargs.get('attributes')
model['recipients'] = kwargs.get('recipients')

return model

def create(self, **kwargs):
"""
Create a recipient list based on the supplied parameters

:param str id: ID used to reference the recipient list
:param str name: Editable display name
:param str description: Detailed description of the recipient list
:param dict attributes: Arbitrary metadata related to the list
:param list recipients: Array of recipient dicts

:returns: a ``dict`` with the ID, name, and number of accepted
and rejected recipients
:raises: :exc:`SparkPostAPIException` if API call fails
"""
payload = self._translate_keys(**kwargs)
results = self.request('POST', self.uri, data=json.dumps(payload))
return results

def update(self, list_id, **kwargs):
"""
Update a recipient list by ID based on the supplied parameters

:param str list_id: ID of the recipient list you want to update
:param str name: Editable display name
:param str description: Detailed description of the recipient list
:param dict attributes: Arbitrary metadata related to the list
:param list recipients: Array of recipient dicts

:returns: a ``dict`` with the ID, name, and number of accepted
and rejected recipients
:raises: :exc:`SparkPostAPIException` if API call fails
"""
uri = "%s/%s" % (self.uri, list_id)
payload = self._translate_keys(**kwargs)
results = self.request('PUT', uri, data=json.dumps(payload))
return results

def delete(self, list_id):
"""
Delete a recipient list by ID

:param str list_id: ID of the recipient list you want to delete

:returns: TODO
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns True if successful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh nevermind - this is one of the APIs that returns 200 with {} - we should just put empty dict.

:raises: :exc:`SparkPostAPIException` if recipient list is not found
or if recipient list is in use
"""
uri = "%s/%s" % (self.uri, list_id)
results = self.request('DELETE', uri)
return results

def get(self, list_id, show_recipients=None):
"""
Get a recipient list by ID

:param str list_id: ID of the recipient list you want to retrieve
:param bool show_recipients: If True, returns attributes for
all recipients

:returns: the requested recipient list if found
:raises: :exc:`SparkPostAPIException` if recipient list is not found
"""
uri = "%s/%s" % (self.uri, list_id)
params = {}
if show_recipients is not None:
params['show_recipients'] = str(show_recipients).lower()
results = self.request('GET', uri, params=params)
return results

def list(self):
"""
Get a list of your recipient lists

:returns: list of recipient lists
:raises: :exc:`SparkPostAPIException` if API call fails
"""
results = self.request('GET', self.uri)
return results
Loading