python - Spyne 自定义 XML 响应
问题描述
我正在将 Spyne 与 Django CMS 一起使用。一个 Web 服务正在调用我的系统,我想回复以下内容。我可以使用 Spyne 进行自定义响应吗?还是我必须通过模型?
请指教。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<tns:initTestQueryResponse xmlns:tns="http://test.com/interface/test/v2"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://test.com/interface/test/v2 testQuery.xsd"
xmlns:v21="http://test.com/model/generic-query/v2"
xmlns:v22="http://test.com/model/common/v2">
<tns:field key="ID" type="ID">
<v21:description lang="en">Identifier</v21:description>
</tns:field>
<tns:field key="CUSTOMER_NAME" type="TEXT">
<v21:description lang="en">Customer Name</v21:description>
<v21:layoutOptions bold="true" italic="false" direction="HORIZONTAL"/>
</tns:field>
<tns:section key="CUSTOMER">
<v21:description lang="en">Customer</v21:description>
</tns:section>
<tns:advancedQuery>
<tns:criteriaGroup key="CUSTOMER" operator="OR">
<v21:criterion key="ID" />
<v21:criterion key="CUSTOMER_NAME" />
</tns:criteriaGroup>
</tns:advancedQuery>
<tns:advanceQueryPerson>
<tns:criteriaGroup key="CUSTOMER" operator="OR">
<v21:criterion key="ID" />
<v21:criterion key="CUSTOMER_NAME" />
</tns:criteriaGroup>
</tns:advanceQueryPerson>
<tns:context>
<v22:status>OK</v22:status>
</tns:context>
</tns:initTestQueryResponse>
</soapenv:Body>
</soapenv:Envelope>
这是请求
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<p:initTestQueryRequest xmlns:p="http://test.com/interface/test/v2"
xmlns:p1="http://test.com/model/common/v2"
xmlns:p2="http://test.com/model/generic-query/v2"
xmlns:p3="http://test.com/model/test/v2"
xmlns:p4="http://test.com/model/service-fault/v2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://test.com/interface/test/v2 ../service/test/v2/TestQuery.xsd "
xsi:type="anyType"/>
</soapenv:Body>
</soapenv:Envelope>
解决方案
你有两个选择:
使用名为 initTestQuery 的裸方法,其返回类型应为名为“initTestQueryResponse”的类,其命名空间为“ http://test.com/interface/test/v2 ”。您需要从函数返回一个
initTestQueryResponse
实例。initTestQuery
使用名为“initTestQuery”的裸方法,其返回类型为 AnyXml。您需要返回包含所需标签的 lxml.etree.Element()。请查阅 lxml 文档以了解如何执行此操作。
如果您想在反序列化之后但在验证之前“编辑”请求,您必须子类化协议并覆盖create_in_document
。
class MyProtocol(Soap11):
def create_in_document(self, ctx, charset=None):
super(MyProt, self).create_in_document(ctx, charset=charset)
# Do whatever you want with ctx.in_document
app = Application(in_protocol=MyProtocol(...), ...)
我希望这有帮助。
推荐阅读
- php - Netbilling Post 客户 IP
- visual-studio-2017 - 是否可以将 Visual Studio 的管理机密功能与 Azure 服务结构一起使用
- python - 我无法理解递归中的返回命令
- spacy - 我的问题是关于“模块'textacy'没有属性'Doc'”
- r - 如何在R中重复机器学习算法设置种子?
- r - 如果不直接使用模型输出计算损失,Tensorflow 急切执行不会获得梯度
- python - 熊猫将列值拆分为单独的列
- wordpress - Wordpress 导航错误未定义属性:WP_Post_Type::$term_id
- javascript - 具有动态内容和行为的导航栏
- javascript - 谷歌标签管理器点击类事件不起作用