Created
August 22, 2012 23:27
-
-
Save AaronGhent/3430525 to your computer and use it in GitHub Desktop.
ExactTarget API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# >><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><< # | |
# ExactTarget API | |
# Author: Aaron Ghent | |
# Libraries Needed: suds | |
# CLI Install: ( pip install suds ) | |
# >><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><< # | |
from django.conf import settings | |
from suds.client import Client | |
from suds.wsse import * | |
import logging | |
WSDL = getattr(settings, "EXACT_TARGET_WSDL", 'https://webservice.exacttarget.com/etframework.wsdl') | |
USERNAME = getattr(settings, "EXACT_TARGET_USERNAME", '') | |
PASSWORD = getattr(settings, "EXACT_TARGET_PASSWORD", '') | |
DEBUG_SOAP = getattr(settings, "EXACT_TARGET_DEBUG_SOAP", False) | |
if settings.DEBUG and DEBUG_SOAP: | |
logging.basicConfig(level=logging.INFO) | |
logging.getLogger('suds.client').setLevel(logging.DEBUG) | |
logging.getLogger('suds.wsse').setLevel(logging.DEBUG) | |
exacttarget = Client(WSDL, cachingpolicy=1) | |
class ExactTargetError(Exception): | |
def __init__(self, errno, msg): | |
self.args = (errno, msg) | |
self.errno = errno | |
self.msg = msg | |
class SubscriberAlreadyExists(ExactTargetError): | |
pass | |
def get_subscriber(key, value): | |
data = None | |
try: | |
client = exacttarget | |
security = Security() | |
token = UsernameToken(USERNAME, PASSWORD) | |
security.tokens.append(token) | |
client.set_options(wsse=security) | |
rr = client.factory.create('RetrieveRequest') | |
rr.ObjectType = 'Subscriber' | |
rr.Properties = ['ID', 'EmailAddress', 'SubscriberKey'] | |
sfp = client.factory.create('SimpleFilterPart') | |
simpleOp = client.factory.create('SimpleOperators') | |
sfp.SimpleOperator = simpleOp.equals | |
sfp.Property = key | |
sfp.Value = value | |
rr.Filter = sfp | |
data = client.service.Retrieve(rr) | |
except Exception, e: | |
logging.error("Exception Thrown: " + e) | |
subscriber = None | |
if data and data.OverallStatus == "OK" and data.__contains__('Results'): | |
subscriber = {} | |
for x in data.Results[0].Attributes: | |
subscriber[x.Name] = x.Value | |
else: | |
logging.error("Couldn't Find Subscriber For " + key + ' - ' + value) | |
return subscriber | |
def add_subscriber(list_id, email, subkey, attrs={}): | |
data = None | |
try: | |
client = exacttarget | |
security = Security() | |
token = UsernameToken(USERNAME, PASSWORD) | |
security.tokens.append(token) | |
client.set_options(wsse=security) | |
subscriber = client.factory.create('Subscriber') | |
subscriber.PrimarySMSPublicationStatus = 'OptedIn' | |
subscriber.Status = 'Active' | |
subscriber.EmailTypePreference = 'HTML' | |
subscriber.EmailAddress = email | |
subscriber.SubscriberKey = subkey | |
subscriber_list = client.factory.create('SubscriberList') | |
subscriber_list.ID = list_id | |
subscriber_list.Status = 'Active' | |
subscriber.Lists = [subscriber_list, ] | |
subscriber.Attributes = [] | |
for name, value in attrs.items(): | |
attr = client.factory.create('Attribute') | |
attr.Name = name | |
attr.Value = value | |
subscriber['Attributes'].append(attr) | |
save_option = client.factory.create('SaveOption') | |
save_option.PropertyName = "*" | |
save_option.SaveAction = 'AddOnly' | |
co = client.factory.create('CreateOptions') | |
co.SaveOptions = [save_option, ] | |
co.RequestType = 'Synchronous' | |
co.QueuePriority = 'High' | |
apiObject = [client.factory.create('APIObject')] | |
apiObject = [subscriber, ] | |
data = client.service.Create(co, apiObject) | |
except Exception, e: | |
logging.error("Exception Thrown: " + e) | |
etid = None | |
if data and data.OverallStatus == "OK" and data.__contains__('Results'): | |
etid = data.Results[0].Object.ID | |
else: | |
if data.__contains__('Results'): | |
if data.Results[0].ErrorCode == 12014: | |
raise SubscriberAlreadyExists(data.Results[0].ErrorCode, data.Results[0].StatusMessage) | |
else: | |
raise ExactTargetError(data.Results[0].ErrorCode, data.Results[0].StatusMessage) | |
else: | |
logging.error("Error Adding Subscriber ???") | |
return etid | |
def update_subscriber(list_id, email, subkey, attrs={}): | |
data = None | |
try: | |
client = exacttarget | |
security = Security() | |
token = UsernameToken(USERNAME, PASSWORD) | |
security.tokens.append(token) | |
client.set_options(wsse=security) | |
subscriber = client.factory.create('Subscriber') | |
subscriber.PrimarySMSPublicationStatus = 'OptedIn' | |
subscriber.Status = 'Active' | |
subscriber.EmailTypePreference = 'HTML' | |
subscriber.SubscriberKey = subkey | |
subscriber.EmailAddress = email | |
subscriber_list = client.factory.create('SubscriberList') | |
subscriber_list.ID = list_id | |
subscriber_list.Status = 'Active' | |
subscriber.Lists = [subscriber_list, ] | |
subscriber.Attributes = [] | |
for name, value in attrs.items(): | |
attr = client.factory.create('Attribute') | |
attr.Name = name | |
attr.Value = value | |
subscriber['Attributes'].append(attr) | |
save_option = client.factory.create('SaveOption') | |
save_option.PropertyName = "*" | |
save_option.SaveAction = 'UpdateOnly' | |
co = client.factory.create('UpdateOptions') | |
co.SaveOptions = [save_option, ] | |
co.RequestType = 'Synchronous' | |
co.QueuePriority = 'High' | |
apiObject = [client.factory.create('APIObject')] | |
apiObject = [subscriber, ] | |
data = client.service.Update(co, apiObject) | |
except Exception, e: | |
logging.error("Exception Thrown: " + e) | |
etid = None | |
if data and data.OverallStatus == "OK" and data.__contains__('Results'): | |
etid = data.Results[0].Object.ID | |
else: | |
if data.__contains__('Results'): | |
if data.Results[0].ErrorCode == 12014: | |
raise SubscriberAlreadyExists(data.Results[0].ErrorCode, data.Results[0].StatusMessage) | |
else: | |
raise ExactTargetError(data.Results[0].ErrorCode, data.Results[0].StatusMessage) | |
else: | |
logging.error("Error Updating Subscriber ???") | |
return etid | |
# >><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><< # |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment