Coverage for faststream/_internal/endpoint/subscriber/specification.py: 93%

33 statements  

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

1from abc import abstractmethod 

2from typing import TYPE_CHECKING, Any 

3 

4from typing_extensions import ( 

5 TypeVar as TypeVar313, 

6) 

7 

8from faststream._internal.configs import BrokerConfig, SubscriberSpecificationConfig 

9from faststream._internal.endpoint.specification import EndpointSpecification 

10from faststream.exceptions import SetupError 

11from faststream.specification.asyncapi.message import parse_handler_params 

12from faststream.specification.asyncapi.utils import to_camelcase 

13 

14if TYPE_CHECKING: 

15 from faststream._internal.endpoint.subscriber.call_item import ( 

16 CallsCollection, 

17 ) 

18 from faststream.specification.schema import SubscriberSpec 

19 

20 

21T_SpecificationConfig = TypeVar313( 

22 "T_SpecificationConfig", 

23 bound=SubscriberSpecificationConfig, 

24 default=SubscriberSpecificationConfig, 

25) 

26T_BrokerConfig = TypeVar313("T_BrokerConfig", bound=BrokerConfig, default=BrokerConfig) 

27 

28 

29class SubscriberSpecification( 

30 EndpointSpecification[T_BrokerConfig, T_SpecificationConfig], 

31): 

32 __slots__ = ("calls",) 

33 

34 def __init__( 

35 self, 

36 _outer_config: "T_BrokerConfig", 

37 specification_config: "T_SpecificationConfig", 

38 calls: "CallsCollection[Any]", 

39 ) -> None: 

40 super().__init__(_outer_config, specification_config) 

41 self.calls = calls 

42 

43 @property 

44 def description(self) -> str | None: 

45 return self.config.description_ or self.calls.description 

46 

47 @property 

48 def call_name(self) -> str: 

49 return self.calls.name or "Subscriber" 

50 

51 def _channel_key(self, address: str, *, split: bool) -> str: 

52 """The key one address's channel goes under. 

53 

54 A `title` names one channel, so `split` prefixes it with the address to 

55 keep several of them unique. 

56 """ 

57 if not self.config.title_: 

58 return f"{address}:{self.call_name}" 

59 

60 return f"{self.config.title_}:{address}" if split else self.config.title_ 

61 

62 def get_payloads(self) -> list[tuple["dict[str, Any]", str]]: 

63 payloads: list[tuple[dict[str, Any], str]] = [] 

64 

65 call_name = self.call_name 

66 

67 for h in self.calls: 

68 if h.dependant is None: 68 ↛ 69line 68 didn't jump to line 69 because the condition on line 68 was never true

69 msg = "You should setup `Handler` at first." 

70 raise SetupError(msg) 

71 

72 body = parse_handler_params( 

73 h.dependant, 

74 prefix=f"{self.config.title_ or call_name}:Message", 

75 ) 

76 payloads.append((body, to_camelcase(h.name))) 

77 

78 if not self.calls: 

79 payloads.append( 

80 ( 

81 { 

82 "title": f"{self.config.title_ or call_name}:Message:Payload", 

83 }, 

84 to_camelcase(call_name), 

85 ), 

86 ) 

87 

88 return payloads 

89 

90 @property 

91 @abstractmethod 

92 def channel_labels(self) -> list[str]: 

93 """The label each of this endpoint's channels is keyed by, one per channel. 

94 

95 Usually the address, but not always: MQTT keeps the `$share/` prefix its 

96 address drops, and RabbitMQ names a queue and an exchange rather than the 

97 routing key a message travels by. 

98 """ 

99 raise NotImplementedError 

100 

101 @property 

102 def name(self) -> str: 

103 """The key of the first channel this endpoint names.""" 

104 labels = self.channel_labels 

105 return self._channel_key(labels[0], split=len(labels) > 1) 

106 

107 @abstractmethod 

108 def get_schema(self) -> dict[str, "SubscriberSpec"]: 

109 raise NotImplementedError