Messages

This page describes how we parse all received data from the StreamReader that lies under the Controller. These messages are then parsed either as a Reply or as an Event depending on whether BaseMessage.is_event is False or not (which means that BaseMessage.status equals 650).

Note that a Message can contain multiple sub-messages, each with their own status and header content. Additionally a sub-message can also contain a body.

Main class

class aiostem.utils.message.Message[source]

Bases: BaseMessage

Utility class used to parse any received message.

items: Sequence[MessageLine | MessageData]

List of sub-messages received within this message.

property keyword: str

Extract the first word from the header.

Note

An event always provides its keyword in the first item.

serialize() str[source]

Serialize this message to a string.

Return type:

str

Message items

class aiostem.utils.message.MessageLine[source]

Bases: BaseMessage

A sub-message with only a single line.

serialize() str[source]

Serialize this line sub-message to a string.

Return type:

str

class aiostem.utils.message.MessageData[source]

Bases: BaseMessage

A sub-message with a body part attached.

data: str = ''

Additional data (body of the message).

serialize() str[source]

Serialize this data sub-message to a string.

Return type:

str

Base class

class aiostem.utils.message.BaseMessage[source]

Bases: ABC

Base class for any kind of message items.

A received message (like a reply) can contain sub-messages that also inherit from this base class.

END_OF_LINE: Final[str] = '\r\n'

The end of line applied while serializing messages.

status: int

Status code of this message or message item.

header: str

Text that comes along the status in this (sub-)message.

property is_error: bool

Whether our status is an error (greater or equal to 400).

property is_event: bool

Tell whether this message is an event.

This property is a simple helper to tell whether our status equals to 650.

property is_success: bool

Whether our status is a success status (=250).

property keyword: str

Extract the first word from the header.

Note

This is not relevant for all kind of messages.

abstractmethod serialize() str[source]

Serialize this message to text that could have been sent.

Return type:

str

Helpers

async aiostem.utils.message.messages_from_stream(stream: StreamReader) AsyncIterator[Message][source]

Parse messages from the underlying stream.

Parameters:

stream (StreamReader) – The asyncio stream reader to read messages from.

Raises:

ProtocolError – When we receive a malformed message.

Yields:

Messages as they are parsed.

Return type:

AsyncIterator[Message]