What an entity answers (Effect's Reply): WithExit, the end of a
request with its exit, or Chunk, a part of a streamed reply.
reply_schema(rpc, codec_for) reads and writes the replies of rpc
(Effect's Reply.Reply); encoded, they are Effect's JSON shapes
WithExitEncoded and ChunkEncoded.
Re-exports#
Types#
| Type | Definition | Description |
|---|---|---|
Reply | type Reply[A = Any, E = Any] = WithExit[A, E] | Chunk[A] | |
Encoded | type Encoded = WithExitEncoded | ChunkEncoded | |
OnWire | type OnWire = Encoded | bytes | A reply as a serialization's |
Classes#
Functions#
is_reply#
is_reply(u) -> TypeIs[Reply]| Parameter | Type | Description |
|---|---|---|
u | object |
Returns TypeIs[Reply]
def is_reply(u: object) -> TypeIs[Reply]:
return isinstance(u, (WithExit, Chunk))with_exit_schema#
with_exit_schema(exit) -> S.Bottom[WithExit, Mapping[str, object]]| Parameter | Type | Description |
|---|---|---|
exit | S.Top |
Returns S.Bottom[WithExit, Mapping[str, object]]
def with_exit_schema(exit: S.Top) -> S.Bottom[WithExit, Mapping[str, object]]:
return wire_codec(
WithExit,
[
Const("_tag", "WithExit"),
key("requestId", "request_id", SnowflakeFromBigInt),
key("id", "id", SnowflakeFromBigInt),
key("exit", "exit", exit),
],
identifier="Reply.WithExit",
)chunk_schema#
chunk_schema(success) -> S.Bottom[Chunk, Mapping[str, object]]| Parameter | Type | Description |
|---|---|---|
success | S.Top |
Returns S.Bottom[Chunk, Mapping[str, object]]
def chunk_schema(success: S.Top) -> S.Bottom[Chunk, Mapping[str, object]]:
return wire_codec(
Chunk,
[
Const("_tag", "Chunk"),
key("requestId", "request_id", SnowflakeFromBigInt),
key("id", "id", SnowflakeFromBigInt),
key("sequence", "sequence", S.Int),
key("values", "values", S.NonEmptyArray(success)),
],
identifier="Reply.Chunk",
)reply_schema#
reply_schema(rpc, codec_for) -> S.Codec[Reply, Any, Any, Any]The replies of rpc: WithExit with its exit, and Chunk of
its stream items when it streams (Effect's Reply.Reply).
| Parameter | Type | Description |
|---|---|---|
rpc | AnyRpc | |
codec_for | CodecFor |
Returns S.Codec[Reply, Any, Any, Any]
def reply_schema(rpc: AnyRpc, codec_for: CodecFor) -> S.Codec[Reply, Any, Any, Any]:
"""The replies of ``rpc``: ``WithExit`` with its exit, and ``Chunk`` of
its stream items when it streams (Effect's ``Reply.Reply``)."""
cached = _schemas.get((id(codec_for), id(rpc)))
if cached is not None and cached[0] is codec_for and cached[1] is rpc:
return cached[2]
streams = get_stream_schemas(rpc.success_schema)
members: list[S.Top] = [with_exit_schema(exit_schema(rpc))]
if streams is not None:
members.append(chunk_schema(streams.success))
schema = codec_for(S.Union(*members))
_schemas[(id(codec_for), id(rpc))] = (codec_for, rpc, schema)
return schemaserialize#
serialize(self, codec_for) -> Effect[OnWire, MalformedMessage]The reply encoded by its RPC's schema.
| Parameter | Type | Description |
|---|---|---|
self | ReplyWithContext | |
codec_for | CodecFor |
Returns Effect[OnWire, MalformedMessage]
def serialize(self: ReplyWithContext, codec_for: CodecFor) -> Effect[OnWire, MalformedMessage]:
"""The reply encoded by its RPC's schema."""
return _encode(reply_schema(self.rpc, codec_for), self.reply, self.context)serialize_or_defect#
serialize_or_defect(self, codec_for) -> Effect[OnWire]The reply encoded, or a defect ending its request when it cannot be.
| Parameter | Type | Description |
|---|---|---|
self | ReplyWithContext | |
codec_for | CodecFor |
Returns Effect[OnWire]
def serialize_or_defect(self: ReplyWithContext, codec_for: CodecFor) -> Effect[OnWire]:
"""The reply encoded, or a defect ending its request when it cannot be."""
def defect(error: MalformedMessage) -> Effect[OnWire]:
fallback = ReplyWithContext.from_defect(
id=self.reply.id, request_id=self.reply.request_id, defect=error
)
return serialize(fallback, codec_for).or_die()
return serialize(self, codec_for).catch_all(defect)decode#
decode(rpc, codec_for, encoded, context) -> Effect[Reply, S.SchemaError]A reply of rpc from its encoded form.
| Parameter | Type | Description |
|---|---|---|
rpc | AnyRpc | |
codec_for | CodecFor | |
encoded | object | |
context | Context[Any] |
Returns Effect[Reply, S.SchemaError]
def decode(
rpc: AnyRpc, codec_for: CodecFor, encoded: object, context: Context[Any]
) -> Effect[Reply, S.SchemaError]:
"""A reply of ``rpc`` from its encoded form."""
return S.decode_unknown_effect(reply_schema(rpc, codec_for))(encoded).pipe(
provide_context(context)
)serialize_last_received#
serialize_last_received(rpc, last_received, context, codec_for) -> Effect[Encoded | None, MalformedMessage]The last reply a sender got, encoded.
| Parameter | Type | Description |
|---|---|---|
rpc | AnyRpc | |
last_received | Reply | None | |
context | Context[Any] | |
codec_for | CodecFor |
Returns Effect[Encoded | None, MalformedMessage]
def serialize_last_received(
rpc: AnyRpc, last_received: Reply | None, context: Context[Any], codec_for: CodecFor
) -> Effect[Encoded | None, MalformedMessage]:
"""The last reply a sender got, encoded."""
if last_received is None:
return succeed(None)
return _encode(reply_schema(rpc, codec_for), last_received, context)encoded_request_id#
encoded_request_id(encoded) -> str| Parameter | Type | Description |
|---|---|---|
encoded | Encoded |
Returns str
def encoded_request_id(encoded: Encoded) -> str:
return encoded["requestId"]