使用Zeep覆盖WSDL文件中的类型

问题描述 投票:0回答:1

ServiceNow SOAP API的WSDL文件在实际上是字典时将request_payload定义为字符串。结果,我每次查询服务时,都会收到错误消息:

"ValueError: The String type doesn't accept collections as value".

WSDL文件的部分:

<wsdl:definitions targetNamespace="http://www.service-now.com/ChangeOperation" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://www.service-now.com/ChangeOperation" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
   <wsdl:types>
      <xsd:schema elementFormDefault="qualified" targetNamespace="http://www.service-now.com/ChangeOperation">
         <xsd:element name="changeOperationRequest">
            <xsd:complexType>
               <xsd:sequence>
                  <xsd:element maxOccurs="1" minOccurs="0" name="source_system" type="xsd:string"/>
                  <xsd:element maxOccurs="1" minOccurs="0" name="source_uid" type="xsd:string"/>
                  <xsd:element maxOccurs="1" minOccurs="0" name="request_type" type="xsd:string"/>
                  <xsd:element maxOccurs="1" minOccurs="0" name="request_payload" type="xsd:string"/>
               </xsd:sequence>
            </xsd:complexType>

使用SOAPUI成功的SOAP请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://www.service-now.com/ChangeOperation">
   <soapenv:Header/>
   <soapenv:Body>
      <ns0:changeOperationRequest>
         <!--Optional:-->
         <ns0:source_system>Script</ns0:source_system>
         <!--Optional:-->
         <ns0:source_uid>131318</ns0:source_uid>
         <!--Optional:-->
         <ns0:request_type>getChangeRequests</ns0:request_type>
         <!--Optional:-->
         <ns0:request_payload>
            <start_date>2020-01-08T00:00:00</start_date>
            <status_list>Proposed</status_list>
            <owning_stream>IB IT</owning_stream>
            <full_details>no</full_details>
            <result_limit>100</result_limit>
         </ns0:request_payload>

      </ns0:changeOperationRequest>
   </soapenv:Body>
</soapenv:Envelope>

是否可以覆盖从WSDL文件读取的数据类型,或者是否有其他方法可以强制Zeep将字段作为字符串发送?

我已经尝试过拆开字典的包装:

xml = client.service.changeOperationRequest(**request_dict)

并且设置关键字参数,并且仅将request_payload设置为字典,但是会导致相同的错误:

xml = client.create_message(client.service, 'changeOperationRequest', source_system='Script',source_uid='131318',request_type='getChangeRequests',request_payload=dictionary)

甚至只是简单地将request_payload设置为结果xml都不起作用,因为xml标签会被扩展。尽管我宁愿不必走手工创建xml的路线,但这似乎在一定程度上抵制了使用Zeep的观点。

xml = client.create_message(client.service, 'changeOperationRequest', source_system='EQ CAB Report Script',source_uid='131318',request_type='getChangeRequests',request_payload='<start_date>2020-01-07T00:00:00</start_date><status_list>Proposed</status_list><owning_stream>IB IT</owning_stream><full_details>no</full_details><result_limit>100</result_limit>')

XML输出:

<sn0:request_payload>&lt;start_date&gt;2020-01-07T00:00:00&lt;/start_date&gt;&lt;status_list&gt;Proposed&lt;/status_list&gt;&lt;owning_stream&gt;IB IT&lt;/owning_stream&gt;&lt;full_details&gt;no&lt;/full_details&gt;&lt;result_limit&gt;100&lt;/result_limit&gt;</ubs:request_payload>
python soap wsdl zeep
1个回答
0
投票

我已经走了将字典作为字符串类型发送的路线。为了不扩展对xml标签的转义,我使用了Zeep插件帮助器。

来自:python zeep: send un-escaped xml as content

from zeep import Client, xsd, Plugin

class my_plugin(Plugin):

    def egress(self, envelope, http_headers, operation, binding_options):
        xmlString = ET.tostring(envelope, encoding='unicode')
        xmlString = xmlString.replace("&lt;", "<")
        xmlString = xmlString.replace("&gt;", ">")
        newenvelope = ET.XML(xmlString)
        return newenvelope, http_headers

# create the client:
client = Client(wsdl_url, transport=transport, plugins=[my_plugin()])

# call service as you would normally
client.service.changeOperationRequest()
© www.soinside.com 2019 - 2024. All rights reserved.