Skip to content

Commit d9fa2f8

Browse files
katie-cmkatharosada
authored andcommitted
Make the API a little more usable with client-side validation of ConsentToTrack.
1 parent d0a1d39 commit d9fa2f8

4 files changed

Lines changed: 20 additions & 3 deletions

File tree

lib/createsend/subscriber.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import json
44

55
from createsend.createsend import CreateSendBase, BadRequest
6-
from createsend.utils import json_to_py
6+
from createsend.utils import json_to_py, validate_consent_to_track
77

88

99
class Subscriber(CreateSendBase):
@@ -26,6 +26,7 @@ def get(self, list_id=None, email_address=None, include_tracking_preference=Fals
2626

2727
def add(self, list_id, email_address, name, custom_fields, resubscribe, consent_to_track, restart_subscription_based_autoresponders=False):
2828
"""Adds a subscriber to a subscriber list."""
29+
validate_consent_to_track(consent_to_track)
2930
body = {
3031
"EmailAddress": email_address,
3132
"Name": name,
@@ -40,6 +41,7 @@ def add(self, list_id, email_address, name, custom_fields, resubscribe, consent_
4041
def update(self, new_email_address, name, custom_fields, resubscribe, consent_to_track, restart_subscription_based_autoresponders=False):
4142
"""Updates any aspect of a subscriber, including email address, name, and
4243
custom field data if supplied."""
44+
validate_consent_to_track(consent_to_track)
4345
params = {"email": self.email_address}
4446
body = {
4547
"EmailAddress": new_email_address,

lib/createsend/transactional.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import json
44

55
from createsend.createsend import CreateSendBase
6-
from createsend.utils import json_to_py
6+
from createsend.utils import json_to_py, validate_consent_to_track
77

88

99
class Transactional(CreateSendBase):
@@ -30,6 +30,7 @@ def smart_email_details(self, smart_email_id):
3030

3131
def smart_email_send(self, smart_email_id, to, consent_to_track, cc=None, bcc=None, attachments=None, data=None, add_recipients_to_list=None):
3232
"""Sends the smart email."""
33+
validate_consent_to_track(consent_to_track)
3334
body = {
3435
"To": to,
3536
"CC": cc,
@@ -45,6 +46,7 @@ def smart_email_send(self, smart_email_id, to, consent_to_track, cc=None, bcc=No
4546

4647
def classic_email_send(self, subject, from_address, to, consent_to_track, client_id=None, cc=None, bcc=None, html=None, text=None, attachments=None, track_opens=True, track_clicks=True, inline_css=True, group=None, add_recipients_to_list=None):
4748
"""Sends a classic email."""
49+
validate_consent_to_track(consent_to_track)
4850
body = {
4951
"Subject": subject,
5052
"From": from_address,

lib/createsend/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import json
99

1010

11+
12+
VALID_CONSENT_TO_TRACK_VALUES = ("yes", "no", "unchanged")
13+
14+
1115
class CertificateError(ValueError):
1216
"""
1317
Raised when an error occurs when attempting to verify an SSL certificate.
@@ -135,6 +139,15 @@ def dict_to_object(d):
135139
return top
136140

137141

142+
def validate_consent_to_track(user_input):
143+
from createsend import ClientError
144+
if hasattr(user_input, 'lower'):
145+
user_input = user_input.lower()
146+
if user_input in VALID_CONSENT_TO_TRACK_VALUES:
147+
return
148+
raise ClientError("Consent to track value must be one of %s" % (VALID_CONSENT_TO_TRACK_VALUES,))
149+
150+
138151
def get_faker(expected_url, filename, status=None, body=None):
139152

140153
class Faker(object):

test/test_subscriber.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_add_without_custom_fields(self):
5757
self.subscriber.stub_request(
5858
"subscribers/%s.json" % self.list_id, "add_subscriber.json")
5959
email_address = self.subscriber.add(
60-
self.list_id, "subscriber@example.com", "Subscriber", [], True, "Yes")
60+
self.list_id, "subscriber@example.com", "Subscriber", [], True, "Unchanged")
6161
self.assertEquals(email_address, "subscriber@example.com")
6262

6363
def test_add_with_custom_fields(self):

0 commit comments

Comments
 (0)