Bases: JsParser
A class to parse NATS batch messages.
Source code in faststream/nats/parser.py
| def __init__(
self,
*,
pattern: str,
) -> None:
path_re, _ = compile_nats_wildcard(pattern)
self._path_re = path_re
|
parse_batch async
Source code in faststream/nats/parser.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 | async def parse_batch(
self,
message: list["Msg"],
) -> "StreamMessage[list[Msg]]":
body: list[bytes] = []
batch_headers: list[dict[str, str]] = []
if message:
path = match_path(self._path_re, message[0].subject)
for m in message:
batch_headers.append(m.headers or {})
body.append(m.data)
else:
path = {}
headers = next(iter(batch_headers), {})
return NatsBatchMessage(
raw_message=message,
body=body,
path=path,
headers=headers,
batch_headers=batch_headers,
)
|
decode_batch async
decode_batch(
msg: StreamMessage[list[Msg]],
) -> list[DecodedMessage]
Source code in faststream/nats/parser.py
134
135
136
137
138
139
140
141
142
143
144 | async def decode_batch(
self,
msg: "StreamMessage[list[Msg]]",
) -> list["DecodedMessage"]:
data: list[DecodedMessage] = []
for m in msg.raw_message:
one_msg = await self.parse_message(m)
data.append(decode_message(one_msg))
return data
|
decode_message async
Source code in faststream/nats/parser.py
| async def decode_message( # ruff: ignore[no-self-use]
self,
msg: "StreamMessage[Any]",
) -> "DecodedMessage":
return decode_message(msg)
|
parse_message async
Source code in faststream/nats/parser.py
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 | async def parse_message(
self,
message: "Msg",
) -> "StreamMessage[Msg]":
path = match_path(self._path_re, message.subject)
headers = message.header or {}
return NatsMessage(
raw_message=message,
body=message.data,
path=path,
reply_to=headers.get("reply_to", ""), # differ from core
headers=headers,
content_type=headers.get("content-type"),
message_id=headers.get("message_id"),
correlation_id=headers.get("correlation_id"),
)
|