Spyne / Python /肥皂.向AnyDict中添加xsi:type



我用Spyne &我尝试添加xsi:type="xsd:string">

现在我有了这个:

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

你有什么办法可以帮我解决这个问题吗?

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

谢谢@Burak🙂,我的代码现在:

@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

最新更新