예제 #1
0
def test_create_requests():
    requests = create_requests({
        "jsonrpc": "2.0",
        "method": "ping"
    },
                               convert_camel_case=False)
    assert isinstance(requests, Request)
예제 #2
0
def test_create_requests_batch():
    requests = create_requests(
        [{
            "jsonrpc": "2.0",
            "method": "ping"
        }, {
            "jsonrpc": "2.0",
            "method": "ping"
        }],
        convert_camel_case=False,
    )
    assert iter(requests)
async def dispatch_pure(request: str, methods: Methods, *, caller: Caller,
                        context: Any, convert_camel_case: bool,
                        debug: bool) -> Response:
    try:
        deserialized = validate(deserialize(request), schema)
    except JSONDecodeError as exc:
        return InvalidJSONResponse(data=str(exc), debug=debug)
    except ValidationError as exc:
        return InvalidJSONRPCResponse(data=None, debug=debug)
    return await call_requests(
        create_requests(deserialized,
                        context=context,
                        convert_camel_case=convert_camel_case),
        methods,
        caller=caller,
        debug=debug,
    )
예제 #4
0
def dispatch(request_raw: str) -> JsonRpcResponse:
    """Dispatch a request (or requests) to methods.

    This is the main public method, it's the only one with optional params, and the only
    one that can be configured with a config file/env vars.

    Args:
        request_raw: The incoming request string.

    Returns:
        A Response.
    """
    methods = global_methods
    try:
        request_json: JSON = json.loads(request_raw)
        validator.validate(request_json)
    except JSONDecodeError as exc:
        return InvalidJSONResponse(data=str(exc), debug=True)
    except ValidationError:
        return InvalidJSONRPCResponse(data=None)

    request = create_requests(request_json, convert_camel_case=False)
    assert isinstance(request, Request)

    try:
        method_name = request.method
        method = methods.items[method_name]

        with sentry_sdk.start_span(op="rpc", transaction="rpc." + method_name) as span:
            span.set_data("args", request.args)
            span.set_data("kwargs", request.kwargs)
            result = call(method, *request.args, **request.kwargs)

        return SuccessResponse(result=result, id=request.id)

    except Exception as exc:
        traceback.print_exc()
        sys.stdout.flush()
        sys.stderr.flush()
        return ExceptionResponse(exc, id=request.id, debug=True)
예제 #5
0
def test_create_requests_batch():
    requests = create_requests(
        [{"jsonrpc": "2.0", "method": "ping"}, {"jsonrpc": "2.0", "method": "ping"}],
    )
    assert iter(requests)
예제 #6
0
def test_create_requests():
    requests = create_requests({"jsonrpc": "2.0", "method": "ping"})
    assert isinstance(requests, Request)