【问题标题】:Overriding type from WSDL file with Zeep使用 Zeep 覆盖 WSDL 文件中的类型
【发布时间】:2020-01-08 20:36:53
【问题描述】:

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】:

    我已经将字典作为字符串类型发送。为了不扩展 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多