如何避免Spyne做出回应

如何避免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,无法向我的响应添加任何包装器。

最佳答案

根据这一要求判断:

<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

这与非裸模式的实际功能非常接近。

关于python - 如何避免Spyne做出回应?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19536282/

10-09 02:05