def handle_response(self, status, data): if status == 400: raise BadRequest(json_to_py(data)) elif status == 401: raise Unauthorized(json_to_py(data)) elif status == 404: raise NotFound() elif status in range(400, 500): raise ClientError() elif status in range(500, 600): raise ServerError() return data
def handle_response(self, status, data): if status == 400: raise BadRequest(json_to_py(data)) elif status == 401: json_data = json_to_py(data) if json_data.Code == 121: raise ExpiredOAuthToken(json_data) raise Unauthorized(json_data) elif status == 404: raise NotFound() elif status in range(400, 500): raise ClientError() elif status in range(500, 600): raise ServerError() return data
def add(self, email_address, name): """Adds an administrator to an account.""" body = { "EmailAddress": email_address, "Name": name} response = self._post("/admins.json", json.dumps(body)) return json_to_py(response)
def external_session_url(self, email, chrome, url, integrator_id, client_id): """ Get a URL which initiates a new external session for the user with the given email. Full details: http://www.campaignmonitor.com/api/account/#single_sign_on :param email: String The representing the email address of the Campaign Monitor user for whom the login session should be created. :param chrome: String representing which 'chrome' to display - Must be either "all", "tabs", or "none". :param url: String representing the URL to display once logged in. e.g. "/subscribers/" :param integrator_id: String representing the Integrator ID. You need to contact Campaign Monitor support to get an Integrator ID. :param client_id: String representing the Client ID of the client which should be active once logged in to the Campaign Monitor account. :returns Object containing a single field SessionUrl which represents the URL to initiate the external Campaign Monitor session. """ body = { "Email": email, "Chrome": chrome, "Url": url, "IntegratorID": integrator_id, "ClientID": client_id } response = self._put('/externalsession.json', json.dumps(body)) return json_to_py(response)
def create(self, client_id, subject, name, from_name, from_email, reply_to, html_url, text_url, list_ids, segment_ids): """Creates a new campaign for a client. :param client_id: String representing the ID of the client for whom the campaign will be created. :param subject: String representing the subject of the campaign. :param name: String representing the name of the campaign. :param from_name: String representing the from name for the campaign. :param from_email: String representing the from address for the campaign. :param reply_to: String representing the reply-to address for the campaign. :param html_url: String representing the URL for the campaign HTML content. :param text_url: String representing the URL for the campaign text content. Note that text_url is optional and if None or an empty string, text content will be automatically generated from the HTML content. :param list_ids: Array of Strings representing the IDs of the lists to which the campaign will be sent. :param segment_ids: Array of Strings representing the IDs of the segments to which the campaign will be sent. :returns String representing the ID of the newly created campaign. """ body = { "Subject": subject, "Name": name, "FromName": from_name, "FromEmail": from_email, "ReplyTo": reply_to, "HtmlUrl": html_url, "TextUrl": text_url, "ListIDs": list_ids, "SegmentIDs": segment_ids } response = self._post("/campaigns/%s.json" % client_id, json.dumps(body)) self.campaign_id = json_to_py(response) return self.campaign_id
def create(self, client_id, subject, name, from_name, from_email, reply_to, html_url, text_url, list_ids, segment_ids): """Creates a new campaign for a client. :param client_id: String representing the ID of the client for whom the campaign will be created. :param subject: String representing the subject of the campaign. :param name: String representing the name of the campaign. :param from_name: String representing the from name for the campaign. :param from_email: String representing the from address for the campaign. :param reply_to: String representing the reply-to address for the campaign. :param html_url: String representing the URL for the campaign HTML content. :param text_url: String representing the URL for the campaign text content. Note that text_url is optional and if None or an empty string, text content will be automatically generated from the HTML content. :param list_ids: Array of Strings representing the IDs of the lists to which the campaign will be sent. :param segment_ids: Array of Strings representing the IDs of the segments to which the campaign will be sent. """ body = { "Subject": subject, "Name": name, "FromName": from_name, "FromEmail": from_email, "ReplyTo": reply_to, "HtmlUrl": html_url, "TextUrl": text_url, "ListIDs": list_ids, "SegmentIDs": segment_ids } response = self._post("/campaigns/%s.json" % client_id, json.dumps(body)) return json_to_py(response)
def create(self, list_id, title, rules): """Creates a new segment.""" body = { "Title": title, "Rules": rules } response = self._post("/segments/%s.json" % list_id, json.dumps(body)) return json_to_py(response)
def create(self, client_id, name, html_url, zip_url): """Creates a new email template.""" body = {"Name": name, "HtmlPageURL": html_url, "ZipFileURL": zip_url} response = self._post("/templates/%s.json" % client_id, json.dumps(body)) self.template_id = json_to_py(response) return self.template_id
def create_webhook(self, events, url, payload_format): """Creates a new webhook for the specified events (an array of strings). Valid events are "Subscribe", "Deactivate", and "Update". Valid payload formats are "json", and "xml".""" body = {"Events": events, "Url": url, "PayloadFormat": payload_format} response = self._post(self.uri_for("webhooks"), json.dumps(body)) return json_to_py(response)
def get(self, list_id=None, email_address=None): """Gets a subscriber by list ID and email address.""" params = {"email": email_address or self.email_address} response = self._get("/subscribers/%s.json" % (list_id or self.list_id), params=params) return json_to_py(response)
def get(self, client_id=None, email_address=None): """Gets a person by client ID and email address.""" params = {"email": email_address or self.email_address} response = self._get("/clients/%s/people.json" % (client_id or self.client_id), params=params) return json_to_py(response)
def create_from_template(self, client_id, subject, name, from_name, from_email, reply_to, list_ids, segment_ids, template_id, template_content): """Creates a new campaign for a client, from a template. :param client_id: String representing the ID of the client for whom the campaign will be created. :param subject: String representing the subject of the campaign. :param name: String representing the name of the campaign. :param from_name: String representing the from name for the campaign. :param from_email: String representing the from address for the campaign. :param reply_to: String representing the reply-to address for the campaign. :param list_ids: Array of Strings representing the IDs of the lists to which the campaign will be sent. :param segment_ids: Array of Strings representing the IDs of the segments to which the campaign will be sent. :param template_id: String representing the ID of the template on which the campaign will be based. :param template_content: Hash representing the content to be used for the editable areas of the template. See documentation at campaignmonitor.com/api/campaigns/#creating_a_campaign_from_template for full details of template content format. """ body = { "Subject": subject, "Name": name, "FromName": from_name, "FromEmail": from_email, "ReplyTo": reply_to, "ListIDs": list_ids, "SegmentIDs": segment_ids, "TemplateID": template_id, "TemplateContent": template_content } response = self._post("/campaigns/%s/fromtemplate.json" % client_id, json.dumps(body)) return json_to_py(response)
class Subscriber(CreateSendBase): """Represents a subscriber and associated functionality.""" def __init__(self, auth=None, list_id=None, email_address=None): self.list_id = list_id self.email_address = email_address super(Subscriber, self).__init__(auth) def get(self, list_id, email_address): """Gets a subscriber by list ID and email address.""" params = { "email": email_address } response = self._get("/subscribers/%s.json" % list_id, params=params) return json_to_py(response) def add(self, list_id, email_address, name, custom_fields, resubscribe, restart_subscription_based_autoresponders=False): """Adds a subscriber to a subscriber list.""" body = { "EmailAddress": email_address, "Name": name, "CustomFields": custom_fields, "Resubscribe": resubscribe, "RestartSubscriptionBasedAutoresponders": restart_subscription_based_autoresponders } response = self._post("/subscribers/%s.json" % list_id, json.dumps(body)) return json_to_py(response) def update(self, new_email_address, name, custom_fields, resubscribe, restart_subscription_based_autoresponders=False): """Updates any aspect of a subscriber, including email address, name, and custom field data if supplied.""" params = { "email": self.email_address } body = { "EmailAddress": new_email_address, "Name": name, "CustomFields": custom_fields, "Resubscribe": resubscribe, "RestartSubscriptionBasedAutoresponders": restart_subscription_based_autoresponders } response = self._put("/subscribers/%s.json" % self.list_id, body=json.dumps(body), params=params) # Update self.email_address, so this object can continue to be used reliably self.email_address = new_email_address def import_subscribers(self, list_id, subscribers, resubscribe, queue_subscription_based_autoresponders=False, restart_subscription_based_autoresponders=False): """Imports subscribers into a subscriber list.""" body = { "Subscribers": subscribers, "Resubscribe": resubscribe, "QueueSubscriptionBasedAutoresponders": queue_subscription_based_autoresponders, "RestartSubscriptionBasedAutoresponders": restart_subscription_based_autoresponders } try: response = self._post("/subscribers/%s/import.json" % list_id, json.dumps(body)) except BadRequest, br: # Subscriber import will throw BadRequest if some subscribers are not imported # successfully. If this occurs, we want to return the ResultData property of # the BadRequest exception (which is of the same "form" as the response we'd # receive upon a completely successful import) if hasattr(br.data, 'ResultData'): return br.data.ResultData else: raise br return json_to_py(response)
def create(self, client_id, name, html_url, zip_url): """Creates a new email template.""" body = { "Name": name, "HtmlPageURL": html_url, "ZipFileURL": zip_url } response = self._post("/templates/%s.json" % client_id, json.dumps(body)) return json_to_py(response)
def create_custom_field(self, field_name, data_type, options=[]): """Creates a new custom field for this list.""" body = { "FieldName": field_name, "DataType": data_type, "Options": options } response = self._post(self.uri_for("customfields"), json.dumps(body)) return json_to_py(response)
def classic_email_groups(self, client_id=None): """Gets the list of classic email groups.""" if client_id is None: response = self._get("/transactional/classicEmail/groups") else: response = self._get( "/transactional/classicEmail/groups?clientID=%s" % client_id) return json_to_py(response)
def create(self, list_id, title, rulegroups): """Creates a new segment.""" body = { "Title": title, "RuleGroups": rulegroups } response = self._post("/segments/%s.json" % list_id, json.dumps(body)) self.segment_id = json_to_py(response) return self.segment_id
def create(self, client_id, title, unsubscribe_page, confirmed_opt_in, confirmation_success_page): """Creates a new list for a client.""" body = { "Title": title, "UnsubscribePage": unsubscribe_page, "ConfirmedOptIn": confirmed_opt_in, "ConfirmationSuccessPage": confirmation_success_page } response = self._post("/lists/%s.json" % client_id, json.dumps(body)) return json_to_py(response)
def update_custom_field(self, custom_field_key, field_name, visible_in_preference_center): """Updates a custom field belonging to this list.""" custom_field_key = urllib.quote(custom_field_key, '') body = { "FieldName": field_name, "VisibleInPreferenceCenter": visible_in_preference_center } response = self._put(self.uri_for("customfields/%s" % custom_field_key), json.dumps(body)) return json_to_py(response)
def add(self, list_id, email_address, name, custom_fields, resubscribe): """Adds a subscriber to a subscriber list.""" body = { "EmailAddress": email_address, "Name": name, "CustomFields": custom_fields, "Resubscribe": resubscribe } response = self._post("/subscribers/%s.json" % list_id, json.dumps(body)) return json_to_py(response)
def suppressionlist(self, page=1, page_size=1000, order_field="email", order_direction="asc"): """Gets this client's suppression list.""" params = { "page": page, "pagesize": page_size, "orderfield": order_field, "orderdirection": order_direction } response = self._get(self.uri_for("suppressionlist"), params=params) return json_to_py(response)
def add(self, client_id, email_address, name, access_level, password): """Adds a person to a client. Password is optional and if not supplied, an invitation will be emailed to the person""" body = { "EmailAddress": email_address, "Name": name, "AccessLevel": access_level, "Password": password} response = self._post("/clients/%s/people.json" % client_id, json.dumps(body)) return json_to_py(response)
def recipients(self, page=1, page_size=1000, order_field="email", order_direction="asc"): """Retrieves the recipients of this campaign.""" params = { "page": page, "pagesize": page_size, "orderfield": order_field, "orderdirection": order_direction } response = self._get(self.uri_for("recipients"), params=params) return json_to_py(response)
def bounces(self, page=1, page_size=1000, order_field="date", order_direction="asc"): """Retrieves the bounces for this campaign.""" params = { "page": page, "pagesize": page_size, "orderfield": order_field, "orderdirection": order_direction } response = self._get(self.uri_for("bounces"), params=params) return json_to_py(response)
def import_subscribers(self, list_id, subscribers, resubscribe): """Imports subscribers into a subscriber list.""" body = { "Subscribers": subscribers, "Resubscribe": resubscribe } try: response = self._post("/subscribers/%s/import.json" % list_id, json.dumps(body)) except BadRequest: pass return json_to_py(response)
def create(self, company, timezone, country): """Creates a client.""" body = { "CompanyName": company, "TimeZone": timezone, "Country": country } response = self._post("/clients.json", json.dumps(body)) self.client_id = json_to_py(response) return self.client_id
def create_custom_field(self, field_name, data_type, options=[], visible_in_preference_center=True): """Creates a new custom field for this list.""" body = { "FieldName": field_name, "DataType": data_type, "Options": options, "VisibleInPreferenceCenter": visible_in_preference_center } response = self._post(self.uri_for("customfields"), json.dumps(body)) return json_to_py(response)
def create_webhook(self, events, url, payload_format): """Creates a new webhook for the specified events (an array of strings). Valid events are "Subscribe", "Deactivate", and "Update". Valid payload formats are "json", and "xml".""" body = { "Events": events, "Url": url, "PayloadFormat": payload_format } response = self._post(self.uri_for("webhooks"), json.dumps(body)) return json_to_py(response)
def subscribers(self, date="", page=1, page_size=1000, order_field="email", order_direction="asc"): """Gets the active subscribers in this segment.""" params = { "date": date, "page": page, "pagesize": page_size, "orderfield": order_field, "orderdirection": order_direction } response = self._get(self.uri_for("active"), params=params) return json_to_py(response)
def clicks(self, date, page=1, page_size=1000, order_field="date", order_direction="asc"): """Retrieves the subscriber clicks for this campaign.""" params = { "date": date, "page": page, "pagesize": page_size, "orderfield": order_field, "orderdirection": order_direction } response = self._get(self.uri_for("clicks"), params=params) return json_to_py(response)
def spam(self, date="", page=1, page_size=1000, order_field="date", order_direction="asc"): """Retrieves the spam complaints for this campaign.""" params = { "date": date, "page": page, "pagesize": page_size, "orderfield": order_field, "orderdirection": order_direction } response = self._get(self.uri_for("spam"), params=params) return json_to_py(response)
def create(self, company, timezone, country): """Creates a client.""" body = { "CompanyName": company, "TimeZone": timezone, "Country": country } response = self._post("/clients.json", json.dumps(body)) return json_to_py(response)
def create(self, company, contact_name, email, timezone, country): """Creates a client.""" body = { "CompanyName": company, "ContactName": contact_name, "EmailAddress": email, "TimeZone": timezone, "Country": country } response = self._post("/clients.json", json.dumps(body)) return json_to_py(response)
def unsubscribed(self, date, page=1, page_size=1000, order_field="email", order_direction="asc"): """Gets the unsubscribed subscribers for this list.""" params = { "date": date, "page": page, "pagesize": page_size, "orderfield": order_field, "orderdirection": order_direction } response = self._get(self.uri_for("unsubscribed"), params=params) return json_to_py(response)
def smart_email_list(self, status="all", client_id=None): """Gets the smart email list.""" if client_id is None: response = self._get("/transactional/smartEmail?status=%s" % status) else: response = self._get( "/transactional/smartEmail?status=%s&clientID=%s" % (status, client_id)) return json_to_py(response)
def subscribers(self, date, page=1, page_size=1000, order_field="email", order_direction="asc"): """Gets the active subscribers in this segment.""" params = { "date": date, "page": page, "pagesize": page_size, "orderfield": order_field, "orderdirection": order_direction } response = self._get(self.uri_for("active"), params=params) return json_to_py(response)
def add(self, list_id, email_address, name, custom_fields, resubscribe, restart_subscription_based_autoresponders=False): """Adds a subscriber to a subscriber list.""" body = { "EmailAddress": email_address, "Name": name, "CustomFields": custom_fields, "Resubscribe": resubscribe, "RestartSubscriptionBasedAutoresponders": restart_subscription_based_autoresponders } response = self._post("/subscribers/%s.json" % list_id, json.dumps(body)) return json_to_py(response)
def add(self, client_id, email_address, name, access_level, password): """Adds a person to a client. Password is optional and if not supplied, an invitation will be emailed to the person""" body = { "EmailAddress": email_address, "Name": name, "AccessLevel": access_level, "Password": password } response = self._post("/clients/%s/people.json" % client_id, json.dumps(body)) return json_to_py(response)
def update_custom_field(self, custom_field_key, field_name, visible_in_preference_center): """Updates a custom field belonging to this list.""" custom_field_key = urllib.quote(custom_field_key, '') body = { "FieldName": field_name, "VisibleInPreferenceCenter": visible_in_preference_center } response = self._put( self.uri_for("customfields/%s" % custom_field_key), json.dumps(body)) return json_to_py(response)
def create(self, company, contact_name, email, timezone, country): """Creates a client.""" if not (contact_name is None or contact_name == ""): warnings.warn("[DEPRECATION] create used in this way has been deprecated. Instead, set contact_name on persons in this client using person.add or person.update") if not (email is None or email == ""): warnings.warn("[DEPRECATION] create used in this way has been deprecated. Instead, set email on persons in this client using person.add or person.update") body = { "CompanyName": company, "ContactName": contact_name, "EmailAddress": email, "TimeZone": timezone, "Country": country } response = self._post("/clients.json", json.dumps(body)) return json_to_py(response)
def create(self, client_id, subject, name, from_name, from_email, reply_to, html_url, text_url, list_ids, segment_ids): """Creates a new campaign for a client.""" body = { "Subject": subject, "Name": name, "FromName": from_name, "FromEmail": from_email, "ReplyTo": reply_to, "HtmlUrl": html_url, "TextUrl": text_url, "ListIDs": list_ids, "SegmentIDs": segment_ids } response = self._post("/campaigns/%s.json" % client_id, json.dumps(body)) return json_to_py(response)
def bounced(self, date, page=1, page_size=1000, order_field="email", order_direction="asc"): """Gets the bounced subscribers for this list.""" params = { "date": date, "page": page, "pagesize": page_size, "orderfield": order_field, "orderdirection": order_direction } response = self._get(self.uri_for("bounced"), params=params) return json_to_py(response)
def import_subscribers(self, list_id, subscribers, resubscribe): """Imports subscribers into a subscriber list.""" body = {"Subscribers": subscribers, "Resubscribe": resubscribe} try: response = self._post("/subscribers/%s/import.json" % list_id, json.dumps(body)) except BadRequest as br: # Subscriber import will throw BadRequest if some subscribers are not imported # successfully. If this occurs, we want to return the ResultData property of # the BadRequest exception (which is of the same "form" as the response we'd # receive upon a completely successful import) if hasattr(br.data, 'ResultData'): return br.data.ResultData else: raise br return json_to_py(response)
def import_subscribers(self, list_id, subscribers, resubscribe): """Imports subscribers into a subscriber list.""" body = { "Subscribers": subscribers, "Resubscribe": resubscribe } try: response = self._post("/subscribers/%s/import.json" % list_id, json.dumps(body)) except BadRequest as br: # Subscriber import will throw BadRequest if some subscribers are not imported # successfully. If this occurs, we want to return the ResultData property of # the BadRequest exception (which is of the same "form" as the response we'd # receive upon a completely successful import) if hasattr(br.data, 'ResultData'): return br.data.ResultData else: raise br return json_to_py(response)
def exchange_token(self, client_id, client_secret, redirect_uri, code): """Exchange a provided OAuth code for an OAuth access token, 'expires in' value and refresh token.""" params = [ ('grant_type', 'authorization_code'), ('client_id', client_id), ('client_secret', client_secret), ('redirect_uri', redirect_uri), ('code', code), ] response = self._post('', urllib.urlencode(params), CreateSend.oauth_token_uri, "application/x-www-form-urlencoded") access_token, expires_in, refresh_token = None, None, None r = json_to_py(response) if hasattr(r, 'error') and hasattr(r, 'error_description'): err = "Error exchanging code for access token: " err += "%s - %s" % (r.error, r.error_description) raise Exception(err) access_token, expires_in, refresh_token = r.access_token, r.expires_in, r.refresh_token return [access_token, expires_in, refresh_token]