Qualified element/attribute forms and unqualified forms with Spyne soap server

♀尐吖头ヾ 提交于 2019-12-06 05:21:00

问题


Is there any way to use elementFormDefault="unqualified" server schema type with Spyne server? Now my all trials end up with method response result:

<senv:Envelope xmlns:tns="http://test.com/remoteService/"
xmlns:senv="http://schemas.xmlsoap.org/soap/envelope/">
<senv:Body>
    <tns:testResponse>
        <tns:status>ok</tns:status>
    </tns:testResponse>
</senv:Body>

And generated wsdl fragment with "qualified" elementFormDefault :

<xs:schema targetNamespace="http://test.com/remoteService/" elementFormDefault="qualified"></xs:schema>

How to configure method or parameters model to get result like this:

<senv:Envelope xmlns:tns="http://test.com/remoteService/"
xmlns:senv="http://schemas.xmlsoap.org/soap/envelope/">
<senv:Body>
    <tns:testResponse>
        <status>ok<status>
    </tns:testResponse>
</senv:Body>

My goal is to generate result where child element:

<tns:status>ok</tns:status>

will appear without namespace prefix - like this:

<status>ok<status>

回答1:


If you wonder how to add listener to the event_manager for method_return_string or for another event, 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.




回答2:


As of 2.10, Spyne does not support this.

The patch would be a bit hairy. Chime in at soap@python.org if you're willing to work on this.

A workaround would be to remove namespace prefixes manually from outgoing documents in a method_return_document hook. If you need to enforce the same for incoming documents as well, you either have to modify the Wsdl as well in a document_built event, or use soft validation (soft validation does not care about namespaces) or no validation at all.



来源:https://stackoverflow.com/questions/16420036/qualified-element-attribute-forms-and-unqualified-forms-with-spyne-soap-server

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