我有一个工作应用程序,它接受 SOAP 请求、处理请求、将 SOAP 请求转发到 API、处理响应,然后将响应转发给客户端。
我正在尝试更改此应用程序,使其在我的应用程序和客户端之间是 JSON,但在 API 和我的应用程序之间仍然使用 SOAP
现在,它可以成功地接受来自客户端的 JSON 请求并使用 API 发送/接收 SOAP。但是,对客户端的所有响应都是空的。
我收到非空响应的唯一情况是我的 JSON 请求存在验证错误。
以下是一些可能相关的代码
app = Application([MyServer],
MY_NAMESPACE,
in_protocol=JsonDocument(validator='soft'),
out_protocol=JsonDocument())
application_server = csrf_exempt(MyDjangoApplication(app))
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的定义
class MyServer(ServiceBase):
@rpc(MyTestMethodRequest, Sign, **method(_returns=MyTestMethodResponse))
@check_method()
def TestMethod(ctx, request, signature):
response = {
'Data': "test"
}
return response
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的定义:
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 的定义:
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 ''
注意:这些定义中的大部分已经被简化(我已经删除了我认为不相关的行)