Skip to content

Dispatcher API

The dispatcher layer lives in the jrpc_core.dispatcher module.

python
from jrpc_core.dispatcher import (
    JsonRpcDispatcher,
    JsonRpcMethodWrapper,
    JsonRpcHandlerCollection,
    JsonRpcResponseCtorWrapper,
)

JsonRpcMethodWrapper

python
class JsonRpcMethodWrapper

Wraps a callable as a JSON-RPC method with optional validation and conversion.

Constructor

python
JsonRpcMethodWrapper(
    *,
    name: str,
    method: Callable[..., Any],
    validator: Callable[..., Option[JsonRpcError] | bool] | None = None,
    converter: Callable[..., Option[Any] | Result[Any, Exception | JsonRpcError] | Any] | None = None,
)
ParameterTypeDefaultDescription
namestr(required)The JSON-RPC method name.
methodCallable[..., Any](required)The callable to invoke when this method is dispatched.
validatorCallable[..., Option[JsonRpcError] | bool] | NoneNoneAn optional callable that receives the parsed params and returns a rejection signal.
converterCallable[..., Option[Any] | Result[Any, Exception | JsonRpcError] | Any] | NoneNoneAn optional callable that transforms the parsed params before the method is invoked.

Validator Protocol

The validator receives the parsed params and may return:

Return valueBehaviour
Some(JsonRpcError)Rejects with that error
Some(Exception)Rejects with that error wrapped in InvalidParams
FalseRejects with a generic InvalidParams error
Exception or JsonRpcErrorRejects with that error directly
True, None, or any other truthy valueAccepts — continues to conversion or method invocation

Converter Protocol

The converter receives the raw params payload and may return:

Return valueBehaviour
Some(value)Uses value as the method argument
Nothing()Rejects with ConversionError
Ok(value)Uses value as the method argument
Err(reason)Rejects with ConversionError, attaching reason to data
Any other valueUses the value directly as the method argument
Raises ExceptionRejects with ConversionError, attaching the exception to data

Attributes

AttributeTypeDescription
namestrThe JSON-RPC method name this wrapper is registered under.

Methods

__hash__() -> int

Return a hash based on the method name. Two wrappers with the same name have the same hash.

__eq__(other) -> bool

Compare two wrappers by method name.

__call__(params: Option[Any]) -> Result[Any, JsonRpcError]

Execute the wrapped method with optional parameters. Validation runs first, then conversion; if either step rejects the parameters the call short-circuits with an Err.

ParameterTypeDescription
paramsOption[Any]An Option containing the method parameters. Some means parameters were provided; Nothing means none.

Returns: Ok(result) on success, or Err(JsonRpcError) on failure.

python
>>> from pyfplib import Some, Nothing, Result
>>> wrapper = JsonRpcMethodWrapper(name="add", method=lambda a: a[0] + a[1])
>>> wrapper(Some([1, 2]))
Result.ok(3)
>>> wrapper(Nothing())
Result.ok(...)  # calls method with no args

JsonRpcHandlerCollection

python
class JsonRpcHandlerCollection

A registry of JsonRpcMethodWrapper instances keyed by method name.

Constructor

python
JsonRpcHandlerCollection()

Initialise an empty handler collection.

Methods

add(method: JsonRpcMethodWrapper) -> bool

Register a method wrapper. If a method with the same name already exists, the call is a no-op.

ParameterTypeDescription
methodJsonRpcMethodWrapperThe wrapper to register.

Returns: True if the method was newly registered, False if it already existed.

python
>>> collection = JsonRpcHandlerCollection()
>>> wrapper = JsonRpcMethodWrapper(name="add", method=lambda a: a)
>>> collection.add(wrapper)
True
>>> collection.add(wrapper)  # duplicate
False

try_get(name: str) -> Option[JsonRpcMethodWrapper]

Look up a method by name.

ParameterTypeDescription
namestrThe JSON-RPC method name.

Returns: Some(wrapper) if found, otherwise Nothing.

exists(name: str) -> bool

Check whether a method is registered.

ParameterTypeDescription
namestrThe JSON-RPC method name.

Returns: True if a wrapper with that name exists.

remove_by_name(name: str) -> bool

Remove a method by name.

ParameterTypeDescription
namestrThe JSON-RPC method name to remove.

Returns: True if the method existed and was removed, False otherwise.

remove(method: str | JsonRpcMethodWrapper) -> bool

Remove a method by name or wrapper instance.

ParameterTypeDescription
methodstr | JsonRpcMethodWrapperEither a method name string or a JsonRpcMethodWrapper.

Returns: True if the method existed and was removed, False otherwise.

python
>>> collection = JsonRpcHandlerCollection()
>>> wrapper = JsonRpcMethodWrapper(name="add", method=lambda a: a)
>>> collection.add(wrapper)
True
>>> collection.remove("add")
True
>>> collection.remove("add")
False

JsonRpcDispatcher

python
class JsonRpcDispatcher

Routes incoming JSON-RPC messages to registered handlers. Maintains separate registries for requests (which expect a response) and notifications (fire-and-forget).

Constructor

python
JsonRpcDispatcher(
    response_handler: Callable[[JsonRpcResponse], None] | None = None,
)
ParameterTypeDefaultDescription
response_handlerCallable[[JsonRpcResponse], None] | NoneNoneOptional callback invoked when a JsonRpcResponse is dispatched directly.

Class Attributes

AttributeTypeDescription
ERROR_CASEJsonRpcResponseCtorWrapper.StateOutcome selector for error responses.
RESULT_CASEJsonRpcResponseCtorWrapper.StateOutcome selector for successful results.
BOTH_CASESJsonRpcResponseCtorWrapper._WhenOutcome selector matching both outcomes.

Attributes

AttributeTypeDescription
request_handler_registryJsonRpcHandlerCollectionRegistry for request handlers.
notification_handler_registryJsonRpcHandlerCollectionRegistry for notification handlers.

Methods

emplace_request_handler(*, name, method, validator=None, converter=None) -> bool

Register a request handler in one call. Convenience for request_handler_registry.add(JsonRpcMethodWrapper(...)).

ParameterTypeDefaultDescription
namestr(required)The JSON-RPC method name.
methodCallable[..., Any](required)The callable to invoke when dispatched.
validatorCallable[..., Option[JsonRpcError] | bool] | NoneNoneOptional parameter validator.
converterCallable[..., Option[Any] | Result[Any, Exception | JsonRpcError] | Any] | NoneNoneOptional parameter converter.

Returns: True if newly registered, False if the name already exists.

emplace_notification_handler(*, name, method, validator=None, converter=None) -> bool

Register a notification handler in one call. Convenience for notification_handler_registry.add(JsonRpcMethodWrapper(...)).

ParameterTypeDefaultDescription
namestr(required)The JSON-RPC method name.
methodCallable[..., Any](required)The callable to invoke when dispatched.
validatorCallable[..., Option[JsonRpcError] | bool] | NoneNoneOptional parameter validator.
converterCallable[..., Option[Any] | Result[Any, Exception | JsonRpcError] | Any] | NoneNoneOptional parameter converter.

Returns: True if newly registered, False if the name already exists.

emplace_custom_response_ctor(method, ctor, *states)

Register a custom response constructor for method.

ParameterTypeDescription
methodstrThe JSON-RPC method name the constructor applies to.
ctorCallable[..., JsonRpcResponse]Callable building a JsonRpcResponse.
*statesJsonRpcResponseCtorWrapper.StateOptional state members restricting when ctor is used.

add_custom_response_ctor(ctor: JsonRpcResponseCtorWrapper)

Register a pre-built custom response constructor. Replaces any constructor previously registered for the same method.

ParameterTypeDescription
ctorJsonRpcResponseCtorWrapperThe wrapper binding a constructor to a method name.

__call__(data) -> Option[Result[JsonRpcResponse, JsonRpcError]]

Dispatch a JSON-RPC message.

ParameterTypeDescription
datastr | JsonRpcRequest | JsonRpcNotification | JsonRpcResponse | Result[...]A JSON string, JsonRpcRequest, JsonRpcNotification, JsonRpcResponse, or Result.

Returns:

InputHandler foundHandler not found
str (parse ok)Delegates to request/notification handling
str (parse fail)Some(Err(ParseError))
JsonRpcRequestSome(Ok(response))Some(Err(MethodNotFound)) via response
JsonRpcNotificationNothing (success)Some(Err(MethodNotFound))
Unknown typeSome(Err(InternalError))
python
>>> from pyfplib import Some, Ok
>>> dispatcher = JsonRpcDispatcher()
>>> dispatcher.request_handler_registry.add(
...     JsonRpcMethodWrapper(name="add", method=lambda a: a[0] + a[1])
... )
True
>>> result = dispatcher(JsonRpcRequest(method="add", params=[1, 2], id=1))
>>> result.unwrap().unwrap().result
3

try_parse(data: str) -> Result[JsonRpcResponse | JsonRpcNotification | JsonRpcRequest, JsonRpcError] (classmethod)

Attempt to parse a JSON string into a response, request, or notification. First tries JsonRpcResponse; on failure falls back to JsonRpcNotification; on failure falls back to JsonRpcRequest.

ParameterTypeDescription
datastrA JSON-encoded string.

Returns: Ok(response | notification | request) on success, or Err(JsonRpcError) on parse failure.


JsonRpcResponseCtorWrapper

python
class JsonRpcResponseCtorWrapper

Binds a custom JsonRpcResponse constructor to a method name. The wrapper records when the constructor applies — successful results, errors, or both — so the dispatcher can pick the right response type per outcome.

Constructor

python
JsonRpcResponseCtorWrapper(
    method: str,
    ctor: Callable[..., JsonRpcResponse],
    *states: JsonRpcResponseCtorWrapper.State,
)
ParameterTypeDefaultDescription
methodstr(required)The JSON-RPC method name this constructor applies to.
ctorCallable[..., JsonRpcResponse](required)Callable receiving keyword arguments (id, result or error, and jsonrpc) and returning a JsonRpcResponse.
*statesStateBoth outcomesOptional State members limiting when ctor is used.

Inner Class: State

python
class State(Enum)

Outcome selector controlling when a constructor is applied.

MemberValueDescription
Result1The constructor handles successful results.
Error2The constructor handles error responses.

Attributes

AttributeTypeDescription
methodstrThe JSON-RPC method name this constructor is bound to.
when_WhenThe outcome selector for this constructor.