Skip to content

KafkaPublishCommand

faststream.confluent.response.KafkaPublishCommand #

KafkaPublishCommand(
    message: KafkaSendableMessage,
    /,
    *messages: KafkaSendableMessage,
    topic: str,
    _publish_type: PublishType,
    key: bytes | Any | None = None,
    partition: int | None = None,
    timestamp_ms: int | None = None,
    headers: dict[str, str] | None = None,
    correlation_id: str | None = None,
    reply_to: str = "",
    no_confirm: bool = False,
    timeout: float = 0.5,
)

Bases: BatchPublishCommand

Source code in faststream/confluent/response.py
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def __init__(
    self,
    message: "KafkaSendableMessage",
    /,
    *messages: "KafkaSendableMessage",
    topic: str,
    _publish_type: PublishType,
    key: bytes | Any | None = None,
    partition: int | None = None,
    timestamp_ms: int | None = None,
    headers: dict[str, str] | None = None,
    correlation_id: str | None = None,
    reply_to: str = "",
    no_confirm: bool = False,
    timeout: float = 0.5,
) -> None:
    super().__init__(
        message,
        *messages,
        destination=topic,
        reply_to=reply_to,
        correlation_id=correlation_id,
        headers=headers,
        _publish_type=_publish_type,
    )

    self.key = key
    self.partition = partition
    self.timestamp_ms = timestamp_ms
    self.no_confirm = no_confirm

    # request option
    self.timeout = timeout

    # per-message keys support
    keys, normalized = extract_per_message_keys_and_bodies(self.batch_bodies)
    if normalized is not None:
        self.batch_bodies = normalized
    self._per_message_keys = keys

key instance-attribute #

key = key

partition instance-attribute #

partition = partition

timestamp_ms instance-attribute #

timestamp_ms = timestamp_ms

no_confirm instance-attribute #

no_confirm = no_confirm

timeout instance-attribute #

timeout = timeout

body instance-attribute #

body = body

headers instance-attribute #

headers = headers or {}

correlation_id instance-attribute #

correlation_id = correlation_id

destination instance-attribute #

destination = destination

reply_to instance-attribute #

reply_to = reply_to

publish_type instance-attribute #

publish_type = _publish_type

extra_bodies instance-attribute #

extra_bodies = bodies

from_cmd classmethod #

from_cmd(
    cmd: Union[PublishCommand, KafkaPublishCommand],
    *,
    batch: bool = False,
) -> KafkaPublishCommand
Source code in faststream/confluent/response.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
@classmethod
def from_cmd(
    cls,
    cmd: Union["PublishCommand", "KafkaPublishCommand"],
    *,
    batch: bool = False,
) -> "KafkaPublishCommand":
    if isinstance(cmd, KafkaPublishCommand):
        # NOTE: Should return a copy probably.
        return cmd

    body, extra_bodies = cls._parse_bodies(cmd.body, batch=batch)

    return cls(
        body,
        *extra_bodies,
        topic=cmd.destination,
        correlation_id=cmd.correlation_id,
        headers=cmd.headers,
        reply_to=cmd.reply_to,
        _publish_type=cmd.publish_type,
    )

key_for #

key_for(index: int) -> Any | None
Source code in faststream/confluent/response.py
134
135
def key_for(self, index: int) -> Any | None:
    return key_for_index(self._per_message_keys, self.key, index)

headers_to_publish #

headers_to_publish() -> dict[str, str]
Source code in faststream/confluent/response.py
137
138
139
140
141
142
143
144
145
146
def headers_to_publish(self) -> dict[str, str]:
    headers = {}

    if self.correlation_id:
        headers["correlation_id"] = self.correlation_id

    if self.reply_to:
        headers["reply_to"] = self.reply_to

    return headers | self.headers

batch_bodies #

batch_bodies(value: Sequence[Any]) -> None
Source code in faststream/confluent/response.py
148
149
150
151
152
153
154
155
156
@BatchPublishCommand.batch_bodies.setter  # type: ignore[attr-defined, untyped-decorator]
def batch_bodies(self, value: Sequence["Any"]) -> None:
    if len(value) == 0:
        self.body = None
        self.extra_bodies = ()
    else:
        self._align_keys(value)
        self.body = value[0]
        self.extra_bodies = tuple(value[1:])

as_publish_command #

as_publish_command() -> PublishCommand

Method to transform handlers' Response result to DTO for publishers.

Source code in faststream/response/response.py
22
23
24
25
26
27
28
29
def as_publish_command(self) -> "PublishCommand":
    """Method to transform handlers' Response result to DTO for publishers."""
    return PublishCommand(
        body=self.body,
        headers=self.headers,
        correlation_id=self.correlation_id,
        _publish_type=PublishType.PUBLISH,
    )

get_publish_key #

get_publish_key() -> Any | None

Get the key for publishing this message.

Override this method in subclasses to provide broker-specific keys. Default implementation returns None (no key).

RETURNS DESCRIPTION
Any | None

The key for publishing, or None if this Response type doesn't use keys.

Source code in faststream/response/response.py
31
32
33
34
35
36
37
38
39
40
def get_publish_key(self) -> Any | None:  # noqa: PLR6301
    """Get the key for publishing this message.

    Override this method in subclasses to provide broker-specific keys.
    Default implementation returns None (no key).

    Returns:
        The key for publishing, or None if this Response type doesn't use keys.
    """
    return None

add_headers #

add_headers(
    headers: dict[str, Any], *, override: bool = True
) -> None
Source code in faststream/response/response.py
71
72
73
74
75
76
77
78
79
80
def add_headers(
    self,
    headers: dict[str, Any],
    *,
    override: bool = True,
) -> None:
    if override:
        self.headers |= headers
    else:
        self.headers = headers | self.headers