gpt4 book ai didi

python - Suds 数组的数组不嵌套

转载 作者:太空宇宙 更新时间:2023-11-03 19:20:04 26 4
gpt4 key购买 nike

首先我要说的是,我对 SOAP 以及事情应该如何工作还很陌生。我正在使用 Vertical Response API。

我无法让 suds 为请求正确构建 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 脚本将其设置为包含四个未嵌套的项目。所以我的问题是如何让 suds 正确构建 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 = ""
}
}

在我看来,ArrayOfNVDictionary 没有被 suds 正确构建(它没有一个实际的数组元素来保存 NVDictionary 的),任何帮助弄清楚你都会很棒。

最佳答案

SOAP 的全部要点是远程方法调用和参数应该看起来像您选择的语言的 native 方法和对象。

为了避免错误,请始终使用特定的 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' },
},
]

关于python - Suds 数组的数组不嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10049989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com