[英]Spyne/Python/Soap. Add xsi:type to AnyDict
我正在使用 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())
你有什么想法可以幫助我解決問題嗎?
AnyDict
無法處理這種用例——它根本沒有任何方法來存儲額外的元數據。 您必須將返回類型設置為AnyXml
並返回具有任何所需屬性的Element
object。
謝謝@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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.