OpenRPC documents for RPC groups (Effect's OpenRpc).
from_rpc_group(Users, info={"title": "Users", "version": "1.0.0"}) describes
each RPC of the group as an OpenRPC 1.3.2 method, with the JSON Schemas (Draft 7,
their shared definitions under components.schemas) of what the JSON-RPC
serializer sends: the encoded payload as named (by-name) or positional
(by-position) params, the result, the errors, and for a streaming RPC the
x-effect-stream extension describing its chunks. The group's and RPC's
Summary, Description, Tags, Examples, Deprecated,
ExternalDocs and Errors annotations fill in the method (the RPC's win);
without Errors, they are read from the error schemas' literal code and
message fields.
An RPC whose payload is None (the default) takes no params.
Values#
| Attribute | Type | Description |
|---|---|---|
OpenRPCMethod | type[OpenRPCMethod] |
Types#
| Type | Definition | Description |
|---|---|---|
ParamStructure | type ParamStructure = Literal['by-name', 'by-position', 'either'] |
Classes#
Functions#
annotations#
annotations(*, summary=None, description=None, tags=None, examples=None, deprecated=None, external_docs=None, errors=None) -> Context[Any]The given OpenRPC annotations as one context, for annotate_merge.
| Parameter | Type | Description |
|---|---|---|
summary | str | None = None | |
description | str | None = None | |
tags | Sequence[OpenRPCTag] | None = None | |
examples | Sequence[OpenRPCExamplePairing] | None = None | |
deprecated | bool | None = None | |
external_docs | OpenRPCExternalDocs | None = None | |
errors | Sequence[OpenRPCError] | None = None |
Returns Context[Any]
def annotations(
*,
summary: str | None = None,
description: str | None = None,
tags: Sequence[OpenRPCTag] | None = None,
examples: Sequence[OpenRPCExamplePairing] | None = None,
deprecated: bool | None = None,
external_docs: OpenRPCExternalDocs | None = None,
errors: Sequence[OpenRPCError] | None = None,
) -> Context[Any]:
"""The given OpenRPC annotations as one context, for ``annotate_merge``."""
given = {
"summary": summary,
"description": description,
"tags": tags,
"examples": examples,
"deprecated": deprecated,
"externalDocs": external_docs,
"errors": errors,
}
context: Context[Any] = Context.empty()
for key, tag in _ANNOTATIONS:
value = given[key]
if value is not None:
context = context.add(tag, value)
return contextfrom_rpc_group#
from_rpc_group(group, *, info, servers=None) -> OpenRPCSpecThe OpenRPC document of group's RPCs. Raises ValueError naming the
method for a payload that is neither an object nor an array of params.
| Parameter | Type | Description |
|---|---|---|
group | type[RpcGroup[Any]] | |
info | OpenRPCInfo | |
servers | Sequence[OpenRPCServer] | None = None |
Returns OpenRPCSpec
def from_rpc_group(
group: type[RpcGroup[Any]],
*,
info: OpenRPCInfo,
servers: Sequence[OpenRPCServer] | None = None,
) -> OpenRPCSpec:
"""The OpenRPC document of ``group``'s RPCs. Raises ``ValueError`` naming the
method for a payload that is neither an object nor an array of params."""
methods: list[OpenRPCMethod] = []
components: dict[str, JsonSchema] = {}
spec: OpenRPCSpec = {
"openrpc": "1.3.2",
"info": info,
"methods": methods,
"components": {"schemas": components},
}
if servers is not None:
spec = {**spec, "servers": servers}
spec["methods"] = methods
rpcs = list(group.rpcs.values())
if not rpcs:
return spec
nodes: list[Top] = []
def register(schema: Top) -> int:
nodes.append(schema)
return len(nodes) - 1
contracts: list[_Contract] = []
for rpc in rpcs:
def contract(rpc: AnyRpc = rpc) -> _Contract:
exit_ = exit_schema(rpc)
streams = get_stream_schemas(rpc.success_schema)
return _Contract(
rpc,
register(rpc.payload_schema),
register(exit_.value),
exit_.error,
None
if streams is None
else _Stream(
register(streams.success),
register(S.NonEmptyArray(streams.success)),
register(exit_.error),
register(S.Cause(exit_.error, exit_.defect)),
),
)
contracts.append(_with_method(rpc.tag, contract))
json_nodes = to_codec_json_asts([node.__codec__().ast for node in nodes])
first, *rest = (S.Bottom[Any, Any](node) for node in json_nodes)
multi = R.to_representations((first, *rest))
representations = list(multi.representations)
references = multi.references
for item in contracts:
def project(item: _Contract = item) -> None:
payload = _resolve(representations[item.payload], references)
if isinstance(payload, (R.Null, R.Void, R.Undefined)):
item.has_payload = False
elif isinstance(payload, R.Objects):
item.param_structure = "by-name"
representations.append(R.Objects(property_signatures=payload.property_signatures))
item.parameters = len(representations) - 1
elif isinstance(payload, R.Arrays):
item.param_structure = "by-position"
optional = False
for element in payload.elements:
if optional and not element.is_optional:
raise ValueError(
"optional positional parameters must follow required parameters"
)
optional = optional or element.is_optional
representations.append(R.Arrays(elements=payload.elements))
item.parameters = len(representations) - 1
else:
item.param_structure = _parameter_structure(payload, references, set())
_with_method(item.rpc.tag, project)
document = _compile(R.MultiDocument((representations[0], *representations[1:]), references))
components.update(document.definitions)
schemas = cast("list[JsonSchema]", document.schema["anyOf"])
group_annotations = group.annotations
methods.extend(
_with_method(
item.rpc.tag,
lambda item=item: _method(
item, schemas, representations, components, group_annotations
),
)
for item in contracts
)
return spec