Module resonitepy.signalr
Expand source code
import logging
from typing import Annotated, Any, Literal
from pydantic import BaseModel, Field, TypeAdapter, ValidationError
from resonitepy import exceptions as resonite_exceptions
RECORD_SEPARATOR = "\x1e"
class HandshakeRequest(BaseModel):
protocol: Literal["json"] = "json"
version: Literal[1] = 1
class HandshakeResponse(BaseModel):
error: str | None = None
class Invocation(BaseModel):
type: Literal[1] = 1
target: str
arguments: list = []
invocationId: str | None = None
class Completion(BaseModel):
type: Literal[3] = 3
invocationId: str
result: Any = None
error: str | None = None
class Ping(BaseModel):
type: Literal[6] = 6
class Close(BaseModel):
type: Literal[7] = 7
error: str | None = None
allowReconnect: bool = False
HubMessage = Annotated[
Invocation
| Completion
| Ping
| Close,
Field(discriminator="type")
]
_HUB_MESSAGE_ADAPTER = TypeAdapter(HubMessage)
def split_messages(ws_frame: str) -> list[HubMessage]:
messages = []
parts = ws_frame.split(RECORD_SEPARATOR)
for part in parts:
if part.strip():
try:
data = _HUB_MESSAGE_ADAPTER.validate_json(part)
messages.append(data)
except ValidationError:
logging.warning(f"Non-JSON message: {part!r}")
return messages
def parse_handshake_response(ws_frame: str) -> list[HubMessage]:
messages = [p for p in ws_frame.split(RECORD_SEPARATOR) if p.strip()]
if not messages:
raise resonite_exceptions.ResoniteHubException(f"No handshake received: {ws_frame!r}")
try:
handshake_response = HandshakeResponse.model_validate_json(messages[0])
except ValidationError as e:
raise resonite_exceptions.ResoniteHubException(f"Invalid handshake failed: {messages[0]!r}") from e
if handshake_response.error is not None:
raise resonite_exceptions.ResoniteHubException(f"Hub did not accept the handshake: {handshake_response.error}")
return split_messages(RECORD_SEPARATOR.join(messages[1:]))
Functions
def parse_handshake_response(ws_frame: str) ‑> list[Invocation | Completion | Ping | Close]-
Expand source code
def parse_handshake_response(ws_frame: str) -> list[HubMessage]: messages = [p for p in ws_frame.split(RECORD_SEPARATOR) if p.strip()] if not messages: raise resonite_exceptions.ResoniteHubException(f"No handshake received: {ws_frame!r}") try: handshake_response = HandshakeResponse.model_validate_json(messages[0]) except ValidationError as e: raise resonite_exceptions.ResoniteHubException(f"Invalid handshake failed: {messages[0]!r}") from e if handshake_response.error is not None: raise resonite_exceptions.ResoniteHubException(f"Hub did not accept the handshake: {handshake_response.error}") return split_messages(RECORD_SEPARATOR.join(messages[1:])) def split_messages(ws_frame: str) ‑> list[Invocation | Completion | Ping | Close]-
Expand source code
def split_messages(ws_frame: str) -> list[HubMessage]: messages = [] parts = ws_frame.split(RECORD_SEPARATOR) for part in parts: if part.strip(): try: data = _HUB_MESSAGE_ADAPTER.validate_json(part) messages.append(data) except ValidationError: logging.warning(f"Non-JSON message: {part!r}") return messages
Classes
class Close (**data: Any)-
Usage Documentation
A base class for creating Pydantic models.
- Attributes
- -----=
__class_vars__- The names of the class variables defined on the model.
__private_attributes__- Metadata about the private attributes of the model.
__signature__- The synthesized
__init__[Signature][inspect.Signature] of the model. __pydantic_complete__- Whether model building is completed, or if there are still undefined fields.
__pydantic_core_schema__- The core schema of the model.
__pydantic_custom_init__- Whether the model has a custom
__init__function. __pydantic_decorators__- Metadata containing the decorators defined on the model.
This replaces
Model.__validators__andModel.__root_validators__from Pydantic V1. __pydantic_generic_metadata__- A dictionary containing metadata about generic Pydantic models.
The
originandargsitems map to the [__origin__][genericalias.origin] and [__args__][genericalias.args] attributes of [generic aliases][types-genericalias], and theparameteritem maps to the__parameter__attribute of generic classes. __pydantic_parent_namespace__- Parent namespace of the model, used for automatic rebuilding of models.
__pydantic_post_init__- The name of the post-init method for the model, if defined.
__pydantic_root_model__- Whether the model is a [
RootModel][pydantic.root_model.RootModel]. __pydantic_serializer__- The
pydantic-coreSchemaSerializerused to dump instances of the model. __pydantic_validator__- The
pydantic-coreSchemaValidatorused to validate instances of the model. __pydantic_fields__- A dictionary of field names and their corresponding [
FieldInfo][pydantic.fields.FieldInfo] objects. __pydantic_computed_fields__- A dictionary of computed field names and their corresponding [
ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects. __pydantic_extra__- A dictionary containing extra values, if [
extra][pydantic.config.ConfigDict.extra] is set to'allow'. __pydantic_fields_set__- The names of fields explicitly set during instantiation.
__pydantic_private__- Values of private attributes set on the model instance.
Create a new model by parsing and validating input data from keyword arguments.
Raises [
ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.selfis explicitly positional-only to allowselfas a field name.Expand source code
class Close(BaseModel): type: Literal[7] = 7 error: str | None = None allowReconnect: bool = FalseAncestors
- pydantic.main.BaseModel
Class variables
var allowReconnect : boolvar error : Optional[str]var model_configvar type : Literal[7]
class Completion (**data: Any)-
Usage Documentation
A base class for creating Pydantic models.
- Attributes
- -----=
__class_vars__- The names of the class variables defined on the model.
__private_attributes__- Metadata about the private attributes of the model.
__signature__- The synthesized
__init__[Signature][inspect.Signature] of the model. __pydantic_complete__- Whether model building is completed, or if there are still undefined fields.
__pydantic_core_schema__- The core schema of the model.
__pydantic_custom_init__- Whether the model has a custom
__init__function. __pydantic_decorators__- Metadata containing the decorators defined on the model.
This replaces
Model.__validators__andModel.__root_validators__from Pydantic V1. __pydantic_generic_metadata__- A dictionary containing metadata about generic Pydantic models.
The
originandargsitems map to the [__origin__][genericalias.origin] and [__args__][genericalias.args] attributes of [generic aliases][types-genericalias], and theparameteritem maps to the__parameter__attribute of generic classes. __pydantic_parent_namespace__- Parent namespace of the model, used for automatic rebuilding of models.
__pydantic_post_init__- The name of the post-init method for the model, if defined.
__pydantic_root_model__- Whether the model is a [
RootModel][pydantic.root_model.RootModel]. __pydantic_serializer__- The
pydantic-coreSchemaSerializerused to dump instances of the model. __pydantic_validator__- The
pydantic-coreSchemaValidatorused to validate instances of the model. __pydantic_fields__- A dictionary of field names and their corresponding [
FieldInfo][pydantic.fields.FieldInfo] objects. __pydantic_computed_fields__- A dictionary of computed field names and their corresponding [
ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects. __pydantic_extra__- A dictionary containing extra values, if [
extra][pydantic.config.ConfigDict.extra] is set to'allow'. __pydantic_fields_set__- The names of fields explicitly set during instantiation.
__pydantic_private__- Values of private attributes set on the model instance.
Create a new model by parsing and validating input data from keyword arguments.
Raises [
ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.selfis explicitly positional-only to allowselfas a field name.Expand source code
class Completion(BaseModel): type: Literal[3] = 3 invocationId: str result: Any = None error: str | None = NoneAncestors
- pydantic.main.BaseModel
Class variables
var error : Optional[str]var invocationId : strvar model_configvar result : Anyvar type : Literal[3]
class HandshakeRequest (**data: Any)-
Usage Documentation
A base class for creating Pydantic models.
- Attributes
- -----=
__class_vars__- The names of the class variables defined on the model.
__private_attributes__- Metadata about the private attributes of the model.
__signature__- The synthesized
__init__[Signature][inspect.Signature] of the model. __pydantic_complete__- Whether model building is completed, or if there are still undefined fields.
__pydantic_core_schema__- The core schema of the model.
__pydantic_custom_init__- Whether the model has a custom
__init__function. __pydantic_decorators__- Metadata containing the decorators defined on the model.
This replaces
Model.__validators__andModel.__root_validators__from Pydantic V1. __pydantic_generic_metadata__- A dictionary containing metadata about generic Pydantic models.
The
originandargsitems map to the [__origin__][genericalias.origin] and [__args__][genericalias.args] attributes of [generic aliases][types-genericalias], and theparameteritem maps to the__parameter__attribute of generic classes. __pydantic_parent_namespace__- Parent namespace of the model, used for automatic rebuilding of models.
__pydantic_post_init__- The name of the post-init method for the model, if defined.
__pydantic_root_model__- Whether the model is a [
RootModel][pydantic.root_model.RootModel]. __pydantic_serializer__- The
pydantic-coreSchemaSerializerused to dump instances of the model. __pydantic_validator__- The
pydantic-coreSchemaValidatorused to validate instances of the model. __pydantic_fields__- A dictionary of field names and their corresponding [
FieldInfo][pydantic.fields.FieldInfo] objects. __pydantic_computed_fields__- A dictionary of computed field names and their corresponding [
ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects. __pydantic_extra__- A dictionary containing extra values, if [
extra][pydantic.config.ConfigDict.extra] is set to'allow'. __pydantic_fields_set__- The names of fields explicitly set during instantiation.
__pydantic_private__- Values of private attributes set on the model instance.
Create a new model by parsing and validating input data from keyword arguments.
Raises [
ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.selfis explicitly positional-only to allowselfas a field name.Expand source code
class HandshakeRequest(BaseModel): protocol: Literal["json"] = "json" version: Literal[1] = 1Ancestors
- pydantic.main.BaseModel
Class variables
var model_configvar protocol : Literal['json']var version : Literal[1]
class HandshakeResponse (**data: Any)-
Usage Documentation
A base class for creating Pydantic models.
- Attributes
- -----=
__class_vars__- The names of the class variables defined on the model.
__private_attributes__- Metadata about the private attributes of the model.
__signature__- The synthesized
__init__[Signature][inspect.Signature] of the model. __pydantic_complete__- Whether model building is completed, or if there are still undefined fields.
__pydantic_core_schema__- The core schema of the model.
__pydantic_custom_init__- Whether the model has a custom
__init__function. __pydantic_decorators__- Metadata containing the decorators defined on the model.
This replaces
Model.__validators__andModel.__root_validators__from Pydantic V1. __pydantic_generic_metadata__- A dictionary containing metadata about generic Pydantic models.
The
originandargsitems map to the [__origin__][genericalias.origin] and [__args__][genericalias.args] attributes of [generic aliases][types-genericalias], and theparameteritem maps to the__parameter__attribute of generic classes. __pydantic_parent_namespace__- Parent namespace of the model, used for automatic rebuilding of models.
__pydantic_post_init__- The name of the post-init method for the model, if defined.
__pydantic_root_model__- Whether the model is a [
RootModel][pydantic.root_model.RootModel]. __pydantic_serializer__- The
pydantic-coreSchemaSerializerused to dump instances of the model. __pydantic_validator__- The
pydantic-coreSchemaValidatorused to validate instances of the model. __pydantic_fields__- A dictionary of field names and their corresponding [
FieldInfo][pydantic.fields.FieldInfo] objects. __pydantic_computed_fields__- A dictionary of computed field names and their corresponding [
ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects. __pydantic_extra__- A dictionary containing extra values, if [
extra][pydantic.config.ConfigDict.extra] is set to'allow'. __pydantic_fields_set__- The names of fields explicitly set during instantiation.
__pydantic_private__- Values of private attributes set on the model instance.
Create a new model by parsing and validating input data from keyword arguments.
Raises [
ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.selfis explicitly positional-only to allowselfas a field name.Expand source code
class HandshakeResponse(BaseModel): error: str | None = NoneAncestors
- pydantic.main.BaseModel
Class variables
var error : Optional[str]var model_config
class Invocation (**data: Any)-
Usage Documentation
A base class for creating Pydantic models.
- Attributes
- -----=
__class_vars__- The names of the class variables defined on the model.
__private_attributes__- Metadata about the private attributes of the model.
__signature__- The synthesized
__init__[Signature][inspect.Signature] of the model. __pydantic_complete__- Whether model building is completed, or if there are still undefined fields.
__pydantic_core_schema__- The core schema of the model.
__pydantic_custom_init__- Whether the model has a custom
__init__function. __pydantic_decorators__- Metadata containing the decorators defined on the model.
This replaces
Model.__validators__andModel.__root_validators__from Pydantic V1. __pydantic_generic_metadata__- A dictionary containing metadata about generic Pydantic models.
The
originandargsitems map to the [__origin__][genericalias.origin] and [__args__][genericalias.args] attributes of [generic aliases][types-genericalias], and theparameteritem maps to the__parameter__attribute of generic classes. __pydantic_parent_namespace__- Parent namespace of the model, used for automatic rebuilding of models.
__pydantic_post_init__- The name of the post-init method for the model, if defined.
__pydantic_root_model__- Whether the model is a [
RootModel][pydantic.root_model.RootModel]. __pydantic_serializer__- The
pydantic-coreSchemaSerializerused to dump instances of the model. __pydantic_validator__- The
pydantic-coreSchemaValidatorused to validate instances of the model. __pydantic_fields__- A dictionary of field names and their corresponding [
FieldInfo][pydantic.fields.FieldInfo] objects. __pydantic_computed_fields__- A dictionary of computed field names and their corresponding [
ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects. __pydantic_extra__- A dictionary containing extra values, if [
extra][pydantic.config.ConfigDict.extra] is set to'allow'. __pydantic_fields_set__- The names of fields explicitly set during instantiation.
__pydantic_private__- Values of private attributes set on the model instance.
Create a new model by parsing and validating input data from keyword arguments.
Raises [
ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.selfis explicitly positional-only to allowselfas a field name.Expand source code
class Invocation(BaseModel): type: Literal[1] = 1 target: str arguments: list = [] invocationId: str | None = NoneAncestors
- pydantic.main.BaseModel
Class variables
var arguments : listvar invocationId : Optional[str]var model_configvar target : strvar type : Literal[1]
class Ping (**data: Any)-
Usage Documentation
A base class for creating Pydantic models.
- Attributes
- -----=
__class_vars__- The names of the class variables defined on the model.
__private_attributes__- Metadata about the private attributes of the model.
__signature__- The synthesized
__init__[Signature][inspect.Signature] of the model. __pydantic_complete__- Whether model building is completed, or if there are still undefined fields.
__pydantic_core_schema__- The core schema of the model.
__pydantic_custom_init__- Whether the model has a custom
__init__function. __pydantic_decorators__- Metadata containing the decorators defined on the model.
This replaces
Model.__validators__andModel.__root_validators__from Pydantic V1. __pydantic_generic_metadata__- A dictionary containing metadata about generic Pydantic models.
The
originandargsitems map to the [__origin__][genericalias.origin] and [__args__][genericalias.args] attributes of [generic aliases][types-genericalias], and theparameteritem maps to the__parameter__attribute of generic classes. __pydantic_parent_namespace__- Parent namespace of the model, used for automatic rebuilding of models.
__pydantic_post_init__- The name of the post-init method for the model, if defined.
__pydantic_root_model__- Whether the model is a [
RootModel][pydantic.root_model.RootModel]. __pydantic_serializer__- The
pydantic-coreSchemaSerializerused to dump instances of the model. __pydantic_validator__- The
pydantic-coreSchemaValidatorused to validate instances of the model. __pydantic_fields__- A dictionary of field names and their corresponding [
FieldInfo][pydantic.fields.FieldInfo] objects. __pydantic_computed_fields__- A dictionary of computed field names and their corresponding [
ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects. __pydantic_extra__- A dictionary containing extra values, if [
extra][pydantic.config.ConfigDict.extra] is set to'allow'. __pydantic_fields_set__- The names of fields explicitly set during instantiation.
__pydantic_private__- Values of private attributes set on the model instance.
Create a new model by parsing and validating input data from keyword arguments.
Raises [
ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.selfis explicitly positional-only to allowselfas a field name.Expand source code
class Ping(BaseModel): type: Literal[6] = 6Ancestors
- pydantic.main.BaseModel
Class variables
var model_configvar type : Literal[6]