Skip to content

RabbitRouter

faststream.rabbit.router.RabbitRouter #

RabbitRouter(
    prefix: str = "",
    handlers: Sequence[RabbitRoute] = (),
    *,
    dependencies: Sequence[Depends] = (),
    middlewares: Sequence[
        Callable[[IncomingMessage], BaseMiddleware]
    ]
    | None = None,
    parser: CustomParser[IncomingMessage, RabbitMessage]
    | None = None,
    decoder: CustomDecoder[RabbitMessage] | None = None,
    include_in_schema: bool = True
)

Bases: RabbitRouter

A class representing a RabbitMQ router for publishing messages.

METHOD DESCRIPTION
_get_publisher_key

Returns the key for a given Publisher object

_update_publisher_prefix

Updates the prefix of a given Publisher object

publisher

Publishes a message to RabbitMQ

Source code in faststream/rabbit/router.py
"""

_publishers: Dict[int, Publisher]

@staticmethod
def _get_publisher_key(publisher: Publisher) -> int:
    """Get the publisher key.

    Args:
        publisher: The publisher object.

    Returns:

include_in_schema instance-attribute #

include_in_schema = include_in_schema

prefix instance-attribute #

prefix: str = prefix

include_router #

include_router(
    router: BrokerRouter[PublisherKeyType, MsgType]
) -> None

Includes a router in the current object.

PARAMETER DESCRIPTION
router

The router to be included.

TYPE: BrokerRouter[PublisherKeyType, MsgType]

RETURNS DESCRIPTION
None

None

Source code in faststream/broker/router.py
def include_router(self, router: "BrokerRouter[PublisherKeyType, MsgType]") -> None:
    """Includes a router in the current object.

    Args:
        router: The router to be included.

    Returns:
        None

    """
    for h in router._handlers:
        self.subscriber(*h.args, **h.kwargs)(h.call)

    for p in router._publishers.values():
        p = self._update_publisher_prefix(self.prefix, p)
        key = self._get_publisher_key(p)
        self._publishers[key] = self._publishers.get(key, p)

include_routers #

include_routers(
    *routers: BrokerRouter[PublisherKeyType, MsgType]
) -> None

Includes routers in the object.

PARAMETER DESCRIPTION
*routers

Variable length argument list of routers to include.

TYPE: BrokerRouter[PublisherKeyType, MsgType] DEFAULT: ()

RETURNS DESCRIPTION
None

None

Source code in faststream/broker/router.py
def include_routers(
    self, *routers: "BrokerRouter[PublisherKeyType, MsgType]"
) -> None:
    """Includes routers in the object.

    Args:
        *routers: Variable length argument list of routers to include.

    Returns:
        None

    """
    for r in routers:
        self.include_router(r)

publisher #

publisher(
    queue: RabbitQueue | str = "",
    exchange: RabbitExchange | str | None = None,
    *,
    routing_key: str = "",
    mandatory: bool = True,
    immediate: bool = False,
    timeout: TimeoutType = None,
    persist: bool = False,
    reply_to: str | None = None,
    title: str | None = None,
    description: str | None = None,
    schema: Any | None = None,
    include_in_schema: bool = True,
    priority: int | None = None,
    **message_kwargs: Any
) -> Publisher

Publishes a message to a RabbitMQ queue or exchange.

PARAMETER DESCRIPTION
queue

The RabbitMQ queue to publish the message to. Can be either a RabbitQueue object or a string representing the queue name.

TYPE: RabbitQueue | str DEFAULT: ''

exchange

The RabbitMQ exchange to publish the message to. Can be either a RabbitExchange object, a string representing the exchange name, or None.

TYPE: RabbitExchange | str | None DEFAULT: None

routing_key

The routing key to use when publishing the message.

TYPE: str DEFAULT: ''

mandatory

Whether the message is mandatory or not.

TYPE: bool DEFAULT: True

immediate

Whether the message should be delivered immediately or not.

TYPE: bool DEFAULT: False

timeout

The timeout for the publish operation.

TYPE: TimeoutType DEFAULT: None

persist

Whether the message should be persisted or not.

TYPE: bool DEFAULT: False

reply_to

The reply-to address for the message.

TYPE: str | None DEFAULT: None

title

The title of the message (AsyncAPI information).

TYPE: str | None DEFAULT: None

description

The description of the message (AsyncAPI information).

TYPE: str | None DEFAULT: None

schema

The schema of the message (AsyncAPI information).

TYPE: Any | None DEFAULT: None

include_in_schema

Whether to include the message in the API specification (AsyncAPI information).

TYPE: bool DEFAULT: True

priority

The priority of the message.

TYPE: int | None DEFAULT: None

**message_kwargs

Additional keyword arguments to include in the message.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Publisher

The Publisher object used to publish the message.

Source code in faststream/rabbit/router.py
@override
def publisher(  # type: ignore[override]
    self,
    queue: Union[RabbitQueue, str] = "",
    exchange: Union[RabbitExchange, str, None] = None,
    *,
    routing_key: str = "",
    mandatory: bool = True,
    immediate: bool = False,
    timeout: TimeoutType = None,
    persist: bool = False,
    reply_to: Optional[str] = None,
    # AsyncAPI information
    title: Optional[str] = None,
    description: Optional[str] = None,
    schema: Optional[Any] = None,
    include_in_schema: bool = True,
    priority: Optional[int] = None,
    **message_kwargs: Any,
) -> Publisher:
    """Publishes a message to a RabbitMQ queue or exchange.

    Args:
        queue: The RabbitMQ queue to publish the message to. Can be either a RabbitQueue object or a string representing the queue name.
        exchange: The RabbitMQ exchange to publish the message to. Can be either a RabbitExchange object, a string representing the exchange name, or None.
        routing_key: The routing key to use when publishing the message.
        mandatory: Whether the message is mandatory or not.
        immediate: Whether the message should be delivered immediately or not.
        timeout: The timeout for the publish operation.
        persist: Whether the message should be persisted or not.
        reply_to: The reply-to address for the message.
        title: The title of the message (AsyncAPI information).
        description: The description of the message (AsyncAPI information).
        schema: The schema of the message (AsyncAPI information).
        include_in_schema: Whether to include the message in the API specification (AsyncAPI information).
        priority: The priority of the message.
        **message_kwargs: Additional keyword arguments to include in the message.

    Returns:
        The Publisher object used to publish the message.

    """
    new_publisher = self._update_publisher_prefix(
        self.prefix,
        Publisher(
            queue=RabbitQueue.validate(queue),
            exchange=RabbitExchange.validate(exchange),
            routing_key=routing_key,
            mandatory=mandatory,
            immediate=immediate,
            timeout=timeout,
            persist=persist,
            reply_to=reply_to,
            priority=priority,
            message_kwargs=message_kwargs,
            title=title,
            _description=description,
            _schema=schema,
            include_in_schema=(
                include_in_schema
                if self.include_in_schema is None
                else self.include_in_schema
            ),
        ),
    )
    key = self._get_publisher_key(new_publisher)
    publisher = self._publishers[key] = self._publishers.get(key, new_publisher)
    return publisher

subscriber #

subscriber(
    queue: str | RabbitQueue,
    exchange: str | RabbitExchange | None = None,
    *,
    consume_args: AnyDict | None = None,
    reply_config: ReplyConfig | None = None,
    dependencies: Sequence[Depends] = (),
    filter: Filter[RabbitMessage] = default_filter,
    parser: CustomParser[IncomingMessage, RabbitMessage]
    | None = None,
    decoder: CustomDecoder[RabbitMessage] | None = None,
    middlewares: Sequence[
        Callable[[IncomingMessage], BaseMiddleware]
    ]
    | None = None,
    retry: bool | int = False,
    no_ack: bool = False,
    title: str | None = None,
    description: str | None = None,
    include_in_schema: bool = True,
    **__service_kwargs: Any
) -> Callable[
    [Callable[P_HandlerParams, T_HandlerReturn]],
    HandlerCallWrapper[
        IncomingMessage, P_HandlerParams, T_HandlerReturn
    ],
]
Source code in faststream/rabbit/router.py
    Args:
        prefix (str): The prefix to be added to the publisher's queue name.
        publisher (Publisher): The publisher object to be updated.

    Returns:
        Publisher: The updated publisher object.

    Note:
        This function is intended to be used as a decorator.

    """
    publisher.queue = model_copy(
        publisher.queue, update={"name": prefix + publisher.queue.name}
    )
    return publisher

@override
def publisher(  # type: ignore[override]
    self,
    queue: Union[RabbitQueue, str] = "",
    exchange: Union[RabbitExchange, str, None] = None,
    *,
    routing_key: str = "",
    mandatory: bool = True,
    immediate: bool = False,
    timeout: TimeoutType = None,