Skip to content

NatsRouter

faststream.nats.router.NatsRouter #

NatsRouter(prefix: str = '', handlers: Sequence[NatsRoute] = (), *, dependencies: Sequence[Depends] = (), middlewares: Sequence[Callable[[Msg], BaseMiddleware]] | None = None, parser: CustomParser[Msg, NatsMessage] | None = None, decoder: CustomDecoder[NatsMessage] | None = None, include_in_schema: bool = True)

Bases: NatsRouter

A class to represent a NATS router.

Source code in faststream/nats/router.py
@override
def publisher(  # type: ignore[override]
    self,
    subject: str,
    headers: Optional[Dict[str, str]] = None,
    reply_to: str = "",
    # AsyncAPI information
    title: Optional[str] = None,
    description: Optional[str] = None,
    schema: Optional[Any] = None,
    include_in_schema: bool = True,

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(subject: str, headers: dict[str, str] | None = None, reply_to: str = '', title: str | None = None, description: str | None = None, schema: Any | None = None, include_in_schema: bool = True) -> Publisher
Source code in faststream/nats/router.py
@override
def publisher(  # type: ignore[override]
    self,
    subject: str,
    headers: Optional[Dict[str, str]] = None,
    reply_to: str = "",
    # AsyncAPI information
    title: Optional[str] = None,
    description: Optional[str] = None,
    schema: Optional[Any] = None,
    include_in_schema: bool = True,
) -> Publisher:
    new_publisher = self._update_publisher_prefix(
        self.prefix,
        Publisher(
            subject=subject,
            reply_to=reply_to,
            headers=headers,
            title=title,
            _description=description,
            _schema=schema,
            include_in_schema=(
                include_in_schema
                if self.include_in_schema is None
                else self.include_in_schema
            ),
        ),
    )
    publisher_key = self._get_publisher_key(new_publisher)
    publisher = self._publishers[publisher_key] = self._publishers.get(
        publisher_key, new_publisher
    )
    return publisher

subscriber #

subscriber(subject: str, queue: str = '', pending_msgs_limit: int | None = None, pending_bytes_limit: int | None = None, max_msgs: int = 0, ack_first: bool = False, stream: str | JStream | None = None, durable: str | None = None, config: ConsumerConfig | None = None, ordered_consumer: bool = False, idle_heartbeat: float | None = None, flow_control: bool = False, deliver_policy: DeliverPolicy | None = None, headers_only: bool | None = None, pull_sub: PullSub | None = None, inbox_prefix: bytes = api.INBOX_PREFIX, dependencies: Sequence[Depends] = (), parser: CustomParser[Msg, NatsMessage] | None = None, decoder: CustomDecoder[NatsMessage] | None = None, middlewares: Sequence[Callable[[Msg], BaseMiddleware]] | None = None, filter: Filter[NatsMessage] = default_filter, retry: bool = 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[Msg, P_HandlerParams, T_HandlerReturn]]
Source code in faststream/nats/router.py
return publisher