2017-02-22 117 views
3

我有供应商提供的web服务;一定操作的WSDL的样子:用zeep创建一个字符串数组参数?

<complexType name="ArrayOf_soapenc_string"> 
<complexContent> 
    <restriction base="soapenc:Array"> 
    <attribute ref="soapenc:arrayType" wsdl:arrayType="soapenc:string[]"/> 
    </restriction> 
</complexContent> 
</complexType> 
... 
<wsdl:message name="initExportDeviceRequest"> 
<wsdl:part name="filter" type="soapenc:string"/> 
<wsdl:part name="options" type="impl:ArrayOf_soapenc_string"/> 
</wsdl:message> 
... 
<wsdl:operation name="initExportDevice" parameterOrder="filter options"> 
<wsdl:input message="impl:initExportDeviceRequest" name="initExportDeviceRequest"/> 
<wsdl:output message="impl:initExportDeviceResponse" name="initExportDeviceResponse"/> 
</wsdl:operation> 

运行在WSDL python -mzeep ipam_export.wsdl产生这样的:

Global types: 
ns0:ArrayOf_soapenc_string(_value_1: string[], arrayType: xsd:string, offset: ns1:arrayCoordinate, id: xsd:ID, href: xsd:anyURI, _attr_1: {}) 
... 
Service: ExportsService 
Port: Exports (Soap11Binding: {http://diamondip.com/netcontrol/ws/}ExportsSoapBinding) 
    Operations: 
    ... 
    initExportDevice(filter: ns1:string, options: {_value_1: string[], arrayType: xsd:string, offset: ns1:arrayCoordinate, id: xsd:ID, href: xsd:anyURI, _attr_1: {}}) -> initExportDeviceReturn: ns2:WSContext 

我有在执行initExportDevice呼叫,具体而言,options参数困难。


How to use a complex type from a WSDL with zeep in Python建议,我认为这应该工作:

filter_type=client.get_type('ns1:string') 
filter=filter_type('addrType=4') 
options_type=client.get_type('ns0:ArrayOf_soapenc_string') 
options=options_type(['recurseContainerHierarchy']) 
client.service.initExportDevice(filter, options) 

但它抛出一个异常


Any element received object of type 'str', expected lxml.etree._Element or zeep.objects.string 
See http://docs.python-zeep.org/en/master/datastructures.html#any-objects for more information 
任何

options_type=client.get_type('ns0:ArrayOf_soapenc_string') 
options=options_type('recurseContainerHierarchy') 
client.service.initExportDevice(filter, options) 

factory = client.type_factory('ns0') 
options=factory.ArrayOf_soapenc_string(['recurseContainerHierarchy']) 
client.service.initExportDevice(filter=filter, options=options) 

factory = client.type_factory('ns0') 
options=factory.ArrayOf_soapenc_string('recurseContainerHierarchy') 
client.service.initExportDevice(filter=filter, options=options) 

factory = client.type_factory('ns0') 
options=factory.ArrayOf_soapenc_string(_value_1=['recurseContainerHierarchy']) 
client.service.initExportDevice(filter=filter, options=options) 

都提出同样的异常


options_type=client.get_type('ns0:ArrayOf_soapenc_string') 
options=xsd.AnyObject(options_type, ['recurseContainerHierarchy']) 
client.service.initExportDevice(filter, options) 

产量

argument of type 'AnyObject' is not iterable 

如何构建这个参数?

回答

1

好的,所以我在使用Zeep时也遇到了麻烦(容易使用泡沫),问题是Zeep返回数组作为函数(从我的测试中),所以您需要将函数分配给数组,然后修改它。从您当前的代码中,它看起来好像是直接将数据传递给函数(不会存储它)。

使用上面的示例,下面应该检索Array类型并允许您将其修改为有效的数据类型。

emptyArrayPlaceholder = client.get_type('ns0:ArrayOf_soapenc_string') 

ZEEP然后返回此类型的功能,所以首先你需要这个功能分配给一个变量,例如:如果你是那么

options = emptyArrayPlaceholder() 

检查选项你会看到它是一个字典,在你的列表里面。

print (options) 
{'soapenc': []} 

然后,您可以将项目添加到阵列轻松:

options['soapenc'].append('Foo') 

然后,您应该能够与

client.service.initExportDevice(filter, options) 

作为选项提交您的客户端现在是一个有效的ZEEP数据类型。

+0

你能帮我解决这个类似的问题吗?https://stackoverflow.com/questions/44586989/not-able-to-create-a-soap-filter-in-suds – Hussain