2017-02-22 31 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

相關問題