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
« 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
9from .config import NatsSubscriberSpecificationConfig
12class NatsSubscriberSpecification(
13 SubscriberSpecification[NatsBrokerConfig, NatsSubscriberSpecificationConfig],
14):
15 __slots__ = ()
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 )
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 ]
34 @property
35 def subjects(self) -> list[str]:
36 """The subjects this endpoint reads, one channel each.
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]
44 return self.filter_subjects
46 @property
47 def channel_labels(self) -> list[str]:
48 return self.subjects
50 def get_schema(self) -> dict[str, SubscriberSpec]:
51 payloads = self.get_payloads()
53 subjects = self.subjects
54 split = len(subjects) > 1
56 channels = {}
57 for subject in subjects:
58 name = self._channel_key(subject, split=split)
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 )
78 return channels
81class NotIncludeSpecifation(SubscriberSpecification):
82 __slots__ = ()
84 @property
85 def include_in_schema(self) -> bool:
86 return False
88 @property
89 def channel_labels(self) -> list[str]:
90 raise NotImplementedError
92 def get_schema(self) -> dict[str, "SubscriberSpec"]:
93 raise NotImplementedError