Skip to content

Commit 88376df

Browse files
author
Augustin
authored
Merge pull request #16 from transcovo/add-contact-list-endpoints
TECH implement and test contact list endpoints
2 parents b860c8e + fe32565 commit 88376df

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

pymarsys/contact_list.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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)

tests/test_contact_list.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import pytest
2+
import responses
3+
from urllib.parse import urljoin
4+
5+
from pymarsys.connections import SyncConnection
6+
from pymarsys.contact_list import ContactList
7+
8+
EMARSYS_URI = "https://api.emarsys.net/"
9+
CONTACT_LIST_ENDPOINT = "api/v2/contactlist/"
10+
TEST_USERNAME = "test_username"
11+
TEST_SECRET = "test_secret"
12+
13+
EMARSYS_CONTACT_LIST_CREATE_RESPONSE = {
14+
"replyCode": 0,
15+
"replyText": "OK",
16+
"data": {"id": 1},
17+
}
18+
19+
ADD_TO_CONTACT_LIST_ENDPOINT = "api/v2/contactlist/1/add/"
20+
21+
22+
EMARSYS_ADD_TO_CONTACT_LIST_RESPONSE = {
23+
"replyCode": 0,
24+
"replyText": "OK",
25+
"data": {"inserted_contacts": 1},
26+
}
27+
28+
class TestContactList:
29+
def test_init_no_exception(self):
30+
connection = SyncConnection(TEST_USERNAME, TEST_SECRET)
31+
ContactList(connection)
32+
33+
def test_init_exception(self):
34+
with pytest.raises(TypeError):
35+
ContactList(123)
36+
37+
@responses.activate
38+
def test_create(self):
39+
responses.add(
40+
responses.POST,
41+
urljoin(EMARSYS_URI, CONTACT_LIST_ENDPOINT),
42+
json=EMARSYS_CONTACT_LIST_CREATE_RESPONSE,
43+
status=200,
44+
content_type="application/json",
45+
)
46+
connection = SyncConnection(TEST_USERNAME, TEST_SECRET)
47+
contact_list = ContactList(connection)
48+
response = contact_list.create("test_list")
49+
assert response == EMARSYS_CONTACT_LIST_CREATE_RESPONSE
50+
51+
@responses.activate
52+
def test_add_contacts(self):
53+
responses.add(
54+
responses.POST,
55+
urljoin(EMARSYS_URI, ADD_TO_CONTACT_LIST_ENDPOINT),
56+
json=EMARSYS_ADD_TO_CONTACT_LIST_RESPONSE,
57+
status=200,
58+
content_type="application/json",
59+
)
60+
connection = SyncConnection(TEST_USERNAME, TEST_SECRET)
61+
contact_list = ContactList(connection)
62+
63+
response = contact_list.add_contacts(1, ["squirrel1@squirrelmail.com"])
64+
assert response == EMARSYS_ADD_TO_CONTACT_LIST_RESPONSE

0 commit comments

Comments
 (0)