跳转到主要内容

Silverpop企业新闻API

项目描述

Python实现Silverpop API

当前实现的API方法

def add_recipient(api_url, list_id, email, columns=[], optionals=ADD_RECIPIENT_OPTIONALS):
    """Add recipient to a list (only email key supported)
       api_url, list_id, email are required, optionally
       takes a list of dicts to define additional columns like
       [{'column_name':'State', 'column_value':'Germany'},]
       optionally takes a list of dicts to define optionals,
       defaults to
       [{'optional_name':'UPDATE_IF_FOUND', 'optional_value':'true'},
        {'optional_name':'SEND_AUTOREPLY', 'optional_value':'true'},
       ]
       returns True or False
    """

def update_recipient(api_url, list_id, old_email, columns=[], optionals=UPDATE_RECIPIENT_OPTIONALS):
    """Update recipient of a list,
       if the old_email is not a recipient of the list, a recipient will be added.
       api_url, list_id, old_email are required, optionally
       takes a list of dicts to define additional columns like:
       [{'column_name':'State', 'column_value':'Germany'},]
       Can change the email of a recipient by specifiying a column like:
       {'column_name':'EMAIL', 'column_value':'new@email.com'}
       Can re-opt-in an opted-out recipient by specifying a column like:
       {'column_name':'OPT_OUT', 'column_value':'False'}
       optionally takes a list of dicts to define optionals,
       defaults to
       [{'optional_name':'SEND_AUTOREPLY', 'optional_value':'true'},]
       returns True or False
    """

def opt_in_recipient(api_url, list_id, email, columns=[], optionals=OPT_IN_RECIPIENT_OPTIONALS):
    """opt in a recipient to a list (only email key supported)
       api_url, list_id, email are required, optionally
       takes a list of dicts to define additional columns like
       [{'column_name':'State', 'column_value':'Germany'},]
       returns True or False
       optionally takes a list of dicts to define optionals,
       defaults to
       [{'optional_name':'SEND_AUTOREPLY', 'optional_value':'true'},]
       returns True or False
    """

def is_opted_in(api_url, list_id, email):
    """Is the specified email opted in to the list?
       api_url, list_id, email are required
       returns True or False
    """

def opt_out_recipient(api_url, list_id, email):
    """opt out a recipient from a list
       api_url, list_id, email are required
       returns True or False
    """

def select_recipient_data(api_url, list_id, email, column=None):
    """get the recipients data
       api_url, list_id, email are required
       you may specify a column dict for non email key lists, like
       {'column_name': 'USER_ID', 'column_value': '4711'}
       returns the silverpop response (xml)
    """

def xml_request(api_url, xml):
    """submit a custom xml request
       api_url, xml, are required
       returns the silverpop response (xml)
    """

变更日志

0.6 (2010-02-25)

  • 更新_recipient、opt_in_recipient、add_recipient API方法现在在创建的XML请求中设置一个可选节点 <SEND_AUTOREPLY>true</SEND_AUTOREPLY>

  • 扩展update_recipient、opt_in_recipient、add_recipient API方法,添加可选参数optionals来控制可选节点。[hplocher]

0.5 (2009-05-18)

  • 将日志级别更改为DEBUG [hplocher]

0.4 (2009-05-13)

  • 实现了api方法

    update_recipient(api_url, list_id, old_email, columns=[]) opt_in_recipient(api_url, list_id, email, columns=[]) [hplocher]

0.3 (2009-05-12)

  • 实现了api方法

    is_opted_in(api_url, list_id, email) select_recipient_data(api_url, list_id, email, column=None) [hplocher]

0.2 (2009-05-12)

  • 添加了doctest [hplocher]

  • 重构 [hplocher]

  • 实现了xml_request(api_url, xml) [hplocher]

0.1 (2009-05-12)

  • 初始发布

  • 实现了api方法

    add_recipient(api_url, list_id, email, columns=[]) opt_out_recipient(api_url, list_id, email) [hplocher]

  • 模拟api方法

    is_opted_in(api_url, list_id, email) select_recipient_data(api_url, list_id, email, columns=[]) xml_request(api_url, xml) [hplocher]

  • 初始包骨架 [hplocher]

详细文档

测试设置

大多数API方法只会返回True或False,为了获得更详细的输出并防止向Silverpop发出请求,我们修改了urllib以打印(url, headers, data)而不是进行任何请求。

我们创建了一个Fake类,它将由urlib2.urlopen返回,该类将始终返回成功的silverpop响应

>>> class Fake(object):
...     def read(self): return "<success>true</success>"

在我们的测试方法中,我们打印请求的URL、头部和数据(为了测试,我们对URL编码的数据进行解码)并返回一个伪造的对象

>>> import cgi
>>> def test_urlopen(req):
...     print '-'*30 + 'request details' + '-'*30
...     print req.get_full_url()
...     print req.headers
...     xml = dict(cgi.parse_qsl(req.data))['xml']
...     print xml
...     print '-'*75
...     return Fake()
>>> import urllib2

最后,我们修复urllib2.urlopen

>>> urllib2.urlopen = test_urlopen

我们还定义了一个FakeRequest类来定义只包含表单的请求

>>> class FakeRequest(dict):
...   def __init__(self, **kwargs):
...     self.form = kwargs

API方法

所有API方法都可以通过导入模块访问

>>> import silverpop

首先,我们定义各种api_method所需的数据。

银pop服务器的URL

>>> api_url = 'http://api1.silverpop.com/XMLAPI '

一个列表ID

>>> list_id = 999

一个电子邮件地址(我们只支持电子邮件键列表,因此这是我们识别时事通讯订阅者的关键标识符)

>>> email = 'my@email.com'

add_recipient

让我们用所需的属性调用add_recipient API

>>> silverpop.add_recipient(api_url, list_id, email)
   ------------------------------request details------------------------------
   http://api1.silverpop.com/XMLAPI
   {'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
   <Envelope>
     <Body>
       <AddRecipient>
         <LIST_ID>999</LIST_ID>
         <CREATED_FROM>2</CREATED_FROM>
         <UPDATE_IF_FOUND>true</UPDATE_IF_FOUND>
         <SEND_AUTOREPLY>true</SEND_AUTOREPLY>
         <COLUMN>
           <NAME>EMAIL</NAME>
           <VALUE>my@email.com</VALUE>
         </COLUMN>
       </AddRecipient>
     </Body>
   </Envelope>
   ---------------------------------------------------------------------------
   True

如果我们提供一个列的列表,这些列将在请求中使用,从而导致银pop中的列。

例如,我们想使用自定义列性别

>>> columns = [{'column_name': 'gender', 'column_value': 'male'}, ]

>>> silverpop.add_recipient(api_url, list_id, email, columns)
    ------------------------------request details------------------------------
    http://api1.silverpop.com/XMLAPI
    {'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
    <Envelope>
      <Body>
        <AddRecipient>
          <LIST_ID>999</LIST_ID>
          <CREATED_FROM>2</CREATED_FROM>
          <UPDATE_IF_FOUND>true</UPDATE_IF_FOUND>
          <SEND_AUTOREPLY>true</SEND_AUTOREPLY>
          <COLUMN>
            <NAME>EMAIL</NAME>
            <VALUE>my@email.com</VALUE>
          </COLUMN>
          <COLUMN>
            <NAME>gender</NAME>
            <VALUE>male</VALUE>
          </COLUMN>
        </AddRecipient>
      </Body>
    </Envelope>
    ---------------------------------------------------------------------------
    True

请注意,在创建的请求中,我们有一些可选节点(UPDATE_IF_FOUND, SEND_AUTOREPLY)。这些是默认的。要更改这些,我们可以使用额外的参数optionals。

让我们使用额外的optionals属性调用add_recipient API来省略SEND_AUTOREPLY

>>> custom_optionals = [{'optional_name':'UPDATE_IF_FOUND', 'optional_value':'true'},]
>>> silverpop.add_recipient(api_url, list_id, email, optionals=custom_optionals)
   ------------------------------request details------------------------------
   http://api1.silverpop.com/XMLAPI
   {'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
   <Envelope>
     <Body>
       <AddRecipient>
         <LIST_ID>999</LIST_ID>
         <CREATED_FROM>2</CREATED_FROM>
         <UPDATE_IF_FOUND>true</UPDATE_IF_FOUND>
         <COLUMN>
           <NAME>EMAIL</NAME>
           <VALUE>my@email.com</VALUE>
         </COLUMN>
       </AddRecipient>
     </Body>
   </Envelope>
   ---------------------------------------------------------------------------
   True

SEND_AUTOREPLY节点已消失。

当然,你可以与列一起使用这个

>>> columns = [{'column_name': 'gender', 'column_value': 'male'}, ]

>>> silverpop.add_recipient(api_url, list_id, email, columns, optionals=custom_optionals)
    ------------------------------request details------------------------------
    http://api1.silverpop.com/XMLAPI
    {'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
    <Envelope>
      <Body>
        <AddRecipient>
          <LIST_ID>999</LIST_ID>
          <CREATED_FROM>2</CREATED_FROM>
          <UPDATE_IF_FOUND>true</UPDATE_IF_FOUND>
          <COLUMN>
            <NAME>EMAIL</NAME>
            <VALUE>my@email.com</VALUE>
          </COLUMN>
          <COLUMN>
            <NAME>gender</NAME>
            <VALUE>male</VALUE>
          </COLUMN>
        </AddRecipient>
      </Body>
    </Envelope>
    ---------------------------------------------------------------------------
    True

SEND_AUTOREPLY节点已消失。

update_recipient

update_recipientadd_recipient类似,但提供了更改收件人电子邮件地址的功能,并重新将退出的收件人加入。在我们的第一次测试中,我们不会这样做

>>> old_email = 'my@email.com'

让我们使用所需的属性调用update_recipient API

>>> silverpop.update_recipient(api_url, list_id, old_email)
   ------------------------------request details------------------------------
   http://api1.silverpop.com/XMLAPI
   {'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
   <Envelope>
     <Body>
       <UpdateRecipient>
         <LIST_ID>999</LIST_ID>
         <CREATED_FROM>2</CREATED_FROM>
         <OLD_EMAIL>my@email.com</OLD_EMAIL>
         <SEND_AUTOREPLY>true</SEND_AUTOREPLY>
       </UpdateRecipient>
     </Body>
   </Envelope>
   ---------------------------------------------------------------------------
   True

如果我们提供一个列的列表,这些列将在请求中使用,从而导致银pop中的列。这也可以用来更改收件人的电子邮件地址。

例如,我们想更改电子邮件地址,因此我们需要指定一个列EMAIL

>>> columns = [{'column_name': 'EMAIL', 'column_value': 'new@email.com'}, ]

>>> silverpop.update_recipient(api_url, list_id, old_email, columns)
    ------------------------------request details------------------------------
    http://api1.silverpop.com/XMLAPI
    {'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
    <Envelope>
      <Body>
        <UpdateRecipient>
          <LIST_ID>999</LIST_ID>
          <CREATED_FROM>2</CREATED_FROM>
          <OLD_EMAIL>my@email.com</OLD_EMAIL>
          <SEND_AUTOREPLY>true</SEND_AUTOREPLY>
          <COLUMN>
            <NAME>EMAIL</NAME>
            <VALUE>new@email.com</VALUE>
          </COLUMN>
        </UpdateRecipient>
      </Body>
    </Envelope>
    ---------------------------------------------------------------------------
    True

另一个常见的用例是将退出的收件人重新加入。

因此我们提供了一个列OPT_OUT,当设置为False时将重新加入收件人

>>> columns = [{'column_name': 'OPT_OUT', 'column_value': 'False'}, ]

>>> silverpop.update_recipient(api_url, list_id, old_email, columns)
    ------------------------------request details------------------------------
    http://api1.silverpop.com/XMLAPI
    {'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
    <Envelope>
      <Body>
        <UpdateRecipient>
          <LIST_ID>999</LIST_ID>
          <CREATED_FROM>2</CREATED_FROM>
          <OLD_EMAIL>my@email.com</OLD_EMAIL>
          <SEND_AUTOREPLY>true</SEND_AUTOREPLY>
          <COLUMN>
            <NAME>OPT_OUT</NAME>
            <VALUE>False</VALUE>
          </COLUMN>
        </UpdateRecipient>
      </Body>
    </Envelope>
    ---------------------------------------------------------------------------
    True

请注意,在创建的请求中有一个可选节点(SEND_AUTOREPLY)。这是默认的。要更改此,我们可以使用额外的参数optionals。

>>> old_email = 'my@email.com'

让我们使用额外的optionals属性调用update_recipient API来省略SEND_AUTOREPLY

>>> silverpop.update_recipient(api_url, list_id, old_email, optionals=[])
   ------------------------------request details------------------------------
   http://api1.silverpop.com/XMLAPI
   {'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
   <Envelope>
     <Body>
       <UpdateRecipient>
         <LIST_ID>999</LIST_ID>
         <CREATED_FROM>2</CREATED_FROM>
         <OLD_EMAIL>my@email.com</OLD_EMAIL>
       </UpdateRecipient>
     </Body>
   </Envelope>
   ---------------------------------------------------------------------------
   True

SEND_AUTOREPLY节点已消失。

当然,你可以与列一起使用这个

>>> columns = [{'column_name': 'EMAIL', 'column_value': 'new@email.com'}, ]

>>> silverpop.update_recipient(api_url, list_id, old_email, columns, optionals=[])
    ------------------------------request details------------------------------
    http://api1.silverpop.com/XMLAPI
    {'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
    <Envelope>
      <Body>
        <UpdateRecipient>
          <LIST_ID>999</LIST_ID>
          <CREATED_FROM>2</CREATED_FROM>
          <OLD_EMAIL>my@email.com</OLD_EMAIL>
          <COLUMN>
            <NAME>EMAIL</NAME>
            <VALUE>new@email.com</VALUE>
          </COLUMN>
        </UpdateRecipient>
      </Body>
    </Envelope>
    ---------------------------------------------------------------------------
    True

SEND_AUTOREPLY节点已消失。

opt_in_recipient

此方法是对update_recipient_data的包装,用于显式加入收件人。

让我们使用所需的属性调用opt_in_recipient API

>>> silverpop.opt_in_recipient(api_url, list_id, email)
------------------------------request details------------------------------
http://api1.silverpop.com/XMLAPI
{'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
<Envelope>
  <Body>
    <UpdateRecipient>
      <LIST_ID>999</LIST_ID>
      <CREATED_FROM>2</CREATED_FROM>
      <OLD_EMAIL>my@email.com</OLD_EMAIL>
      <SEND_AUTOREPLY>true</SEND_AUTOREPLY>
      <COLUMN>
        <NAME>OPT_OUT</NAME>
        <VALUE>False</VALUE>
      </COLUMN>
    </UpdateRecipient>
  </Body>
</Envelope>
---------------------------------------------------------------------------
True

如果我们提供一个列的列表,这些列将在请求中使用,从而导致银pop中的列。这也可以用来更改收件人的电子邮件地址。例如,我们想更改电子邮件地址。因此我们需要指定一个列EMAIL

>>> columns = [{'column_name': 'EMAIL', 'column_value': 'new@email.com'}, ]

>>> silverpop.opt_in_recipient(api_url, list_id, email, columns)
------------------------------request details------------------------------
http://api1.silverpop.com/XMLAPI
{'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
<Envelope>
  <Body>
    <UpdateRecipient>
      <LIST_ID>999</LIST_ID>
      <CREATED_FROM>2</CREATED_FROM>
      <OLD_EMAIL>my@email.com</OLD_EMAIL>
      <SEND_AUTOREPLY>true</SEND_AUTOREPLY>
      <COLUMN>
        <NAME>OPT_OUT</NAME>
        <VALUE>False</VALUE>
      </COLUMN>
      <COLUMN>
        <NAME>EMAIL</NAME>
        <VALUE>new@email.com</VALUE>
      </COLUMN>
    </UpdateRecipient>
  </Body>
</Envelope>
---------------------------------------------------------------------------
True

如果用户指定了一个OPT_OUT列,这将被忽略,因为我们总是想加入。

让我们定义要忽略的列

>>> columns = [{'column_name': 'OPT_OUT', 'column_value': 'some value'}, ]

请注意,OPT_OUT的值仍然是False

>>> silverpop.opt_in_recipient(api_url, list_id, email)
------------------------------request details------------------------------
http://api1.silverpop.com/XMLAPI
{'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
<Envelope>
  <Body>
    <UpdateRecipient>
      <LIST_ID>999</LIST_ID>
      <CREATED_FROM>2</CREATED_FROM>
      <OLD_EMAIL>my@email.com</OLD_EMAIL>
      <SEND_AUTOREPLY>true</SEND_AUTOREPLY>
      <COLUMN>
        <NAME>OPT_OUT</NAME>
        <VALUE>False</VALUE>
      </COLUMN>
    </UpdateRecipient>
  </Body>
</Envelope>
---------------------------------------------------------------------------
True

请注意,在创建的请求中有一个可选节点(SEND_AUTOREPLY)。这是默认的。要更改此,我们可以使用额外的参数optionals。

>>> old_email = 'my@email.com'

让我们使用额外的optionals属性调用opt_in_recipient API来省略SEND_AUTOREPLY

>>> silverpop.opt_in_recipient(api_url, list_id, email, optionals=[])
------------------------------request details------------------------------
http://api1.silverpop.com/XMLAPI
{'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
<Envelope>
  <Body>
    <UpdateRecipient>
      <LIST_ID>999</LIST_ID>
      <CREATED_FROM>2</CREATED_FROM>
      <OLD_EMAIL>my@email.com</OLD_EMAIL>
      <COLUMN>
        <NAME>OPT_OUT</NAME>
        <VALUE>False</VALUE>
      </COLUMN>
    </UpdateRecipient>
  </Body>
</Envelope>
---------------------------------------------------------------------------
True

SEND_AUTOREPLY节点已消失。

当然,你可以与列一起使用这个

>>> columns = [{'column_name': 'EMAIL', 'column_value': 'new@email.com'}, ]

>>> silverpop.opt_in_recipient(api_url, list_id, email, columns, optionals=[])
------------------------------request details------------------------------
http://api1.silverpop.com/XMLAPI
{'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
<Envelope>
  <Body>
    <UpdateRecipient>
      <LIST_ID>999</LIST_ID>
      <CREATED_FROM>2</CREATED_FROM>
      <OLD_EMAIL>my@email.com</OLD_EMAIL>
      <COLUMN>
        <NAME>OPT_OUT</NAME>
        <VALUE>False</VALUE>
      </COLUMN>
      <COLUMN>
        <NAME>EMAIL</NAME>
        <VALUE>new@email.com</VALUE>
      </COLUMN>
    </UpdateRecipient>
  </Body>
</Envelope>
---------------------------------------------------------------------------
True

SEND_AUTOREPLY节点已消失。

opt_out_recipient

让我们使用所需的属性调用opt_out_recipient API

>>> silverpop.opt_out_recipient(api_url, list_id, email)
------------------------------request details------------------------------
http://api1.silverpop.com/XMLAPI
{'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
<Envelope>
  <Body>
    <OptOutRecipient>
      <LIST_ID>999</LIST_ID>
      <EMAIL>my@email.com</EMAIL>
    </OptOutRecipient>
  </Body>
</Envelope>
---------------------------------------------------------------------------
True

is_opted_in

此方法在指定的电子邮件是列表的非退出收件人时返回True。如果列表不存在,或用户不是现有列表的收件人,或用户已退出指定的列表,则应返回False

对于这个测试,我们需要解释可能从银pop得到的各种响应,所以我们将更详细地更改,所以我们将改变我们的模拟不同银pop响应的伪造类。

首先,我们假设我们指定了一个不存在的list_id(is_opted_in应该返回False)

>>> class Fake(object):
...     def read(self): return """
...         <Envelope>
...           <Body>
...             <RESULT>
...                 <SUCCESS>false</SUCCESS>
...             </RESULT>
...             <Fault>
...               <Request/>
...               <FaultCode/>
...               <FaultString>
...                 <![CDATA[List with id 999 Does Not Exist.]]>
...               </FaultString>
...               <detail>
...                 <error>
...                   <errorid>108</errorid>
...                   <module/>
...                   <class>SP.ListManager</class>
...                   <method/>
...                 </error>
...                </detail>
...               </Fault>
...             </Body>
...         </Envelope>
...         """

>>> silverpop.is_opted_in(api_url, list_id, email)
------------------------------request details------------------------------
http://api1.silverpop.com/XMLAPI
{'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
<Envelope>
  <Body>
    <SelectRecipientData>
       <LIST_ID>999</LIST_ID>
       <EMAIL>my@email.com</EMAIL>
    </SelectRecipientData>
  </Body>
</Envelope>
---------------------------------------------------------------------------
False

现在,我们指定一个无效的list_id(它必须是一个整数)

>>> class Fake(object):
...     def read(self): return """
...         <Envelope>
...           <Body>
...             <RESULT>
...                 <SUCCESS>false</SUCCESS>
...             </RESULT>
...             <Fault>
...               <Request/>
...               <FaultCode/>
...               <FaultString>
...                 <![CDATA[List ID is not valid.]]>
...               </FaultString>
...               <detail>
...                 <error>
...                   <errorid>106</errorid>
...                   <module/>
...                   <class>SP.ListManager</class>
...                   <method/>
...                 </error>
...                </detail>
...               </Fault>
...             </Body>
...         </Envelope>
...         """

>>> silverpop.is_opted_in(api_url, 'NOT AN INTEGER', email)
------------------------------request details------------------------------
http://api1.silverpop.com/XMLAPI
{'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
<Envelope>
  <Body>
    <SelectRecipientData>
       <LIST_ID>NOT AN INTEGER</LIST_ID>
       <EMAIL>my@email.com</EMAIL>
    </SelectRecipientData>
  </Body>
</Envelope>
---------------------------------------------------------------------------
False

现在,我们假设提供的电子邮件不是列表成员

>>> class Fake(object):
...     def read(self): return """
...         <Envelope>
...           <Body>
...             <RESULT>
...                 <SUCCESS>false</SUCCESS>
...             </RESULT>
...             <Fault>
...               <Request/>
...               <FaultCode/>
...               <FaultString>
...                 <![CDATA[Recipient is not a member of the list.]]>
...               </FaultString>
...               <detail>
...                 <error>
...                   <errorid>128</errorid>
...                   <module/>
...                   <class>SP.ListManager</class>
...                   <method/>
...                 </error>
...                </detail>
...               </Fault>
...             </Body>
...         </Envelope>
...         """

>>> silverpop.is_opted_in(api_url, list_id, email)
------------------------------request details------------------------------
http://api1.silverpop.com/XMLAPI
{'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
<Envelope>
  <Body>
    <SelectRecipientData>
       <LIST_ID>999</LIST_ID>
       <EMAIL>my@email.com</EMAIL>
    </SelectRecipientData>
  </Body>
</Envelope>
---------------------------------------------------------------------------
False

现在,我们假设提供的电子邮件是列表成员,但该成员已选择退出(导致在 <OptedOut> 标签中有一个值)

>>> class Fake(object):
...     def read(self): return """
...         <Envelope>
...           <Body>
...             <RESULT>
...               <SUCCESS>TRUE</SUCCESS>
...               <EMAIL>my@email.com</EMAIL>
...               <Email>my@email.com</Email>
...               <RecipientId>5</RecipientId>
...               <EmailType>0</EmailType>
...               <LastModified>5/11/09 9:43 AM</LastModified>
...               <CreatedFrom>2</CreatedFrom>
...               <OptedIn>3/26/09 10:29 AM</OptedIn>
...               <OptedOut>5/11/09 9:43 AM</OptedOut>
...               <COLUMNS>
...                 <COLUMN>
...                   <NAME>State</NAME>
...                   <VALUE>Germany</VALUE>
...                 </COLUMN>
...               </COLUMNS>
...             </RESULT>
...           </Body>
...         </Envelope>
...         """

>>> silverpop.is_opted_in(api_url, list_id, email)
------------------------------request details------------------------------
http://api1.silverpop.com/XMLAPI
{'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
<Envelope>
  <Body>
    <SelectRecipientData>
       <LIST_ID>999</LIST_ID>
       <EMAIL>my@email.com</EMAIL>
    </SelectRecipientData>
  </Body>
</Envelope>
---------------------------------------------------------------------------
False

最后,我们假设提供的电子邮件是列表成员,并且该成员没有选择退出(导致 <OptedOut/> 标签为空)

>>> class Fake(object):
...     def read(self): return """
...         <Envelope>
...           <Body>
...             <RESULT>
...               <SUCCESS>TRUE</SUCCESS>
...               <EMAIL>my@email.com</EMAIL>
...               <Email>my@email.com</Email>
...               <RecipientId>5</RecipientId>
...               <EmailType>0</EmailType>
...               <LastModified>5/11/09 9:43 AM</LastModified>
...               <CreatedFrom>2</CreatedFrom>
...               <OptedIn>3/26/09 10:29 AM</OptedIn>
...               <OptedOut/>
...               <COLUMNS>
...                 <COLUMN>
...                   <NAME>State</NAME>
...                   <VALUE>Germany</VALUE>
...                 </COLUMN>
...               </COLUMNS>
...             </RESULT>
...           </Body>
...         </Envelope>
...         """

>>> silverpop.is_opted_in(api_url, list_id, email)
------------------------------request details------------------------------
http://api1.silverpop.com/XMLAPI
{'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
<Envelope>
  <Body>
    <SelectRecipientData>
       <LIST_ID>999</LIST_ID>
       <EMAIL>my@email.com</EMAIL>
    </SelectRecipientData>
  </Body>
</Envelope>
---------------------------------------------------------------------------
True

select_recipient_data

对于这次测试,我们希望从Silverpop那里得到一个简单的XML作为回复,因为我们不会进一步处理响应

>>> class Fake(object):
...     def read(self): return "<silverpop_response>true</silverpop_response>"


>>> silverpop.select_recipient_data(api_url, list_id, email)
------------------------------request details------------------------------
http://api1.silverpop.com/XMLAPI
{'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
<Envelope>
  <Body>
    <SelectRecipientData>
       <LIST_ID>999</LIST_ID>
       <EMAIL>my@email.com</EMAIL>
    </SelectRecipientData>
  </Body>
</Envelope>
---------------------------------------------------------------------------
'<silverpop_response>true</silverpop_response>'

如果我们提供了一个列,这将用于请求(仅用于非电子邮件密钥列表)。

例如,我们有一个自定义密钥 USER_ID

>>> column = {'column_name': 'USER_ID', 'column_value': '4711'}

>>> silverpop.select_recipient_data(api_url, list_id, email, column)
------------------------------request details------------------------------
http://api1.silverpop.com/XMLAPI
{'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
<Envelope>
  <Body>
    <SelectRecipientData>
       <LIST_ID>999</LIST_ID>
       <EMAIL>my@email.com</EMAIL>
         <COLUMN>
           <NAME>USER_ID</NAME>
           <VALUE>4711</VALUE>
         </COLUMN>
    </SelectRecipientData>
  </Body>
</Envelope>
---------------------------------------------------------------------------
'<silverpop_response>true</silverpop_response>'

xml_request

Silverpop XML API提供了相当大的命令集,我们只实现了其中的一部分。如果您需要执行不同的请求,可以使用此方法将自定义XML提交给Silverpop。结果,您将获得也是XML的Silverpop响应。

想象一下,我们想使用 ForwardToFrient XML命令。

让我们定义自定义的XML

>>> xml = """<Envelope>
...   <Body>
...     <ForwardToFriend>
...       <SENDER_EMAIL>bob@bob.com</SENDER_EMAIL>
...       <r>5</r>
...       <m>10</m>
...       <RECIPIENTS>jane@jane.com</RECIPIENTS>
...       <MESSAGE>Forwarded: Check this out, I just got that</MESSAGE>
...     </ForwardToFriend>
...   </Body>
...  </Envelope>"""

将XML发送到Silverpop,我们得到响应,以便进一步处理

>>> silverpop.xml_request(api_url, xml)
------------------------------request details------------------------------
http://api1.silverpop.com/XMLAPI
{'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}
<Envelope>
  <Body>
    <ForwardToFriend>
      <SENDER_EMAIL>bob@bob.com</SENDER_EMAIL>
      <r>5</r>
      <m>10</m>
      <RECIPIENTS>jane@jane.com</RECIPIENTS>
      <MESSAGE>Forwarded: Check this out, I just got that</MESSAGE>
    </ForwardToFriend>
  </Body>
 </Envelope>
---------------------------------------------------------------------------
'<silverpop_response>true</silverpop_response>'

贡献者

Hans-Peter Locher,作者

下载

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源分发

silverpop-0.6.zip (26.6 kB 查看哈希值)

上传时间

由以下机构支持