gpt4 book ai didi

python - Zeep 发送缺少强制标签的请求

转载 作者:太空宇宙 更新时间:2023-11-03 20:24:57 27 4
gpt4 key购买 nike

我的 SOAP 服务将使用各种无效负载进行测试,以确认返回适当的响应。

在进行测试之前,我想对我的服务进行自己的测试。一些测试涉及删除必填字段。我想使用 zeep 模拟这些测试,但 zeep 不允许我发送请求,如果我想要的数据中不存在任何必填字段,就会给我一个 zeep.exceptions.ValidationError 错误发送。

我可以配置一些设置,以便 zeep 不会因缺少字段而引发错误并发送无效请求吗?

示例代码:

from zeep import Client
from datetime import datetime

wsdl_url = 'http://myservice.com/egservice?wsdl'

payload = {
'ServiceType': 'EgService',
'AvailabilityWindow': [
{'StartDateTime': datetime.now(),
'EndDateTime': datetime.now(),
'Validation': 'VALID'}],
'Confirmation': 'Confirmed',
'DateTimeStamp': datetime.now()
} # N.B No ContractID included

soap_client = Client(wsdl_url)
operation = 'myExampleOperation'
with soap_client.settings(raw_response=True):
response = soap_client.service[operation](**payload)

wsdl的相关部分:

<xs:complexType name="EgMessage">
<xs:sequence>
<xs:element name="ServiceType" type="tns:EgMessage_ServiceTypeType"/>
<xs:element name="ContractID" type="tns:EgMessage_ContractIDType"/>
<xs:element name="AUI" type="tns:EgMessage_AUIType" minOccurs="0"/>
<xs:element name="AvailabilityWindow" type="tns:AvailabilityWindowType" maxOccurs="unbounded"/>
<xs:element name="Confirmation" type="tns:EgMessage_ConfirmationType"/>
<xs:element name="FileReason" type="tns:EgMessage_FileReasonType" minOccurs="0"/>
<xs:element name="DateTimeStamp" type="xs:dateTime"/>
</xs:sequence>
</xs:complexType>

回溯:

Traceback (most recent call last):
File "C:/Users/Thomas.Gabereau/PycharmProjects/pas_rmq/pas_gateway/inbound/test_local_soap.py", line 157, in <module>
status, fault_field, fault = send_to_grid(soap_client, operation, payload)
File "C:/Users/Thomas.Gabereau/PycharmProjects/pas_rmq/pas_gateway/inbound/test_local_soap.py", line 129, in send_to_grid
response = soap_client.service[operation](**payload)
File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\proxy.py", line 45, in __call__
kwargs,
File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\wsdl\bindings\soap.py", line 119, in send
operation, args, kwargs, client=client, options=options
File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\wsdl\bindings\soap.py", line 68, in _create
serialized = operation_obj.create(*args, **kwargs)
File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\wsdl\definitions.py", line 215, in create
return self.input.serialize(*args, **kwargs)
File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\wsdl\messages\soap.py", line 74, in serialize
self.body.render(body, body_value)
File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\xsd\elements\element.py", line 231, in render
self._render_value_item(parent, value, render_path)
File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\xsd\elements\element.py", line 255, in _render_value_item
return self.type.render(node, value, None, render_path)
File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\xsd\types\complex.py", line 279, in render
element.render(parent, element_value, child_path)
File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\xsd\elements\indicators.py", line 242, in render
element.render(parent, element_value, child_path)
File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\xsd\elements\element.py", line 225, in render
self.validate(value, render_path)
File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\xsd\elements\element.py", line 280, in validate
"Missing element %s" % (self.name), path=render_path
zeep.exceptions.ValidationError: Missing element ContractID(myExampleOperation.ContractID)

最佳答案

我遇到了同样的问题,然后找到了解决该问题的方法。有一个选项可以跳过必填字段的验证。您必须将要跳过的属性的值设置为 xsd.skipvalue。在您的场景中,将contractId的值设置为有效负载对象中的xsd.skipvalue

from zeep import xsd

payload = {
'ServiceType': 'EgService',
'ContractID':xsd.SkipValue,
'AvailabilityWindow': [
{'StartDateTime': datetime.now(),
'EndDateTime': datetime.now(),
'Validation': 'VALID'}],
'Confirmation': 'Confirmed',
'DateTimeStamp': datetime.now()
}

关于python - Zeep 发送缺少强制标签的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57905715/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com