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



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>

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

取自:python zeep:将未转义的xml作为内容发送

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()

最新更新