Skip to content

set_message_context

faststream.broker.utils.set_message_context #

set_message_context(
    func: Callable[
        [StreamMessage[MsgType]],
        Awaitable[WrappedReturn[T_HandlerReturn]],
    ]
) -> Callable[
    [StreamMessage[MsgType]],
    Awaitable[WrappedReturn[T_HandlerReturn]],
]

Sets the message context for a function.

PARAMETER DESCRIPTION
func

The function to set the message context for.

TYPE: Callable[[StreamMessage[MsgType]], Awaitable[WrappedReturn[T_HandlerReturn]]]

RETURNS DESCRIPTION
Callable[[StreamMessage[MsgType]], Awaitable[WrappedReturn[T_HandlerReturn]]]

The function with the message context set.

Note

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

Source code in faststream/broker/utils.py
def set_message_context(
    func: Callable[
        [StreamMessage[MsgType]],
        Awaitable[WrappedReturn[T_HandlerReturn]],
    ],
) -> Callable[[StreamMessage[MsgType]], Awaitable[WrappedReturn[T_HandlerReturn]]]:
    """Sets the message context for a function.

    Args:
        func: The function to set the message context for.

    Returns:
        The function with the message context set.
    !!! note

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

    @wraps(func)
    async def set_message_wrapper(
        message: StreamMessage[MsgType],
    ) -> WrappedReturn[T_HandlerReturn]:
        """Wraps a function that handles a stream message.

        Args:
            message: The stream message to be handled.

        Returns:
            The wrapped return value of the handler function.
        !!! note

            The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)
        """
        with context.scope("message", message):
            return await func(message)

    return set_message_wrapper

Last update: 2023-11-13