Skip to content

Commit

Permalink
Make the API a little more usable with client-side validation of Cons…
Browse files Browse the repository at this point in the history
…entToTrack.
  • Loading branch information
katie-cm committed May 21, 2018
1 parent c47b5a0 commit 1e6d9c6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/createsend/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json

from createsend.createsend import CreateSendBase, BadRequest
from createsend.utils import json_to_py
from createsend.utils import json_to_py, validate_consent_to_track


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

def add(self, list_id, email_address, name, custom_fields, resubscribe, consent_to_track, restart_subscription_based_autoresponders=False):
"""Adds a subscriber to a subscriber list."""
validate_consent_to_track(consent_to_track)
body = {
"EmailAddress": email_address,
"Name": name,
Expand All @@ -40,6 +41,7 @@ def add(self, list_id, email_address, name, custom_fields, resubscribe, consent_
def update(self, new_email_address, name, custom_fields, resubscribe, consent_to_track, restart_subscription_based_autoresponders=False):
"""Updates any aspect of a subscriber, including email address, name, and
custom field data if supplied."""
validate_consent_to_track(consent_to_track)
params = {"email": self.email_address}
body = {
"EmailAddress": new_email_address,
Expand Down
4 changes: 3 additions & 1 deletion lib/createsend/transactional.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json

from createsend.createsend import CreateSendBase
from createsend.utils import json_to_py
from createsend.utils import json_to_py, validate_consent_to_track


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

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):
"""Sends the smart email."""
validate_consent_to_track(consent_to_track)
body = {
"To": to,
"CC": cc,
Expand All @@ -45,6 +46,7 @@ def smart_email_send(self, smart_email_id, to, consent_to_track, cc=None, bcc=No

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):
"""Sends a classic email."""
validate_consent_to_track(consent_to_track)
body = {
"Subject": subject,
"From": from_address,
Expand Down
13 changes: 13 additions & 0 deletions lib/createsend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import json



VALID_CONSENT_TO_TRACK_VALUES = ("yes", "no", "unchanged")


class CertificateError(ValueError):
"""
Raised when an error occurs when attempting to verify an SSL certificate.
Expand Down Expand Up @@ -135,6 +139,15 @@ def dict_to_object(d):
return top


def validate_consent_to_track(user_input):
from createsend import ClientError
if hasattr(user_input, 'lower'):
user_input = user_input.lower()
if user_input in VALID_CONSENT_TO_TRACK_VALUES:
return
raise ClientError("Consent to track value must be one of %s" % (VALID_CONSENT_TO_TRACK_VALUES,))


def get_faker(expected_url, filename, status=None, body=None):

class Faker(object):
Expand Down
2 changes: 1 addition & 1 deletion test/test_subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_add_without_custom_fields(self):
self.subscriber.stub_request(
"subscribers/%s.json" % self.list_id, "add_subscriber.json")
email_address = self.subscriber.add(
self.list_id, "subscriber@example.com", "Subscriber", [], True, "Yes")
self.list_id, "subscriber@example.com", "Subscriber", [], True, "Unchanged")
self.assertEquals(email_address, "subscriber@example.com")

def test_add_with_custom_fields(self):
Expand Down

0 comments on commit 1e6d9c6

Please sign in to comment.