Search code examples
pythonsoapwsdlspynexsi

Spyne/Python/Soap. Add xsi:type to AnyDict


Im using Spyne & I try to add xsi:type="xsd:string" to my AnyDict result in response .

Now i have this one:

<soap11env:Envelope xmlns:soap11env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://localhost/nusoap">
   <soap11env:Body>
      <tns:ERespons>
         <tns:EResponsRecord>
            <state>failed</state>
            <err_msg>Nastala chyba</err_msg>
         </tns:EResponsRecord>
      </tns:ERespons>
   </soap11env:Body>
</soap11env:Envelope>

But i need to get :

<soap11env:Envelope xmlns:soap11env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://localhost/nusoap">
   <soap11env:Body>
      <tns:ERespons>
         <tns:EResponsRecord>
            <state xsi:type="xsd:string">failed</state>
            <err_msg xsi:type="xsd:string">Nastala chyba</err_msg>
         </tns:EResponsRecord>
      </tns:ERespons>
   </soap11env:Body>
</soap11env:Envelope>

My Service:

class AddEntryEC(ServiceBase):
        EntryObject.__namespace__ = 'Entry.soap'
        __out_header__ = EntryObject

        @rpc(
                AnyDict,
                _out_message_name = 'ERespons',
                _out_variable_name = 'EResponsRecord',
                _returns=AnyDict
        )

        def AddEntry(ctx, data):

                data = get_object_as_dict(data)
                try :
                        ctx.app.db_tool.set_args(data)
                        res = ctx.app.db_tool.insert_data()
                        
                        return res

                except Exception as e:
                        logging.exception(e) 
                        return  {'state' : 'failed',
                                'err_msg' : 'Nastala chyba'}

My app declaration :

application = MyApplication([AddEntryEC], 'http://localhost/nusoap', in_protocol=Soap11(validator='soft'), out_protocol=Soap11())

Do u have some idea to help me with solution ?


Solution

  • AnyDict can't handle this use case -- it simply doesn't have any means to store the additional metadata. You must set your return type as AnyXml and return an Element object with any desired attributes.