Coverage for faststream/rabbit/helpers/declarer.py: 98%

35 statements  

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

1from typing import TYPE_CHECKING, Optional, Protocol, cast 

2 

3from faststream._internal.constants import EMPTY 

4 

5if TYPE_CHECKING: 

6 import aio_pika 

7 

8 from faststream.rabbit.schemas import Channel, RabbitExchange, RabbitQueue 

9 

10 from .channel_manager import ChannelManager 

11 

12 

13class RabbitDeclarer(Protocol): 

14 """An utility class to declare RabbitMQ queues and exchanges.""" 

15 

16 __slots__ = () 

17 

18 def disconnect(self) -> None: ... 

19 

20 async def declare_queue( 

21 self, 

22 queue: "RabbitQueue", 

23 declare: bool = EMPTY, 

24 *, 

25 channel: Optional["Channel"] = None, 

26 ) -> "aio_pika.RobustQueue": 

27 """Declare a queue.""" 

28 ... 

29 

30 async def declare_exchange( 

31 self, 

32 exchange: "RabbitExchange", 

33 declare: bool = EMPTY, 

34 *, 

35 channel: Optional["Channel"] = None, 

36 ) -> "aio_pika.RobustExchange": 

37 """Declare an exchange, parent exchanges and bind them each other.""" 

38 ... 

39 

40 

41class FakeRabbitDeclarer(RabbitDeclarer): 

42 __slots__ = () 

43 

44 def disconnect(self) -> None: 

45 raise NotImplementedError 

46 

47 async def declare_queue( 

48 self, 

49 queue: "RabbitQueue", 

50 declare: bool = EMPTY, 

51 *, 

52 channel: Optional["Channel"] = None, 

53 ) -> "aio_pika.RobustQueue": 

54 raise NotImplementedError 

55 

56 async def declare_exchange( 

57 self, 

58 exchange: "RabbitExchange", 

59 declare: bool = EMPTY, 

60 *, 

61 channel: Optional["Channel"] = None, 

62 ) -> "aio_pika.RobustExchange": 

63 raise NotImplementedError 

64 

65 

66class RabbitDeclarerImpl(RabbitDeclarer): 

67 __slots__ = ("__channel_manager", "_exchanges", "_queues") 

68 

69 def __init__(self, channel_manager: "ChannelManager") -> None: 

70 self.__channel_manager = channel_manager 

71 self._queues: dict[RabbitQueue, aio_pika.RobustQueue] = {} 

72 self._exchanges: dict[RabbitExchange, aio_pika.RobustExchange] = {} 

73 

74 def __repr__(self) -> str: 

75 return f"{self.__class__.__name__}(queues={list(self._queues.keys())}, exchanges={list(self._exchanges.keys())})" 

76 

77 def disconnect(self) -> None: 

78 self._queues.clear() 

79 self._exchanges.clear() 

80 

81 async def declare_queue( 

82 self, 

83 queue: "RabbitQueue", 

84 declare: bool = EMPTY, 

85 *, 

86 channel: Optional["Channel"] = None, 

87 ) -> "aio_pika.RobustQueue": 

88 if (q := self._queues.get(queue)) is None: 

89 if declare is EMPTY: 89 ↛ 92line 89 didn't jump to line 92 because the condition on line 89 was always true

90 declare = queue.declare 

91 

92 channel_obj = await self.__channel_manager.get_channel(channel) 

93 

94 self._queues[queue] = q = cast( 

95 "aio_pika.RobustQueue", 

96 await channel_obj.declare_queue( 

97 name=queue.name, 

98 durable=queue.durable, 

99 exclusive=queue.exclusive, 

100 passive=not declare, 

101 auto_delete=queue.auto_delete, 

102 arguments=queue.arguments, 

103 timeout=queue.timeout, 

104 robust=queue.robust, 

105 ), 

106 ) 

107 

108 return q 

109 

110 async def declare_exchange( 

111 self, 

112 exchange: "RabbitExchange", 

113 declare: bool = EMPTY, 

114 *, 

115 channel: Optional["Channel"] = None, 

116 ) -> "aio_pika.RobustExchange": 

117 channel_obj = await self.__channel_manager.get_channel(channel) 

118 

119 if not exchange.name: 

120 return channel_obj.default_exchange 

121 

122 if (exch := self._exchanges.get(exchange)) is None: 

123 if declare is EMPTY: 

124 declare = exchange.declare 

125 

126 self._exchanges[exchange] = exch = cast( 

127 "aio_pika.RobustExchange", 

128 await channel_obj.declare_exchange( 

129 name=exchange.name, 

130 type=exchange.type.value, 

131 durable=exchange.durable, 

132 auto_delete=exchange.auto_delete, 

133 passive=not declare, 

134 arguments=exchange.arguments, 

135 timeout=exchange.timeout, 

136 robust=exchange.robust, 

137 internal=False, # deprecated RMQ option 

138 ), 

139 ) 

140 

141 if exchange.bind_to is not None: 

142 parent = await self.declare_exchange(exchange.bind_to) 

143 await exch.bind( 

144 exchange=parent, 

145 routing_key=exchange.routing(), 

146 arguments=exchange.bind_arguments, 

147 timeout=exchange.timeout, 

148 robust=exchange.robust, 

149 ) 

150 

151 return exch