本文介绍了Spyne:为什么我收到针对json请求的空响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正常工作的应用程序,它接受SOAP请求,处理请求,将SOAP请求转发给API,处理响应,然后将响应转发给客户端.

I have a working application that accepts SOAP requests, processes the requests, forwards the SOAP request to an API, processes the response, and then forwards the response to the client.

我正在尝试更改此应用程序,以便在我的应用程序和客户端之间使用JSON格式,但仍在API和我的应用程序之间使用SOAP

I'm trying to change this application so that it will be JSON between my application and the client but still use SOAP between API and my application

现在,它可以成功接受来自客户端的JSON请求并使用API​​发送/接收SOAP.但是,所有对客户端的响应都是空的.

Now, it can successfully accept JSON requests from client and send/receive SOAP with API. However, all the responses to client are empty.

我收到非空响应的唯一情况是JSON请求出现验证错误.

The only case that I receive a non-empty response is when there are validation errors with my JSON request.

以下是一些可能相关的代码

Here are some code that might be relevant

app = Application([MyServer],
              MY_NAMESPACE,
              in_protocol=JsonDocument(validator='soft'),
              out_protocol=JsonDocument())

application_server = csrf_exempt(MyDjangoApplication(app))

MyDjangoApplication的定义

definition of MyDjangoApplication

class MyDjangoApplication(DjangoApplication):
def __call__(self, request, *args, **kwargs):
    retval = self.HttpResponseObject()

    def start_response(status, headers):
        # Status is one of spyne.const.http
        status, reason = status.split(' ', 1)

        retval.status_code = int(status)
        for header, value in headers:
            retval[header] = value

    environ = request.META.copy()

    if request.method == 'POST':
        response = self.handle_rpc(environ, start_response)
    else:
        home_path = reverse('proxy:list_method')

        uri = MY_ENDPOINT_URL or request.build_absolute_uri(home_path)

        # to generate wsdl content
        response = self._WsgiApplication__handle_wsdl_request(environ, start_response, uri)

        if request.path == home_path and _is_wsdl_request(environ):
            fn = None
        elif 'method_name' in kwargs:
            fn = view_method
        else:
            fn = list_method

        if fn:
            return fn(request, app=self, *args, **kwargs)

    self.set_response(retval, response)

    return retval

MyServer的定义

Definition of MyServer

class MyServer(ServiceBase):
    @rpc(MyTestMethodRequest, Sign, **method(_returns=MyTestMethodResponse))
    @check_method()
    def TestMethod(ctx, request, signature):
        response = {
            'Data': "test"
        }
        return response

MyTestMethodRequest,MyTestMethodResponse的定义:

Definitions of MyTestMethodRequest, MyTestMethodResponse:

class MyTestMethodRequest(ComplexModel):
    __namespace__ = MY_NAMESPACE

    MyString = String(encoding=STR_ENCODING)


class MyTestMethodResponse(ComplexModel):
    __namespace__ = MY_NAMESPACE

    Data = String(encoding=STR_ENCODING)

check_method的定义:

Definition of check_method:

def check_method(error_handler=None):
    def _check_method(func):
        method_name = func.__name__

        def __check_method(ctx, request, signature, *args, **kwargs):
            if hasattr(request, '__dict__'):
                request = request.__dict__

            if hasattr(signature, '__dict__'):
                signature = signature.__dict__

            response = func(ctx, request or {}, signature or {}, *args, **kwargs)

            # setting output protocol
            output_message = generate_out_string(ctx, [response])

            return response

        __check_method.__name__ = method_name
        __check_method.__doc__ = func.__doc__

        return __check_method

    return _check_method

generate_out_string的定义:

Definition of generate_out_string:

def generate_out_string(ctx, objects):
    ctx.out_protocol = ctx.in_protocol

    return _generate_out_string(ctx, objects)

def _generate_out_string(ctx, objects):
    protocol = ctx.out_protocol

    ctx.out_object = objects

    protocol.serialize(ctx, protocol.RESPONSE)
    protocol.create_out_string(ctx)

    out_string = list(ctx.out_string)

    return out_string[0] if out_string else ''

注意:这些定义中的大多数已简化(我删除了我认为不相关的行)

Note: Most of these definitions have been simplified (I have removed lines which I think are not relevant)

推荐答案

看看您发布的代码,我不能说我理解围绕参数的所有其他修饰符和修饰符的作用.

Looking at the code you posted, I can't say I understand what good all those additional decorators and modifiers around arguments do.

删除它们应该可以解决您所有的问题.

Removing them should fix all of your problems.

所以让我们:

class MyTestMethodRequest(ComplexModel):
    __namespace__ = MY_NAMESPACE

    MyString = Unicode


class MyTestMethodResponse(ComplexModel):
    __namespace__ = MY_NAMESPACE

    Data = Unicode

假设您具有以下服务:

class MyService(ServiceBase):
    @rpc(MyTestMethodRequest, Sign, _returns=MyTestMethodResponse)
    def TestMethod(ctx, request, signature):
        return MyTestMethodResponse(data="test")

您可以拥有:

app_json = Application([MyService],
              MY_NAMESPACE,
              in_protocol=JsonDocument(validator='soft'),
              out_protocol=JsonDocument())

app_soap = Application([MyService],
              MY_NAMESPACE,
              in_protocol=Soap11(validator='lxml'),
              out_protocol=Soap11())

,您可以照常传递给DjangoApplication:

app_json_dja = csrf_exempt(DjangoApplication(app_json))

app_soap_dja = csrf_exempt(DjangoApplication(app_soap))

您可以将其安装在Django的url路由器中.

which in turn you can mount in Django's url router.

我希望这会有所帮助!

这篇关于Spyne:为什么我收到针对json请求的空响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:02