예제 #1
0
    async def dispatch(request: 'SanicRequest'):
        req = request.json
        url = request.url

        context = {"url": url}

        response: Union[Response, DictResponse, BatchResponse]
        try:
            client_ip = request.remote_addr if request.remote_addr else request.ip
            Logger.info(f'rest_server_v2 request with {req}', DISPATCH_V2_TAG)
            Logger.info(f"{client_ip} requested {req} on {url}")

            validate_jsonschema_v2(request=req)
        except GenericJsonRpcServerError as e:
            Logger.debug(f'dispatch() validate exception = {e}')
            response = ExceptionResponse(e, id=req.get('id', 0), debug=False)
        else:
            response = await async_dispatch(request.body,
                                            methods,
                                            context=context)

        Logger.info(f'rest_server_v2 response with {response}',
                    DISPATCH_V2_TAG)
        return sanic_response.json(response.deserialized(),
                                   status=response.http_status,
                                   dumps=json.dumps)
예제 #2
0
    async def dispatch(request: 'SanicRequest', channel_name: str = ""):
        """Node dispatch

        FIXME : this dispatch is not considered to support batch request.
            If you want to support batch request, need to update code that using req_json.
        """

        req_json = request.json
        url = request.url
        channel = channel_name if channel_name else StubCollection().conf[
            ConfigKey.CHANNEL]
        req_json['method'] = convert_upper_camel_method_to_lower_camel(
            req_json['method'])

        if 'params' in req_json and 'message' in req_json[
                'params']:  # this will be removed after update.
            req_json['params'] = req_json['params']['message']

        context = {'url': url, 'channel': channel}

        response: Union[Response, DictResponse, BatchResponse]
        try:
            client_ip = request.remote_addr if request.remote_addr else request.ip
            Logger.info(f'rest_server_node request with {req_json}',
                        DISPATCH_NODE_TAG)
            Logger.info(f'{client_ip} requested {req_json} on {url}')

            validate_jsonschema_node(request=req_json)
        except GenericJsonRpcServerError as e:
            response = ExceptionResponse(e,
                                         id=req_json.get('id', 0),
                                         debug=False)
        except Exception as e:
            response = ExceptionResponse(e,
                                         id=req_json.get('id', 0),
                                         debug=False)
        else:
            response = await async_dispatch(request.body,
                                            methods,
                                            context=context)

        Logger.info(f'rest_server_node with response {response}',
                    DISPATCH_NODE_TAG)
        return sanic_response.json(response.deserialized(),
                                   status=response.http_status,
                                   dumps=json.dumps)
예제 #3
0
    async def dispatch(request, channel_name=None):
        req_json = request.json
        url = request.url
        # channel = channel_name if channel_name is not None \
        #     else StubCollection().conf[ConfigKey.CHANNEL]

        context = {
            "url": url,
            "channel": 'icon_dex',
        }

        try:
            client_ip = request.remote_addr if request.remote_addr else request.ip
            Logger.info(f'rest_server_v3 request with {req_json}',
                        DISPATCH_V3_TAG)
            Logger.info(f"{client_ip} requested {req_json} on {url}")

            validate_jsonschema_v3(request=req_json)
        except GenericJsonRpcServerError as e:
            response = ExceptionResponse(e,
                                         id=req_json.get('id', 0),
                                         debug=False)
        except Exception as e:
            response = ExceptionResponse(e,
                                         id=req_json.get('id', 0),
                                         debug=False)
        else:
            if "params" in req_json:
                req_json["params"]["context"] = context
            else:
                req_json["params"] = {"context": context}
            response: DictResponse = await async_dispatch(
                json.dumps(req_json), methods)
        Logger.info(f'rest_server_v3 with response {response}',
                    DISPATCH_V3_TAG)
        return sanic_response.json(response.deserialized(),
                                   status=response.http_status,
                                   dumps=json.dumps)