Coverage for faststream/confluent/helpers/admin.py: 89%
35 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
1from dataclasses import dataclass
2from typing import TYPE_CHECKING
4from confluent_kafka.admin import AdminClient
6from .client import _LazyLoggerProxy
8if TYPE_CHECKING:
9 from faststream._internal.logger import LoggerState
10 from faststream.confluent.schemas import Topic
12 from .config import ConfluentFastConfig
15@dataclass(slots=True)
16class CreateResult:
17 topic: str
18 error: Exception | None
21class AdminService:
22 __slots__ = ("admin_client",)
24 def __init__(self) -> None:
25 self.admin_client: AdminClient | None = None
27 async def connect(
28 self,
29 config: "ConfluentFastConfig",
30 logger: "LoggerState | None" = None,
31 ) -> None:
32 if self.admin_client is None: 32 ↛ exitline 32 didn't return from function 'connect' because the condition on line 32 was always true
33 admin_config = config.admin_config
34 if logger is not None: 34 ↛ 40line 34 didn't jump to line 40 because the condition on line 34 was always true
35 self.admin_client = AdminClient(
36 admin_config,
37 logger=_LazyLoggerProxy(logger),
38 )
39 else:
40 self.admin_client = AdminClient(admin_config)
42 async def disconnect(self) -> None:
43 self.admin_client = None
45 @property
46 def client(self) -> AdminClient:
47 assert self.admin_client is not None, (
48 "Admin client was not connected. Please, connect the broker first."
49 )
50 return self.admin_client
52 def create_topics(self, topics: list["Topic"]) -> list[CreateResult]:
53 if not topics:
54 return []
56 create_result = self.client.create_topics(
57 [topic.to_confluent() for topic in topics],
58 )
60 final_results = []
61 for topic, f in create_result.items():
62 try:
63 f.result()
65 except Exception as e:
66 if "TOPIC_ALREADY_EXISTS" not in str(e): 66 ↛ 67line 66 didn't jump to line 67 because the condition on line 66 was never true
67 result = CreateResult(topic, e)
68 else:
69 result = CreateResult(topic, None)
71 else:
72 result = CreateResult(topic, None)
74 final_results.append(result)
76 return final_results