GitHub

entity_proxy_server

API reference for entity_proxy_server.

Serves the RPCs and HTTP endpoints entity_proxy derives (Effect's EntityProxyServer): each call goes to the entity through its sharding client.

Functions

layer_rpc_handlers

layer_rpc_handlers(entity) -> Layer[HandlersFor[Any], Never, Sharding]

The handlers of to_rpc_group(entity): "<type>.<Tag>" calls the entity and "<type>.<Tag>Discard" sends without waiting for the reply.

ParameterTypeDescription
entitytype[Entity]

Returns Layer[HandlersFor[Any], Never, Sharding]

def layer_rpc_handlers(entity: type[Entity]) -> Layer[HandlersFor[Any], Never, Sharding]:
    """The handlers of ``to_rpc_group(entity)``: ``"<type>.<Tag>"`` calls
    the entity and ``"<type>.<Tag>Discard"`` sends without waiting for the
    reply."""
    group = to_rpc_group(entity)

    @gen
    def make():
        client = yield from entity.client()
        by_tag: dict[str, Any] = {}
        for parent in entity.rpcs.values():
            by_tag[f"{entity.type}.{parent.tag}"] = _call(client, parent, discard=False)
            by_tag[f"{entity.type}.{parent.tag}Discard"] = _call(client, parent, discard=True)
        return Handlers[Any, Never](group, by_tag)

    return group.to_layer_effect(make)

layer_http_api

layer_http_api[G: HttpApiGroup[Any, Any]](api, group, entity) -> Layer[G, Never, Sharding]

The handlers of group in api, a group made by entity_proxy.to_http_api_group(..., entity) (Effect's EntityProxyServer.layerHttpApi) or declared with entity_proxy.http_endpoints, which may leave out RPCs of the entity to keep them off HTTP. Defects are logged.

ParameterTypeDescription
apiHttpApi[G]
grouptype[G]
entitytype[Entity]

Returns Layer[G, Never, Sharding]

def layer_http_api[G: HttpApiGroup[Any, Any]](
    api: HttpApi[G], group: type[G], entity: type[Entity]
) -> Layer[G, Never, Sharding]:
    """The handlers of ``group`` in ``api``, a group made by
    ``entity_proxy.to_http_api_group(..., entity)`` (Effect's
    ``EntityProxyServer.layerHttpApi``) or declared with
    ``entity_proxy.http_endpoints``, which may leave out RPCs of the entity
    to keep them off HTTP. Defects are logged."""

    def build(handlers: GroupHandlers[Any]) -> Effect[Any, Never, Sharding]:
        @gen
        def make():
            client = yield from entity.client()
            built: Any = handlers
            for parent in entity.rpcs.values():
                for discard in (False, True):
                    identifier = f"{parent.tag}Discard" if discard else parent.tag
                    if identifier in group.endpoints:
                        built = built.handle(group.endpoints[identifier])(
                            _endpoint_handler(client, entity, parent, discard=discard)
                        )
            return built

        return make

    return builder.group(api, group, build)