How to use a complex type from a WSDL with zeep in Python

假装没事ソ 提交于 2019-12-13 12:04:48

问题


I have a WSDL that contains a complex type like so:

<xsd:complexType name="string_array">
  <xsd:complexContent>
    <xsd:restriction base="SOAP-ENC:Array">
      <xsd:attribute ref="SOAP-ENC:arrayType" wsdl:arrayType="xsd:string[]"/>
    </xsd:restriction>
  </xsd:complexContent>
</xsd:complexType>

I have decided to use zeep for the soap client and want to use that type as a parameter to one of the other methods referenced in the WSDL. I can't seem to figure out how to use this type though. When I looked through the documentation on how to use certain data structures referenced in the WSDL, it says to use the client.get_type() method, so I did the following:

wsdl = "https://wsdl.location.com/?wsdl"
client = Client(wsdl=wsdl)
string_array = client.get_type('tns:string_array')
string_array('some value')
client.service.method(string_array)

This give an error TypeError: argument of type 'string_array' is not iterable. I also tried many variations of that as well as trying to use a dictionary like so:

client.service.method(param_name=['some value']) 

Which gives the error

ValueError: Error while create XML for complexType '{https://wsdl.location.com/?wsdl}string_array': Expected instance of type <class 'zeep.objects.string_array'>, received <class 'str'> instead.`

If anyone knows how to use the above type from the WSDL with zeep, I would be grateful. Thanks.


回答1:


The client.get_type() method returns a 'type constructor' that you can later use to construct the value. You need to assign the constructed value to a separate variable and use that variable in the method invocation:

wsdl = "https://wsdl.location.com/?wsdl"
client = Client(wsdl=wsdl)
string_array_type = client.get_type('tns:string_array')
string_array = string_array_type(['some value'])
client.service.method(string_array)


来源:https://stackoverflow.com/questions/39606566/how-to-use-a-complex-type-from-a-wsdl-with-zeep-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!