Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 599
user2358335 Avatar asked May 07 '13 13:05

user2358335


2 Answers

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.

like image 183
FelixEnescu Avatar answered Nov 03 '22 20:11

FelixEnescu


As of 2.10, Spyne does not support this.

The patch would be a bit hairy. Chime in at [email protected] 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.

like image 1
Burak Arslan Avatar answered Nov 03 '22 21:11

Burak Arslan