GitHub

API reference for group.

Groups of RPCs (Effect's RpcGroup) and their handlers.

A group is a class whose attributes are RPCs::

class Users(RpcGroup):
    get_user = rpc.make("GetUser", payload=S.Struct(GetUser), success=User)

Its handlers are built one RPC at a time, then made into a layer the server needs::

UsersLive = Users.to_layer(
    Users.handlers()
    .handle(Users.get_user)(lambda request: find_user(request.payload["id"]))
)

A client of the group is an instance of it (RpcClient.make(Users)), on which each RPC attribute is the method calling it.

Types

TypeDefinitionDescription
HandlerFntype HandlerFn = Callable[[RpcRequest[Any]], Any]

Classes

Functions

to_layer_handler

to_layer_handler[P, S, E, R2](rpc, handler) -> Layer[HandlerOf[Rpc[P, S, E]], Never, R2]

The layer of one RPC's handler (Effect's toLayerHandler); it runs with the services of the layer's context.

ParameterTypeDescription
rpcRpc[P, S, E, Never]
handlerCallable[[RpcRequest[P]], Effect[S | Deferred[S, E], E, R2]]

Returns Layer[HandlerOf[Rpc[P, S, E]], Never, R2]

def to_layer_handler[P, S, E, R2](
    rpc: Rpc[P, S, E, Never],
    handler: Callable[[RpcRequest[P]], Effect[S | Deferred[S, E], E, R2]],
) -> Layer[HandlerOf[Rpc[P, S, E]], Never, R2]:
    """The layer of one RPC's handler (Effect's ``toLayerHandler``); it
    runs with the services of the layer's context."""

    def entry(services: Context[Any]) -> Context[Any]:
        kept = Context({t: s for t, s in services.map_unsafe.items() if t is not Scope})
        return Context({handler_tag(rpc): HandlerEntry(rpc.tag, handler, kept)})

    return layer_effect_context(context().map(entry))

access_handler

access_handler[P, S, E](rpc) -> Effect[Callable[[RpcRequest[P]], Effect[S, E]], Never, HandlerOf[Rpc[P, S, E]]]

The handler of rpc from the context, running with its layer's services (Effect's accessHandler).

ParameterTypeDescription
rpcRpc[P, S, E, Never]

Returns Effect[Callable[[RpcRequest[P]], Effect[S, E]], Never, HandlerOf[Rpc[P, S, E]]]

def access_handler[P, S, E](
    rpc: Rpc[P, S, E, Never],
) -> Effect[Callable[[RpcRequest[P]], Effect[S, E]], Never, HandlerOf[Rpc[P, S, E]]]:
    """The handler of ``rpc`` from the context, running with its layer's
    services (Effect's ``accessHandler``)."""

    def found(services: Context[Any]) -> Callable[[RpcRequest[P]], Effect[S, E]]:
        entry: HandlerEntry = services.get_unsafe(handler_tag(rpc))

        def run(request: RpcRequest[P]) -> Effect[S, E]:
            return cast("Effect[S, E]", entry.handler(request)).pipe(provide_context(entry.context))

        return run

    return cast(
        "Effect[Callable[[RpcRequest[P]], Effect[S, E]], Never, HandlerOf[Rpc[P, S, E]]]",
        context().map(found),
    )