如何避免Spyne做出回应? [英] How do I keep Spyne from wrapping my response?

查看:117
本文介绍了如何避免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并失败了

I don't want response to be wrapped into my_methodResponse. I tried to add _body_style='bare' to @srpc and got the fail

<?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,并且无法在响应中添加任何包装器.

How should I deal with it? I am trying to implement strict API and cannot add any wrappers to my responses.

推荐答案

从此请求来看:

<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

与非裸露模式的实际效果非常接近.

Which is very close to what the non-bare mode actually does.

这篇关于如何避免Spyne做出回应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆