我必须与某种代理服务进行通信,该服务会在
<Body>
中内嵌 XML 文档并将其进一步传递。问题是该服务提供了损坏的 WSDL,其操作定义为 operation() -> None
但它实际上需要 operation(any-xml) -> any-xml
。
我可以编辑此 WSDL 并将其提供给我的客户端生成器 (python zeep),但我不知道如何定义接受任何 XML 结构的 WSDL 操作。
我已经用相当肮脏的技巧克服了这个问题。
zeep
支持插件,这些插件可以处理请求。我的插件将 XML 文档注入到 soap-env:Body
中。以下是有关如何设置自己的插件的文档:https://docs.python-zeep.org/en/master/plugins.html
我的插件看起来像这样:
from lxml import etree
from zeep import Plugin
from typing import Optional
import xml.etree.ElementTree as ET
namespaces = {"soap-env": "http://schemas.xmlsoap.org/soap/envelope/"}
class MyProxyPlugin(Plugin):
def __init__(self) -> None:
self.xml: Optional[ET.Element] = None
def ingress(self, envelope, http_headers, __operation__):
return envelope, http_headers
def egress(self, envelope, http_headers, __operation__, __binding_options__):
if self.xml is not None:
body = envelope.find("./soap-env:Body", namespaces)
body.append(self.xml)
return envelope, http_headers
在每次调用这个有问题的方法之前,我必须设置
my_plugin.xml = element_tree_root
然后取消设置。