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

1from dataclasses import dataclass 

2from typing import TYPE_CHECKING 

3 

4from confluent_kafka.admin import AdminClient 

5 

6from .client import _LazyLoggerProxy 

7 

8if TYPE_CHECKING: 

9 from faststream._internal.logger import LoggerState 

10 from faststream.confluent.schemas import Topic 

11 

12 from .config import ConfluentFastConfig 

13 

14 

15@dataclass(slots=True) 

16class CreateResult: 

17 topic: str 

18 error: Exception | None 

19 

20 

21class AdminService: 

22 __slots__ = ("admin_client",) 

23 

24 def __init__(self) -> None: 

25 self.admin_client: AdminClient | None = None 

26 

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) 

41 

42 async def disconnect(self) -> None: 

43 self.admin_client = None 

44 

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 

51 

52 def create_topics(self, topics: list["Topic"]) -> list[CreateResult]: 

53 if not topics: 

54 return [] 

55 

56 create_result = self.client.create_topics( 

57 [topic.to_confluent() for topic in topics], 

58 ) 

59 

60 final_results = [] 

61 for topic, f in create_result.items(): 

62 try: 

63 f.result() 

64 

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) 

70 

71 else: 

72 result = CreateResult(topic, None) 

73 

74 final_results.append(result) 

75 

76 return final_results