首页 > 解决方案 > 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>

标签: pythonxmlsoapwsdlspyne

解决方案


你有两个选择:

  1. 使用名为 initTestQuery 的裸方法,其返回类型应为名为“initTestQueryResponse”的类,其命名空间为“ http://test.com/interface/test/v2 ”。您需要从函数返回一个initTestQueryResponse实例。initTestQuery

  2. 使用名为“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(...), ...)

我希望这有帮助。


推荐阅读