用zeep创建一个字符串数组参数?

时间:2017-02-22 15:24:31

标签: python web-services wsdl zeep

我有供应商提供的网络服务;某个操作的WSDL如下所示:

<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 个答案:

答案 0 :(得分:4)

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

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

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

然后Zeep将此类型作为函数返回,因此首先需要将此函数分配给变量,例如:

options = emptyArrayPlaceholder()

如果您要检查选项,您会看到它是一个字典,其中包含您的列表。

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

然后,您可以使用以下内容轻松地向项目添加项目:

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

然后您应该能够使用

提交您的客户
client.service.initExportDevice(filter, options)

因为选项现在是有效的Zeep数据类型。