Spyne/Python/Soap. Add xsi:type to AnyDict
Question:
Im using Spyne & I try to add xsi_type="xsd:string" to my AnyDict result in response .
Now i have this one:
<soap11env:Envelope
__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 ?
Answers:
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.
Thanks @Burak , my code now:
@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
Im using Spyne & I try to add xsi_type="xsd:string" to my AnyDict result in response .
Now i have this one:
<soap11env:Envelope
__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 ?
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.
Thanks @Burak , my code now:
@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