Skip to content

encode_message

faststream.broker.parsers.encode_message #

encode_message(
    msg: SendableMessage,
) -> Tuple[bytes, Optional[ContentType]]

Encodes a message.

PARAMETER DESCRIPTION
msg

The message to be encoded.

TYPE: SendableMessage

RETURNS DESCRIPTION
Tuple[bytes, Optional[ContentType]]

A tuple containing the encoded message as bytes and the content type of the message.

Note

The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)

Source code in faststream/broker/parsers.py
def encode_message(msg: SendableMessage) -> Tuple[bytes, Optional[ContentType]]:
    """Encodes a message.

    Args:
        msg: The message to be encoded.

    Returns:
        A tuple containing the encoded message as bytes and the content type of the message.
    !!! note

        The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)
    """
    if msg is None:
        return b"", None

    if isinstance(msg, bytes):
        return msg, None

    if isinstance(msg, str):
        return msg.encode(), ContentTypes.text.value

    return (
        dump_json(msg).encode(),
        ContentTypes.json.value,
    )

Last update: 2023-11-13