1010import os
1111import warnings
1212
13- from typing import Callable , Coroutine , Union
13+ from typing import Callable , Coroutine , Optional , Union , Tuple
1414import weakref
1515
1616from aiohttp .web import HTTPException
3636
3737from .error import ProtocolMinorVersionNotSupported
3838from .protocol_registry import ProtocolRegistry
39+ from .util import (
40+ get_version_from_message_type ,
41+ validate_get_response_version ,
42+ # WARNING_DEGRADED_FEATURES,
43+ # WARNING_VERSION_MISMATCH,
44+ # WARNING_VERSION_NOT_SUPPORTED,
45+ )
3946
4047LOGGER = logging .getLogger (__name__ )
4148
@@ -133,16 +140,22 @@ async def handle_message(
133140 inbound_message: The inbound message instance
134141 send_outbound: Async function to send outbound messages
135142
143+ # Raises:
144+ # MessageParseError: If the message type version is not supported
145+
136146 Returns:
137147 The response from the handler
138148
139149 """
140150 r_time = get_timer ()
141151
142152 error_result = None
153+ version_warning = None
143154 message = None
144155 try :
145- message = await self .make_message (inbound_message .payload )
156+ (message , warning ) = await self .make_message (
157+ profile , inbound_message .payload
158+ )
146159 except ProblemReportParseError :
147160 pass # avoid problem report recursion
148161 except MessageParseError as e :
@@ -155,6 +168,47 @@ async def handle_message(
155168 )
156169 if inbound_message .receipt .thread_id :
157170 error_result .assign_thread_id (inbound_message .receipt .thread_id )
171+ # if warning:
172+ # warning_message_type = inbound_message.payload.get("@type")
173+ # if warning == WARNING_DEGRADED_FEATURES:
174+ # LOGGER.error(
175+ # f"Sending {WARNING_DEGRADED_FEATURES} problem report, "
176+ # "message type received with a minor version at or higher"
177+ # " than protocol minimum supported and current minor version "
178+ # f"for message_type {warning_message_type}"
179+ # )
180+ # version_warning = ProblemReport(
181+ # description={
182+ # "en": (
183+ # "message type received with a minor version at or "
184+ # "higher than protocol minimum supported and current"
185+ # f" minor version for message_type {warning_message_type}"
186+ # ),
187+ # "code": WARNING_DEGRADED_FEATURES,
188+ # }
189+ # )
190+ # elif warning == WARNING_VERSION_MISMATCH:
191+ # LOGGER.error(
192+ # f"Sending {WARNING_VERSION_MISMATCH} problem report, message "
193+ # "type received with a minor version higher than current minor "
194+ # f"version for message_type {warning_message_type}"
195+ # )
196+ # version_warning = ProblemReport(
197+ # description={
198+ # "en": (
199+ # "message type received with a minor version higher"
200+ # " than current minor version for message_type"
201+ # f" {warning_message_type}"
202+ # ),
203+ # "code": WARNING_VERSION_MISMATCH,
204+ # }
205+ # )
206+ # elif warning == WARNING_VERSION_NOT_SUPPORTED:
207+ # raise MessageParseError(
208+ # f"Message type version not supported for {warning_message_type}"
209+ # )
210+ # if version_warning and inbound_message.receipt.thread_id:
211+ # version_warning.assign_thread_id(inbound_message.receipt.thread_id)
158212
159213 trace_event (
160214 self .profile .settings ,
@@ -199,6 +253,8 @@ async def handle_message(
199253
200254 if error_result :
201255 await responder .send_reply (error_result )
256+ elif version_warning :
257+ await responder .send_reply (version_warning )
202258 elif context .message :
203259 context .injector .bind_instance (BaseResponder , responder )
204260
@@ -215,7 +271,9 @@ async def handle_message(
215271 perf_counter = r_time ,
216272 )
217273
218- async def make_message (self , parsed_msg : dict ) -> BaseMessage :
274+ async def make_message (
275+ self , profile : Profile , parsed_msg : dict
276+ ) -> Tuple [BaseMessage , Optional [str ]]:
219277 """
220278 Deserialize a message dict into the appropriate message instance.
221279
@@ -224,6 +282,7 @@ async def make_message(self, parsed_msg: dict) -> BaseMessage:
224282
225283 Args:
226284 parsed_msg: The parsed message
285+ profile: Profile
227286
228287 Returns:
229288 An instance of the corresponding message class for this message
@@ -240,6 +299,7 @@ async def make_message(self, parsed_msg: dict) -> BaseMessage:
240299
241300 if not message_type :
242301 raise MessageParseError ("Message does not contain '@type' parameter" )
302+ message_type_rec_version = get_version_from_message_type (message_type )
243303
244304 registry : ProtocolRegistry = self .profile .inject (ProtocolRegistry )
245305 try :
@@ -256,8 +316,10 @@ async def make_message(self, parsed_msg: dict) -> BaseMessage:
256316 if "/problem-report" in message_type :
257317 raise ProblemReportParseError ("Error parsing problem report message" )
258318 raise MessageParseError (f"Error deserializing message: { e } " ) from e
259-
260- return instance
319+ _ , warning = await validate_get_response_version (
320+ profile , message_type_rec_version , message_cls
321+ )
322+ return (instance , warning )
261323
262324 async def complete (self , timeout : float = 0.1 ):
263325 """Wait for pending tasks to complete."""
0 commit comments