-
Notifications
You must be signed in to change notification settings - Fork 68
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
Changes from 3 commits
e9bd6f8
66a6801
a77c84a
f6fffab
4b7aabb
1ad3716
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: |
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`_. | ||
|
||
.. _SparkPost Recipient Lists API Reference: https://www.sparkpost.com/api#/reference/recipient_lists |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returns True if successful. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ugh nevermind - this is one of the APIs that returns 200 with |
||
: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 |
There was a problem hiding this comment.
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
.There was a problem hiding this comment.
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