File: recipient_lists.py

package info (click to toggle)
python-sparkpost 1.3.10-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 512 kB
  • sloc: python: 2,528; makefile: 31; sh: 10
file content (105 lines) | stat: -rw-r--r-- 3,671 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import json

from .base import Resource


class RecipientLists(Resource):
    """
    RecipientLists class used to create, update, delete, list and get recipient
    lists. For 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: 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