Remove the namespace from Spyne response variables

随声附和 提交于 2019-12-19 10:07:30

问题


Implementing a WebService according to a specific WSDL. Client cannot be changed. Correctly processing request from Client, but Client is complaining on Response because of namespace in variables.

What I want (soapUI response based on WSDL):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cal="http://callback.foo.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <cal:foo_statusResponse>
         <result>SUCCESS</result>
         <notify>Thanks!</notify>
      </cal:foo_statusResponse>
   </soapenv:Body>
</soapenv:Envelope>

What I am getting (notice tns: on variables causing validation issues):

<senv:Envelope xmlns:tns="http://callback.foo.com/" xmlns:senv="http://schemas.xmlsoap.org/soap/envelope/">
  <senv:Body>
    <tns:foo_statusResponse>
      <tns:result>SUCCESS</tns:result>
      <tns:notify>Thanks!</tns:notify>
    </tns:foo_statusResponse>
  </senv:Body>
</senv:Envelope>

Java Client is throwing this exception:

[com.sun.istack.SAXParseException2; lineNumber: 2; columnNumber: 162; unexpected element (uri:"http://callback.foo.com/", local:"result"). Expected elements are <{}result>,<{}notify>]

Implementation snippet:

class fooStatusRS(ComplexModel):
    result = Unicode()
    notify = Unicode()

class foo_callback(ServiceBase):
    @srpc(Unicode, Unicode, Unicode, Unicode, statusbarInfo, anotherResponse, 
            _out_header=None, 
            _out_variable_names=("result", "notify"), 
            _returns=(Unicode, Unicode), 
            _out_message_name="foo_statusResponse",
            _operation_name="foo_status_rq")
    def foo_status(foo_id, reply, ref, status, statusbar, another):
        if foo_id:
            print foo_id

        return fooStatusRS(result="SUCCESS", notify="Foo received!")

回答1:


This is not possible (yet) and is answered in a similar question here by the maintainer.

The work around was to add listener to the event_manager for "method_return_string" and then perform some string operations.

def _method_return_string(ctx):
    ctx.out_string[0] = ctx.out_string[0].replace("tns:result>", "result>")
    ctx.out_string[0] = ctx.out_string[0].replace("tns:notify>", "notify>")



回答2:


it can be fixed by overriding nsmap in application.interface

    def fix_nsmap(application):
        conversion_dict = {
            'tns': None,
            'senv': 'soap',
        }
        nsmap = application.interface.nsmap
        for k, v in conversion_dict.iteritems():
            nsmap[v] = nsmap[k]
            del nsmap[k]

        application.interface.nsmap = nsmap

    application_security2 = Application(
        [Security2Service],
        tns=NS,
        name='Security2',
        in_protocol=Soap11(),
        out_protocol=Soap11()
    )

    fix_nsmap(application_security2)

nsmap[None] set the default NS




回答3:


If you wonder how to add listener to the event_manager for method_return_string, see bellow a full example:

from spyne import Application, rpc, ServiceBase, Iterable, Integer, Unicode

from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication


class HelloWorldService(ServiceBase):
    @rpc(Unicode, Integer, _returns=Iterable(Unicode))
    def say_hello(ctx, name, times):
        for i in range(times):
            yield u'Hello, %s' % name


def on_method_return_string(ctx):
    ctx.out_string[0] = ctx.out_string[0].replace(b'Hello>', b'Good by')

HelloWorldService.event_manager.add_listener('method_return_string', 
                                              on_method_return_string)

application = Application([HelloWorldService], 'spyne.examples.hello.soap',
                          in_protocol=Soap11(validator='lxml'),
                          out_protocol=Soap11())

wsgi_application = WsgiApplication(application)


if __name__ == '__main__':
    import logging

    from wsgiref.simple_server import make_server
    server = make_server('127.0.0.1', 8000, wsgi_application)
    server.serve_forever()

As of Spyne 2.12 this is still the only way to remove namespaces from response variables.



来源:https://stackoverflow.com/questions/28832969/remove-the-namespace-from-spyne-response-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!