Coverage for faststream/confluent/publisher/usecase.py: 97%

66 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-17 21:20 +0000

1import asyncio 

2from collections.abc import Callable, Iterable, Mapping 

3from typing import TYPE_CHECKING, Any, Literal, Union, cast, overload 

4 

5from confluent_kafka import Message 

6from typing_extensions import override 

7 

8from faststream._internal.endpoint.publisher import ( 

9 PublisherSpecification, 

10 PublisherUsecase, 

11) 

12from faststream._internal.kafka import KafkaCallAssertions 

13from faststream._internal.types import P_HandlerParams, T_HandlerReturn 

14from faststream.confluent.call_wrapper import KafkaHandlerCallWrapper 

15from faststream.confluent.response import KafkaPublishCommand 

16from faststream.response.publish_type import PublishType 

17 

18if TYPE_CHECKING: 

19 from faststream._internal.basic_types import SendableMessage 

20 from faststream._internal.types import PublisherMiddleware 

21 from faststream.confluent.message import KafkaMessage 

22 from faststream.confluent.types import KafkaSendableMessage 

23 from faststream.response.response import PublishCommand 

24 

25 from .config import KafkaPublisherConfig 

26 from .producer import AsyncConfluentFastProducer 

27 

28 

29class LogicPublisher(KafkaCallAssertions, PublisherUsecase): 

30 """A class to publish messages to a Kafka topic.""" 

31 

32 __slots__ = ( 

33 "_topic", 

34 "headers", 

35 "partition", 

36 "reply_to", 

37 ) 

38 

39 _call_wrapper_class = KafkaHandlerCallWrapper 

40 _read_field = KafkaHandlerCallWrapper._read_field 

41 

42 def __init__( 

43 self, 

44 config: "KafkaPublisherConfig", 

45 specifcication: "PublisherSpecification[Any, Any]", 

46 ) -> None: 

47 super().__init__(config, specifcication) 

48 

49 self._topic = config.topic 

50 self.partition = config.partition 

51 self.reply_to = config.reply_to 

52 self.headers = dict(config.headers or {}) 

53 

54 @override 

55 def __call__( 

56 self, 

57 func: Callable[P_HandlerParams, T_HandlerReturn], 

58 ) -> "KafkaHandlerCallWrapper[P_HandlerParams, T_HandlerReturn]": 

59 # The base builds the wrapper from `_call_wrapper_class`; this only narrows the name 

60 return cast( 

61 "KafkaHandlerCallWrapper[P_HandlerParams, T_HandlerReturn]", 

62 super().__call__(func), 

63 ) 

64 

65 @property 

66 def topic(self) -> str: 

67 return f"{self._outer_config.prefix}{self._topic}" 

68 

69 @override 

70 async def request( 

71 self, 

72 message: "SendableMessage", 

73 topic: str = "", 

74 *, 

75 key: bytes | str | None = None, 

76 partition: int | None = None, 

77 timestamp_ms: int | None = None, 

78 headers: Mapping[str, str | bytes | None] | None = None, 

79 correlation_id: str | None = None, 

80 timeout: float = 0.5, 

81 ) -> "KafkaMessage": 

82 cmd = KafkaPublishCommand( 

83 message, 

84 topic=topic or self.topic, 

85 key=key, 

86 partition=partition if partition is not None else self.partition, 

87 headers=self.headers | dict(headers or {}), 

88 correlation_id=correlation_id or self._outer_config.id_generator(), 

89 timestamp_ms=timestamp_ms, 

90 timeout=timeout, 

91 _publish_type=PublishType.REQUEST, 

92 ) 

93 

94 msg: KafkaMessage = await self._basic_request( 

95 cmd, 

96 producer=self._outer_config.producer, 

97 ) 

98 return msg 

99 

100 async def flush(self) -> None: 

101 producer = cast("AsyncConfluentFastProducer", self._outer_config.producer) 

102 await producer.flush() 

103 

104 

105class DefaultPublisher(LogicPublisher): 

106 __slots__ = () 

107 

108 def __init__( 

109 self, 

110 config: "KafkaPublisherConfig", 

111 specifcication: "PublisherSpecification[Any, Any]", 

112 ) -> None: 

113 super().__init__(config, specifcication) 

114 

115 self.key = config.key 

116 

117 @overload 

118 async def publish( 

119 self, 

120 message: "SendableMessage", 

121 topic: str = "", 

122 *, 

123 key: bytes | str | None = None, 

124 partition: int | None = None, 

125 timestamp_ms: int | None = None, 

126 headers: Mapping[str, str | bytes | None] | None = None, 

127 correlation_id: str | None = None, 

128 reply_to: str = "", 

129 no_confirm: Literal[False] = False, 

130 ) -> Message | None: ... 

131 

132 @overload 

133 async def publish( 

134 self, 

135 message: "SendableMessage", 

136 topic: str = "", 

137 *, 

138 key: bytes | str | None = None, 

139 partition: int | None = None, 

140 timestamp_ms: int | None = None, 

141 headers: Mapping[str, str | bytes | None] | None = None, 

142 correlation_id: str | None = None, 

143 reply_to: str = "", 

144 no_confirm: Literal[True] = ..., 

145 ) -> asyncio.Future[Message | None]: ... 

146 

147 @overload 

148 async def publish( 

149 self, 

150 message: "SendableMessage", 

151 topic: str = "", 

152 *, 

153 key: bytes | str | None = None, 

154 partition: int | None = None, 

155 timestamp_ms: int | None = None, 

156 headers: Mapping[str, str | bytes | None] | None = None, 

157 correlation_id: str | None = None, 

158 reply_to: str = "", 

159 no_confirm: bool = False, 

160 ) -> asyncio.Future[Message | None] | Message | None: ... 

161 

162 @override 

163 async def publish( 

164 self, 

165 message: "SendableMessage", 

166 topic: str = "", 

167 *, 

168 key: bytes | str | None = None, 

169 partition: int | None = None, 

170 timestamp_ms: int | None = None, 

171 headers: Mapping[str, str | bytes | None] | None = None, 

172 correlation_id: str | None = None, 

173 reply_to: str = "", 

174 no_confirm: bool = False, 

175 ) -> asyncio.Future[Message | None] | Message | None: 

176 cmd = KafkaPublishCommand( 

177 message, 

178 topic=topic or self.topic, 

179 key=key or self.key, 

180 partition=partition if partition is not None else self.partition, 

181 reply_to=reply_to or self.reply_to, 

182 headers=self.headers | dict(headers or {}), 

183 correlation_id=correlation_id or self._outer_config.id_generator(), 

184 timestamp_ms=timestamp_ms, 

185 no_confirm=no_confirm, 

186 _publish_type=PublishType.PUBLISH, 

187 ) 

188 msg: asyncio.Future[Message | None] | Message | None = await self._basic_publish( 

189 cmd, 

190 producer=self._outer_config.producer, 

191 _extra_middlewares=(), 

192 ) 

193 return msg 

194 

195 @override 

196 async def _publish( 

197 self, 

198 cmd: Union["PublishCommand", "KafkaPublishCommand"], 

199 *, 

200 _extra_middlewares: Iterable["PublisherMiddleware"], 

201 ) -> None: 

202 """This method should be called in subscriber flow only.""" 

203 cmd = KafkaPublishCommand.from_cmd(cmd) 

204 

205 cmd.destination = self.topic 

206 cmd.add_headers(self.headers, override=False) 

207 cmd.reply_to = cmd.reply_to or self.reply_to 

208 

209 cmd.partition = cmd.partition if cmd.partition is not None else self.partition 

210 cmd.key = cmd.key or self.key 

211 

212 await self._basic_publish( 

213 cmd, 

214 producer=self._outer_config.producer, 

215 _extra_middlewares=_extra_middlewares, 

216 ) 

217 

218 @override 

219 async def request( 

220 self, 

221 message: "SendableMessage", 

222 topic: str = "", 

223 *, 

224 key: bytes | str | None = None, 

225 partition: int | None = None, 

226 timestamp_ms: int | None = None, 

227 headers: Mapping[str, str | bytes | None] | None = None, 

228 correlation_id: str | None = None, 

229 timeout: float = 0.5, 

230 ) -> "KafkaMessage": 

231 return await super().request( 

232 message, 

233 topic=topic, 

234 key=key or self.key, 

235 partition=partition, 

236 timestamp_ms=timestamp_ms, 

237 headers=headers, 

238 correlation_id=correlation_id, 

239 timeout=timeout, 

240 ) 

241 

242 

243class BatchPublisher(LogicPublisher): 

244 __slots__ = () 

245 

246 def __init__( 

247 self, 

248 config: "KafkaPublisherConfig", 

249 specification: "PublisherSpecification[Any, Any]", 

250 ) -> None: 

251 super().__init__(config, specification) 

252 self.key = config.key 

253 

254 @override 

255 async def publish( 

256 self, 

257 *messages: "KafkaSendableMessage", 

258 topic: str = "", 

259 key: bytes | str | None = None, 

260 partition: int | None = None, 

261 timestamp_ms: int | None = None, 

262 headers: Mapping[str, str | bytes | None] | None = None, 

263 correlation_id: str | None = None, 

264 reply_to: str = "", 

265 no_confirm: bool = False, 

266 ) -> None: 

267 cmd = KafkaPublishCommand( 

268 *messages, 

269 key=key or self.key, 

270 topic=topic or self.topic, 

271 partition=partition if partition is not None else self.partition, 

272 reply_to=reply_to or self.reply_to, 

273 headers=self.headers | dict(headers or {}), 

274 correlation_id=correlation_id or self._outer_config.id_generator(), 

275 timestamp_ms=timestamp_ms, 

276 no_confirm=no_confirm, 

277 _publish_type=PublishType.PUBLISH, 

278 ) 

279 

280 await self._basic_publish_batch( 

281 cmd, 

282 producer=self._outer_config.producer, 

283 _extra_middlewares=(), 

284 ) 

285 

286 @override 

287 async def _publish( 

288 self, 

289 cmd: Union["PublishCommand", "KafkaPublishCommand"], 

290 *, 

291 _extra_middlewares: Iterable["PublisherMiddleware"], 

292 ) -> None: 

293 """This method should be called in subscriber flow only.""" 

294 cmd = KafkaPublishCommand.from_cmd(cmd, batch=True) 

295 

296 if not cmd.batch_bodies: 

297 # Match the non-batch publisher: an empty result is one empty message, 

298 # not a batch of zero, which no broker can express (see issue #3056). 

299 cmd.batch_bodies = (b"",) 

300 

301 cmd.destination = self.topic 

302 cmd.add_headers(self.headers, override=False) 

303 cmd.reply_to = cmd.reply_to or self.reply_to 

304 

305 cmd.partition = cmd.partition if cmd.partition is not None else self.partition 

306 cmd.key = cmd.key or self.key 

307 

308 await self._basic_publish_batch( 

309 cmd, 

310 producer=self._outer_config.producer, 

311 _extra_middlewares=_extra_middlewares, 

312 )