Skip to content

Internal Middlewares

fastpubsub.middlewares.HandleMessageSerializerMiddleware

HandleMessageSerializerMiddleware(next_call, target)

Bases: BaseMiddleware

A special middleware for handling incoming messages serialization.

Initializes the HandleMessageSerializerMiddleware.

PARAMETER DESCRIPTION
next_call

The next call to the middleware chain.

TYPE: BaseMiddleware | None

target

The target callable to handle the message. An async function runs on the event loop. A sync function runs in the thread pool.

TYPE: MessageHandler

Source code in fastpubsub/middlewares/di.py
def __init__(
    self, next_call: BaseMiddleware | None, target: MessageHandler
):
    """Initializes the HandleMessageSerializerMiddleware.

    Args:
        next_call: The next call to the middleware chain.
        target: The target callable to handle the message. An async
            function runs on the event loop. A sync function runs in
            the thread pool.
    """
    super().__init__(next_call)
    self.target = target
    self.is_async_target = inspect.iscoroutinefunction(target)

next_call instance-attribute

next_call = next_call

target instance-attribute

target = target

is_async_target instance-attribute

is_async_target = inspect.iscoroutinefunction(target)

on_publish async

on_publish(data, ordering_key, attributes)

Handles a publish event.

When extending this methods, you should always call await super().on_publish(...) to continue the chain.

PARAMETER DESCRIPTION
data

The message data.

TYPE: bytes

ordering_key

The ordering key for the message.

TYPE: str

attributes

A dictionary of message attributes.

TYPE: dict[str, str] | None

Source code in fastpubsub/middlewares/base.py
async def on_publish(
    self, data: bytes, ordering_key: str, attributes: dict[str, str] | None
) -> Any:
    """Handles a publish event.

    When extending this methods, you should always call
    `await super().on_publish(...)` to continue the chain.

    Args:
        data: The message data.
        ordering_key: The ordering key for the message.
        attributes: A dictionary of message attributes.
    """
    if not self.next_call:
        return

    return await self.next_call.on_publish(data, ordering_key, attributes)

on_message async

on_message(message)

Handles a message.

PARAMETER DESCRIPTION
message

The message to handle.

TYPE: Message

RETURNS DESCRIPTION
Any

The result of the target callable.

Source code in fastpubsub/middlewares/di.py
async def on_message(self, message: Message) -> Any:
    """Handles a message.

    Args:
        message: The message to handle.

    Returns:
        The result of the target callable.
    """
    if self.is_async_target:
        return await self.target(message)

    return await apply_async(self.target, message)

fastpubsub.middlewares.PublishMessageSerializerMiddleware

PublishMessageSerializerMiddleware(
    next_call, project_id, topic_name, autocreate=True
)

Bases: BaseMiddleware

A special middleware for publishing messages serialization.

Initializes the PublishMessageSerializerMiddleware.

PARAMETER DESCRIPTION
next_call

The next call to the middleware chain.

TYPE: BaseMiddleware | None

project_id

The Google Cloud project ID.

TYPE: str

topic_name

The name of the topic.

TYPE: str

autocreate

Whether to automatically create the topic.

TYPE: bool DEFAULT: True

Source code in fastpubsub/middlewares/di.py
def __init__(
    self,
    next_call: BaseMiddleware | None,
    project_id: str,
    topic_name: str,
    autocreate: bool = True,
):
    """Initializes the PublishMessageSerializerMiddleware.

    Args:
        next_call: The next call to the middleware chain.
        project_id: The Google Cloud project ID.
        topic_name: The name of the topic.
        autocreate: Whether to automatically create the topic.
    """
    super().__init__(next_call)
    self.project_id = project_id
    self.topic_name = topic_name
    self.autocreate = autocreate
    self._client_factory: Any = None
    self._client: Any = None

next_call instance-attribute

next_call = next_call

project_id instance-attribute

project_id = project_id

topic_name instance-attribute

topic_name = topic_name

autocreate instance-attribute

autocreate = autocreate

on_message async

on_message(message)

Handles a message.

When extending this methods, you should always call await super().on_message(...) to continue the chain.

PARAMETER DESCRIPTION
message

The message to handle.

TYPE: Message

Source code in fastpubsub/middlewares/base.py
async def on_message(self, message: "Message") -> Any:
    """Handles a message.

    When extending this methods, you should always call
    `await super().on_message(...)` to continue the chain.

    Args:
        message: The message to handle.
    """
    if not self.next_call:
        return

    return await self.next_call.on_message(message)

on_publish async

on_publish(data, ordering_key, attributes)

Publishes a message.

PARAMETER DESCRIPTION
data

The message data.

TYPE: bytes

ordering_key

The ordering key for the message.

TYPE: str

attributes

A dictionary of message attributes.

TYPE: dict[str, str] | None

Source code in fastpubsub/middlewares/di.py
async def on_publish(
    self, data: bytes, ordering_key: str, attributes: dict[str, str] | None
) -> Any:
    """Publishes a message.

    Args:
        data: The message data.
        ordering_key: The ordering key for the message.
        attributes: A dictionary of message attributes.
    """
    client = self._get_client()
    topic_key = (self.project_id, self.topic_name)
    if self.autocreate and topic_key not in self._created_topics:
        await client.create_topic(self.topic_name)
        self._created_topics.add(topic_key)

    try:
        return await client.publish(
            topic_name=self.topic_name,
            data=data,
            ordering_key=ordering_key,
            attributes=attributes,
        )
    except NotFoundError:
        self._created_topics.discard(topic_key)
        raise