Search code examples
soaptwistedspyne

Custom tags with spyne


Im trying to set up a small SOAP 1.1 server with twisted and spyne, but I can't seem to find anything on how to create custom tags(body), namespaces, or headers for my responses. Is there a better way to do this than creating my own ProtocolBase?

The goal is to have soap responses that look like this:

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cwmp="urn:dslforum-org:cwmp-1-2">
<SOAP-ENV:Header>
    <cwmp:ID SOAP-ENV:mustUnderstand="1">123456789</cwmp:ID>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
    <cwmp:SetParameterValues>
        <ParameterList SOAP-ENC:arrayType="cwmp:ParameterValueStruct[1]">
            <ParameterValueStruct>
                <Name>MY.NAME</Name>
                <Value xsi:type="xsd:unsignedInt">4000</Value>
            </ParameterValueStruct>
        </ParameterList>
        <ParameterKey>Axess Update parameters</ParameterKey>
    </cwmp:SetParameterValues>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Im able to produce what I'm looking for by creating my own protocol for which I hand a string of xml similar to below.

class spyne(ServiceBase):
    isLeaf = TwistedWebResource

    @rpc(AnyDict, AnyDict, _returns=Unicode)
    def my_rpc(ctx, DeviceId, ParameterList):

        out_document = etree.parse('data.xml')

        return etree.tostring(out_document, pretty_print=True,
                                         encoding='utf8', xml_declaration=False)

class my_protocol(XmlDocument):

    mime_type = 'text/xml; charset=utf-8'
    type = set(XmlDocument.type)
    type.update(('soap', 'soap11'))

    def __init__(self, *args, **kwargs):
        super(TR_069_SOAP, self).__init__(*args, **kwargs)

    def serialize(self, ctx, message):
        ctx.out_document = ctx.out_object[0]

    def create_out_string(self, ctx, charset=None):
       ctx.out_string = [ctx.out_document] 

I'm not sure if there is a better way to be doing this.


Solution

  • Spyne does not support serializing array using the SOAP-ENC:arrayType="cwmp:ParameterValueStruct[1]" style, whatever that means.

    You don't need your own protocol but you do need to override XmlDocument's complex_to_parent in Soap11 and add special handling for arrays that have, say, Array(SomeType, array_type="cwmp:ParameterValueStruct[1]").

    You can either patch Spyne itself and send a pull request my way or create a Soap11 subclass (NOT XmlDocument subclass) of your own and write overriding code there.

    Chime in at http://lists.spyne.io/listinfo/people if you wish to proceed either way.