Spyne/Python/Soap,将xsi:type添加到AnyDict

55ooxyrt  于 2023-01-12  发布在  Python
关注(0)|答案(2)|浏览(95)

我正在使用Spyne &我试图将xsi:type=“xsd:string”添加到我的AnyDict结果中作为响应。
现在我有了这个

<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>

但我需要得到:

<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>

我的服务:

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'}

我的应用声明:

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

你有什么办法可以帮我解决吗?

mf98qq94

mf98qq941#

AnyDict无法处理这种用例--它根本没有任何方法来存储额外的元数据,必须将返回类型设置为AnyXml,并返回一个具有任何所需属性的Element对象。

nx7onnlm

nx7onnlm2#

谢谢你🙂,
我的代码:

@rpc(
                AnyDict,
                _out_message_name = 'ERespons',
                _returns=AnyXml
        )

        def AddEntry(ctx, data):

                data = get_object_as_dict(data)
                
                qname = etree.QName("http://www.w3.org/2001/XMLSchema-instance", "type")

                root = etree.Element('EResponsRecord')

                s = etree.SubElement(root, 'state')
                s.attrib[qname] = 'string'

                e = etree.SubElement(root, 'err_msg')
                e.attrib[qname] = 'string'

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

                        s.text = res['state']
                        e.text = res['err_msg']
                        
                except Exception as e:
                        logging.exception(e) 

                        s.text = 'failed' 
                        e.text = 'Nastala chyba'

                return root

相关问题