Coverage for faststream/redis/publisher/specification.py: 94%

57 statements  

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

1from faststream._internal.endpoint.publisher import PublisherSpecification 

2from faststream.redis.configs import RedisBrokerConfig 

3from faststream.redis.schemas import ListSub, PubSub, StreamSub 

4from faststream.specification.asyncapi.utils import resolve_payloads 

5from faststream.specification.schema import Message, Operation, PublisherSpec 

6from faststream.specification.schema.bindings import ChannelBinding, redis 

7 

8from .config import RedisPublisherSpecificationConfig 

9 

10 

11class RedisPublisherSpecification( 

12 PublisherSpecification[RedisBrokerConfig, RedisPublisherSpecificationConfig], 

13): 

14 __slots__ = () 

15 

16 def get_schema(self) -> dict[str, PublisherSpec]: 

17 payloads = self.get_payloads() 

18 

19 return { 

20 self.name: PublisherSpec( 

21 address=self.address, 

22 description=self.config.description_, 

23 operation=Operation( 

24 message=Message( 

25 title=f"{self.name}:Message", 

26 payload=resolve_payloads(payloads, "Publisher"), 

27 ), 

28 bindings=None, 

29 ), 

30 bindings=ChannelBinding( 

31 redis=self.channel_binding, 

32 ), 

33 ), 

34 } 

35 

36 @property 

37 def address(self) -> str: 

38 raise NotImplementedError 

39 

40 @property 

41 def channel_binding(self) -> redis.ChannelBinding: 

42 raise NotImplementedError 

43 

44 

45class ChannelPublisherSpecification(RedisPublisherSpecification): 

46 __slots__ = ("channel",) 

47 

48 def __init__( 

49 self, 

50 _outer_config: RedisBrokerConfig, 

51 specification_config: RedisPublisherSpecificationConfig, 

52 channel: PubSub, 

53 ) -> None: 

54 super().__init__(_outer_config, specification_config) 

55 self.channel = channel 

56 

57 @property 

58 def name(self) -> str: 

59 if self.config.title_: 

60 return self.config.title_ 

61 

62 return f"{self.address}:Publisher" 

63 

64 @property 

65 def address(self) -> str: 

66 # Through `PubSub`, the way the usecase does it: a prefix decorates the 

67 # declaration, so a `{{` of its own comes off with the rest. 

68 return self.channel.add_prefix(self._outer_config.prefix).address.template 

69 

70 @property 

71 def channel_binding(self) -> redis.ChannelBinding: 

72 return redis.ChannelBinding( 

73 channel=self.address, 

74 method="publish", 

75 ) 

76 

77 

78class ListPublisherSpecification(RedisPublisherSpecification): 

79 __slots__ = ("list_sub",) 

80 

81 def __init__( 

82 self, 

83 _outer_config: RedisBrokerConfig, 

84 specification_config: RedisPublisherSpecificationConfig, 

85 list_sub: ListSub, 

86 ) -> None: 

87 super().__init__(_outer_config, specification_config) 

88 self.list_sub = list_sub 

89 

90 @property 

91 def name(self) -> str: 

92 if self.config.title_: 92 ↛ 93line 92 didn't jump to line 93 because the condition on line 92 was never true

93 return self.config.title_ 

94 

95 return f"{self.address}:Publisher" 

96 

97 @property 

98 def address(self) -> str: 

99 return f"{self._outer_config.prefix}{self.list_sub.name}" 

100 

101 @property 

102 def channel_binding(self) -> redis.ChannelBinding: 

103 return redis.ChannelBinding( 

104 channel=self.address, 

105 method="rpush", 

106 ) 

107 

108 

109class StreamPublisherSpecification(RedisPublisherSpecification): 

110 __slots__ = ("stream_sub",) 

111 

112 def __init__( 

113 self, 

114 _outer_config: RedisBrokerConfig, 

115 specification_config: RedisPublisherSpecificationConfig, 

116 stream_sub: StreamSub, 

117 ) -> None: 

118 super().__init__(_outer_config, specification_config) 

119 self.stream_sub = stream_sub 

120 

121 @property 

122 def name(self) -> str: 

123 if self.config.title_: 123 ↛ 124line 123 didn't jump to line 124 because the condition on line 123 was never true

124 return self.config.title_ 

125 

126 return f"{self.address}:Publisher" 

127 

128 @property 

129 def address(self) -> str: 

130 return f"{self._outer_config.prefix}{self.stream_sub.name}" 

131 

132 @property 

133 def channel_binding(self) -> "redis.ChannelBinding": 

134 return redis.ChannelBinding( 

135 channel=self.address, 

136 method="xadd", 

137 )