jsonrpc.JsonRpcContext¶
A per-call context object. Middlewares receive it as their only argument; any code running inside a JSON-RPC call can obtain it via get_jsonrpc_context().
Attributes¶
entrypoint: Entrypoint— the entrypoint handling this call.raw_request: Any— the parsed JSON-RPC request dict (not yet validated againstJsonRpcRequest).raw_response: dict | None— the response dict that will be sent back. Assigning a new value runs it through theon_raw_responsenormaliser: theidis taken fromraw_request, exceptions are converted into an error response, unhandled exceptions are turned intoInternalError. Mutating the existing dict in-place bypasses the normaliser.exception: Exception | None— the exception raised by the method, if any.is_unhandled_exception: bool—Trueif the exception was not aBaseError/HTTPException, i.e. it was converted toInternalError.http_request: starlette.requests.Request— the wrapping HTTP request.http_response: starlette.responses.Response— the HTTP response that will be returned.background_tasks: fastapi.BackgroundTasks— the background task queue.method_route: MethodRoute | None— the matched method route (orNoneif the request failed before dispatch).request: JsonRpcRequest— a validatedJsonRpcRequest(cached). RaisesInvalidRequestif validation fails.
Helpers¶
from fastapi_jsonrpc import (
get_jsonrpc_context,
get_jsonrpc_request_id,
get_jsonrpc_method,
)
ctx = get_jsonrpc_context() # the current JsonRpcContext
rid = get_jsonrpc_request_id() # value of "id" from the raw request
name = get_jsonrpc_method() # value of "method" from the raw request
All three read from a contextvars.ContextVar set for the duration of the call, so they work from anywhere in your async call graph.
Middleware signature¶
Implementations are typically written with @asynccontextmanager: