|
| 1 | +from .base_endpoint import BaseEndpoint |
| 2 | + |
| 3 | + |
| 4 | +class ContactList(BaseEndpoint): |
| 5 | + """ |
| 6 | + Class representation of the ContactList endpoint. |
| 7 | +
|
| 8 | + Examples: |
| 9 | + If you want to use contact lists' methods through an instance of the Emarsys |
| 10 | + client: |
| 11 | + >>> from pymarsys import SyncConnection, Emarsys |
| 12 | + >>> connection = SyncConnection('username', 'password') |
| 13 | + >>> client = Emarsys(connection) |
| 14 | + >>> client.contact_list |
| 15 | + <pymarsys.contact_list.ContactList at 0x1050f7048> |
| 16 | +
|
| 17 | + If you want to use contact lists' methods trough an instance of the ContactList |
| 18 | + endpoint class: |
| 19 | + >>> from pymarsys import SyncConnection |
| 20 | + >>> from pymarsys.contact_list import ContactList |
| 21 | + >>> connection = SyncConnection('username', 'password') |
| 22 | + >>> contact_list = ContactList(connection) |
| 23 | + >>> contact_list |
| 24 | + <pymarsys.contact_list.ContactList at 0x10333ec88> |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self, connection, endpoint="api/v2/contactlist/"): |
| 28 | + super().__init__(connection, endpoint) |
| 29 | + |
| 30 | + def create(self, name, key_id=3, with_contacts_ids=None, description=None): |
| 31 | + """ |
| 32 | + Create a contact list. |
| 33 | + https://dev.emarsys.com/v2/contact-lists/create-a-contact-list |
| 34 | + :param name: name of the list to create |
| 35 | + :param key_id: Key which identifies the contact. This can be a field |
| 36 | + id, id, uid or eid. If left empty, the email address (field ID 3) will |
| 37 | + be used by default. |
| 38 | + :param with_contacts_ids: list of key's value to add to the list. e.g. ['squirrel@squirrelmail.com',] |
| 39 | + :param description: Additional information about the contact list. |
| 40 | + :return: The API response payload. |
| 41 | +. |
| 42 | + Examples: |
| 43 | + If you want to create a list which name is test_list and assign squirrel@squirrelmail.com to this list: |
| 44 | + >>> client.lists.create("test_list", ['squirrel@squirrelmail.com',]) |
| 45 | + {'data': {'id': 123}, 'replyCode': 0, 'replyText': 'OK'} |
| 46 | + """ |
| 47 | + payload = { |
| 48 | + "key_id": key_id, |
| 49 | + "name": name, |
| 50 | + "description": description, |
| 51 | + "external_ids": with_contacts_ids, |
| 52 | + } |
| 53 | + |
| 54 | + return self.connection.make_call("POST", self.endpoint, payload=payload) |
| 55 | + |
| 56 | + def add_contacts(self, list_id, contacts_ids, key_id=3): |
| 57 | + """ |
| 58 | + Add multiple contacts to an existing list. |
| 59 | + https://dev.emarsys.com/v2/contact-lists/add-contacts-to-a-contact-list |
| 60 | +
|
| 61 | + :param list_id: Identifier of the list you want to add a contact to. |
| 62 | + :param contacts_ids: List of contact identifier you want to add to the list |
| 63 | + :param key_id: Key which identifies the contact. This can be a field |
| 64 | + id, id, uid or eid. If left empty, the email address (field ID 3) will |
| 65 | + be used by default. |
| 66 | + :return: The API response payload. |
| 67 | +
|
| 68 | + Examples: |
| 69 | + If you want to add two squirrel1@squirrelmail.com to the list test_list: |
| 70 | + >>> client.contact_list.add('test_list', ['squirrel1@squirrelmail.com',]) |
| 71 | + {'data': {'inserted_contacts': 123}, 'replyCode': 0, 'replyText': 'OK'} |
| 72 | + """ |
| 73 | + |
| 74 | + payload = {"key_id": key_id, "external_ids": contacts_ids} |
| 75 | + |
| 76 | + endpoint = "{}{}/add/".format(self.endpoint, list_id) |
| 77 | + return self.connection.make_call("POST", endpoint, payload=payload) |
0 commit comments