Coverage for faststream/nats/subscriber/specification.py: 97%

33 statements  

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

1from faststream._internal.endpoint.subscriber import SubscriberSpecification 

2from faststream._internal.utils.path import Address 

3from faststream.nats.configs import NatsBrokerConfig 

4from faststream.nats.schemas.js_stream import NATS_ADDRESS_SYNTAX 

5from faststream.specification.asyncapi.utils import resolve_payloads 

6from faststream.specification.schema import Message, Operation, SubscriberSpec 

7from faststream.specification.schema.bindings import ChannelBinding, nats 

8 

9from .config import NatsSubscriberSpecificationConfig 

10 

11 

12class NatsSubscriberSpecification( 

13 SubscriberSpecification[NatsBrokerConfig, NatsSubscriberSpecificationConfig], 

14): 

15 __slots__ = () 

16 

17 @property 

18 def subject(self) -> "Address": 

19 """The subject this endpoint was declared with, and its Broker address.""" 

20 return Address(self.config.subject, NATS_ADDRESS_SYNTAX).add_prefix( 

21 self._outer_config.prefix, 

22 ) 

23 

24 @property 

25 def filter_subjects(self) -> list[str]: 

26 """The subjects a JetStream consumer filters on, and their Broker addresses.""" 

27 return [ 

28 Address(subject, NATS_ADDRESS_SYNTAX) 

29 .add_prefix(self._outer_config.prefix) 

30 .template 

31 for subject in self.config.filter_subjects 

32 ] 

33 

34 @property 

35 def subjects(self) -> list[str]: 

36 """The subjects this endpoint reads, one channel each. 

37 

38 A JetStream consumer with no `subject` reaches its stream through 

39 `filter_subjects`, and each of those is an address in its own right. 

40 """ 

41 if subject := self.subject.template: 

42 return [subject] 

43 

44 return self.filter_subjects 

45 

46 @property 

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

48 return self.subjects 

49 

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

51 payloads = self.get_payloads() 

52 

53 subjects = self.subjects 

54 split = len(subjects) > 1 

55 

56 channels = {} 

57 for subject in subjects: 

58 name = self._channel_key(subject, split=split) 

59 

60 channels[name] = SubscriberSpec( 

61 address=subject, 

62 description=self.description, 

63 operation=Operation( 

64 message=Message( 

65 title=f"{name}:Message", 

66 payload=resolve_payloads(payloads), 

67 ), 

68 bindings=None, 

69 ), 

70 bindings=ChannelBinding( 

71 nats=nats.ChannelBinding( 

72 subject=subject, 

73 queue=self.config.queue, 

74 ), 

75 ), 

76 ) 

77 

78 return channels 

79 

80 

81class NotIncludeSpecifation(SubscriberSpecification): 

82 __slots__ = () 

83 

84 @property 

85 def include_in_schema(self) -> bool: 

86 return False 

87 

88 @property 

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

90 raise NotImplementedError 

91 

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

93 raise NotImplementedError