使用zeep创建字符串数组参数? [英] Create a string array parameter with zeep?
问题描述
我有供应商提供的Web服务;特定操作的WSDL如下:
I have a vendor-supplied webservice; the WSDL for a certain operation looks like:
<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
会产生以下结果:
Running python -mzeep ipam_export.wsdl
on the WSDL yields this:
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
参数)时遇到困难.
I am having difficulty in executing the initExportDevice call, specifically, the options
parameter.
如何使用WSDL中带有zeep的WSDL中的一种复杂类型对我建议应该起作用:
How to use a complex type from a WSDL with zeep in Python suggests to me that this should work:
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
任何
Any of
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)
所有人都提出相同的例外情况
all raise the same exception
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
如何构造此参数?
How do I construct this parameter?
推荐答案
好,所以我在使用Zeep时也遇到了麻烦(很容易使用suds),问题是Zeep将数组作为函数返回(根据我的测试),所以您需要将函数分配给数组,然后对其进行修改.从您当前的代码来看,好像您是将数据直接传递给该函数(该函数不会存储数据).
Ok so I had trouble with this using Zeep as well (works with suds easily), the problem is Zeep returns arrays as a function (from my testing) so you need to assign the function to an array, and then modify it. From your current code, it looks as if you are passing data directly to the function (which won't store it).
使用上面的示例,下面的示例应检索Array类型,并允许您将其修改为有效的数据类型.
Using your example above, the below should retrieve the Array type and allow you to modify it as a valid data type.
emptyArrayPlaceholder = client.get_type('ns0:ArrayOf_soapenc_string')
Zeep然后将此类型作为函数返回,因此首先需要将此函数分配给变量,例如:
Zeep then returns this type as a function, so first off you need to assign this function to a variable, such as:
options = emptyArrayPlaceholder()
如果您随后要检查选项,您会发现它是一个字典,其中包含列表.
If you were then to inspect options you will see it is a dict, with your list inside of it.
print (options)
{'soapenc': []}
然后,您可以使用以下方法轻松地将项目添加到数组中:
You can then add items to the array easily with:
options['soapenc'].append('Foo')
然后,您应该可以使用以下方式提交客户
You should then be able to submit your client with
client.service.initExportDevice(filter, options)
由于选项现在是有效的Zeep数据类型.
As options is now a valid Zeep data type.
这篇关于使用zeep创建字符串数组参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!