Spyne / RPClib - 如何使用ComplexModel进行响应?

时间:2012-09-17 14:36:01

标签: python django soap wsdl xmlrpclib

我第一次使用SPYNE而且我对如何回应我的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

有人知道如何填充ComplexModels吗? docsexamples无法帮助我。

1 个答案:

答案 0 :(得分:5)

我找到了解决方案。这是一个小型演示。

#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