如何让Spyne不要回复我的回复?

时间:2013-10-23 08:13:33

标签: python xml soap spyne

以下是示例服务

NS = 'my_app_namespace'

class MyMethodRS(ComplexModel):
    __tns__ = NS
    Version = XmlAttribute(Unicode)

class MyService(ServiceBase):
    __tns__ = NS

    @srpc(Unicode, _returns=MyMethodRS, _out_variable_name='MyMethodRS')
    def my_method(foo):
        response = MyMethodRS()
        response.Version = '1.0'
        return response

# and then application will be created and starged as wsgi app

然后我发布了一个请求

<?xml version='1.0' encoding='UTF-8' ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
  <soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <my_method xmlns="my_app_namespace">
      <foo>bar</foo>
    <my_method>
  </soap:Body>
</soap:Envelope>

我得到了

<?xml version='1.0' encoding='UTF-8' ?>
<senv:Envelope>
  <senv:Body>
    <tns:my_methodResponse>
      <tns:MyMethodRS Version="1.0" />
    </tns:my_methodResponse>
  </senv:Body>
</senv:Envelope>

我不希望将回复包含在my_methodResponse中。我尝试将_body_style='bare'添加到@srpc并获得失败

<?xml version='1.0' encoding='UTF-8' ?>
<senv:Envelope>
  <senv:Body>
    <senv:Fault>
      <faultcode>senv:Client.SchemaValidationError</faultcode>
      <faultstring><string>:14:0:ERROR:SCHEMASV:SCHEMAV_CVC_TYPE_3_1_2: Element '{http://www.opentravel.org/OTA/2003/05}my_method': Element content is not allowed, because the type definition is simple.</faultstring>
      <faultactor />
    </senv:Fault>
  </senv:Body>
</senv:Envelope>

我该如何处理?我正在尝试实现严格的API,并且不能在我的响应中添加任何包装器。

1 个答案:

答案 0 :(得分:3)

从这个要求判断:

<my_method xmlns="my_app_namespace">
  <foo>bar</foo>
<my_method>

你需要这个:

class MyMethodRequest(ComplexModel):
    foo = Unicode

class MyService(ServiceBase):
    @srpc(MyMethodRequest, _returns=MyMethodRS, _body_style='bare' 
                           _out_variable_name='MyMethodRS')
    def my_method(request):
        foo = request.foo
        response = MyMethodRS()
        response.Version = '1.0'
        return response

这与非裸模式实际上非常接近。