数组的Suds数组没有嵌套

时间:2012-04-06 22:31:36

标签: python soap suds

让我先说一下,我仍然是SOAP的新手,以及事情应该如何运作。我正在使用Vertical Response API。

我在为请求正确构建xml时遇到麻烦。这是一些代码:

from suds.client import Client

url = 'https://api.verticalresponse.com/wsdl/1.0/VRAPI.wsdl'
client = Client(url)
vr = client.service
...
test_list = (
    (
        {
            'name' : 'email_address',
            'value' : login['username'],
        },
        {
            'name' : 'First_Name',
            'value' : 'VR_User',
        }
    ),
    (
        {
            'name' : 'email_address',
            'value' : 'api-support@verticalresponse.com',
        },
        {
            'name' : 'First_Name',
            'value' : login['username'],
        },
    ),
)

# sid and cid are correctly retrieved prior to this point
print "Sending test message..."
vr.sendEmailCampaignTest({
    'session_id' : sid,
    'campaign_id' : cid,
    'recipients' : test_list,
})

在此上下文中,login ['username']只是一个电子邮件地址。

该代码引发了这个错误:

suds.WebFault: Server raised fault: 'Application failed during request deserialization: Too many elements in array. 4 instead of claimed 2 (2)

以下是sendEmailCampaignTest的定义: http://developers.verticalresponse.com/api/soap/methods/campaigns/sendemailcampaigntest/

这是记录输出的xml。 此XML不正确且不起作用(我删除了session_id和list_id以供显示):

<?xml version="1.0" encoding="utf-8" ?>
DEBUG:suds.client:headers = {'SOAPAction': u'"VR/API/1_0#sendEmailCampaignTest"', 'Content-Type': 'text/xml; charset=utf-8'}
ERROR:suds.client:<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns3="http://api.verticalresponse.com/1.0/VRAPI.xsd" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.w3.org/2001/XMLSchema" xmlns:ns2="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns4="VR/API/1_0" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Header/>
   <ns0:Body>
      <ns4:sendEmailCampaignTest>
         <args xsi:type="ns3:sendEmailCampaignTestArgs">
            <session_id xsi:type="ns1:string">redacted</session_id>
            <campaign_id xsi:type="ns1:int">redacted</campaign_id>
            <recipients xsi:type="ns3:ArrayOfNVDictionary" ns2:arrayType="ns3:NVDictionary[2]">
               <item>
                  <name xsi:type="ns1:string">email_address</name>
                  <value xsi:type="ns1:string">email@example.com</value>
               </item>
               <item>
                  <name xsi:type="ns1:string">First_Name</name>
                  <value xsi:type="ns1:string">VR_User</value>
               </item>
               <item>
                  <name xsi:type="ns1:string">email_address</name>
                  <value xsi:type="ns1:string">api-support@verticalresponse.com</value>
               </item>
               <item>
                  <name xsi:type="ns1:string">First_Name</name>
                  <value xsi:type="ns1:string">email@example.com</value>
               </item>
            </recipients>
         </args>
      </ns4:sendEmailCampaignTest>
   </ns0:Body>
</SOAP-ENV:Envelope>
DEBUG:suds.client:http failed:
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Client</faultcode><faultstring>Application failed during request deserialization: Too many elements in array. 4 instead of claimed 2 (2)
</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>

我根据以下ruby脚本移植我的脚本(由Vertical Response提供):

test_list = [
           [{
              'name' => 'email_address',
              'value' => $username
            },
            {
              'name' => 'First_Name',
              'value' => 'VR_User'
            }
           ],
           [{
              'name' => 'email_address',
              'value' => 'api-support@verticalresponse.com'
            },
            {
              'name' => 'First_Name',
              'value' => $username
            }
           ]
          ]
print "Sending test message...\n"
temp0 = vr.sendEmailCampaignTest({
                                 'session_id' => sid,
                                 'campaign_id' => cid,
                                 'recipients' => test_list,
                               })

它输出以下xml。 此XML是正确的并且可以正常工作(我删除了session_id和list_id):

<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <env:Body>
    <n1:sendEmailCampaignTest xmlns:n1="VR/API/1_0"
        env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <args xmlns:n2="http://api.verticalresponse.com/1.0/VRAPI.xsd"
          xsi:type="n2:sendEmailCampaignTestArgs">
        <session_id xsi:type="xsd:string">redacted</session_id>
        <campaign_id xsi:type="xsd:int">redacted</campaign_id>
        <recipients xmlns:n3="http://schemas.xmlsoap.org/soap/encoding/"
            xsi:type="n3:Array"
            n3:arrayType="n2:NVDictionary[2]">
          <item xsi:type="n3:Array"
              n3:arrayType="n2:NVPair[2]">
            <item>
              <name xsi:type="xsd:string">email_address</name>
              <value href="#id9496430"></value>
            </item>
            <item>
              <name xsi:type="xsd:string">First_Name</name>
              <value xsi:type="xsd:string">VR_User</value>
            </item>
          </item>
          <item xsi:type="n3:Array"
              n3:arrayType="n2:NVPair[2]">
            <item>
              <name xsi:type="xsd:string">email_address</name>
              <value xsi:type="xsd:string">api-support@verticalresponse.com</value>
            </item>
            <item>
              <name xsi:type="xsd:string">First_Name</name>
              <value href="#id9496430"></value>
            </item>
          </item>
        </recipients>
      </args>
    </n1:sendEmailCampaignTest>
    <value id="id9496430"
        xsi:type="xsd:string"
        env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">address@example.com</value>
  </env:Body>
</env:Envelope>

我知道错误在于收件人的构建。它应该包含两个项目,每个项目包含两个项目,但我使用suds的python脚本将其设置为包含四个未连接的项目。所以我的问题是如何才能正确构建xml?

编辑:我尝试了什么:

>>> url = 'https://api.verticalresponse.com/wsdl/1.0/VRAPI.wsdl'
>>> client = Client(url)
>>> args = client.factory.create('ns0:sendEmailCampaignTestArgs')  
>>> args
(sendEmailCampaignTestArgs){
   session_id = None
   campaign_id = None
   recipients = 
      (ArrayOfNVDictionary){
         _arrayType = ""
         _offset = ""
         _id = ""
         _href = ""
         _arrayType = ""
      }
 }

在我看来,suds没有正确构造ArrayOfNVDictionary(它没有一个可以容纳NVDictionary的实际数组元素),任何帮助都可以确定你会很棒。

1 个答案:

答案 0 :(得分:1)

SOAP的重点是远程方法调用和参数应该看起来像您选择的语言的本机方法和对象。

为了避免错误,请始终使用此(未经测试)代码段中显示的特定SOAP类型:

>>> recipient_list = [ 
...     ('root@eruditorum.org', 'Enoch'), 
...     ('billg@microsoft.com', 'Bill'), 
... ]
>>> client = suds.client.Client('https://api.verticalresponse.com/wsdl/1.0/VRAPI.wsdl')
>>> args = client.factory.create('ns0:sendEmailCampaignTestArgs')
>>> for email, first_name in recipient_list:
...     recipient = client.factory.create('ns0:NVDictionary')
...     recipient['email_address'] = email
...     recipient['First_Name'] = first_name
...     args.recipients.append(nvdict)
>>> args.session_id = sid
>>> args.campaign_id = cid
>>> client.service.sendEmailCampaignTest(args)

恕我直言,如上所述的明确代码更清晰,更易于维护。

当然你可以忽略我的建议,在这种情况下我猜test_list应该是这样的:

test_list = [ 
    { 
        'email_address': 'josh@cartme.com', 
        'First_Name': 'Josh' },
    },
    { 
        'email_address': 'josh.cartme@gmail.com', 
        'First_Name': 'Josh' },
    },
]