Spyne/RPClib - How to respond with ComplexModel?
문제
I'm using SPYNE for the very first time and I'm a little bit confused of how to respond with my ComplexModel.
#service.py
class Status(ComplexModel):
statusCode = Integer
statusMsg = String
class ResponseData(ComplexModel):
status = Array(Status)
version = Integer
class SoapService(ServiceBase):
@rpc(String, _returns=ResponseData)
def doSomething(ctx, name):
# ...
# do something to get a django-model-object
obj_data = Servers.objects.get(host=name)
# put vars from the django-model-object into 'ResponseData' and nested 'Status'
# e.g. statusCode = 1, statusMsg = 'online', version = 1
""" experimental - status is not returned - version is 1:
obj_status = Status()
obj_status.statusCode = 1
obj_status.statusMsg = 'online'
retval = ResponseData()
retval.status = obj_status
retval.version = 1
"""
return retval
Does anybody got an idea of how to fill the ComplexModels? The docs and examples couldn't help me.
해결책
I've found the solution. Here a small demo.
#service.py
class Status(ComplexModel):
statusCode = Integer
statusMsg = String
class ResponseData(ComplexModel):
status = Array(Status)
version = Integer
class SoapService(ServiceBase):
@rpc(String, _returns=ResponseData)
def doSomething(ctx, name):
obj_data = Servers.objects.get(host=name)
if obj_data.status:
my_statusCode = 1
my_statusMsg = 'online'
else:
my_statusCode = 0
my_statusMsg = 'offline'
retval = ResponseData()
retval.status = [{'statusCode': my_statusCode, 'statusMsg': my_statusMsg}]
retval.version = 1
return retval
제휴하지 않습니다 StackOverflow