33from chartlets .extensioncontext import ExtensionContext
44from chartlets .response import Response
55from chartlets .util .assertions import assert_is_instance_of
6- from chartlets .util .assertions import assert_is_not_none
76from ._helpers import get_contribution
87
98
10- # POST /chartlets/callback
119def get_callback_results (
12- ext_ctx : ExtensionContext | None , data : dict [str , Any ]
10+ ext_ctx : ExtensionContext , data : dict [str , Any ]
1311) -> Response :
1412 """Generate the response for the endpoint `POST /chartlets/callback`.
1513
1614 Args:
17- ext_ctx: Extension context. If `None`,
18- the function returns a 404 error response.
15+ ext_ctx: Extension context.
1916 data: A dictionary deserialized from a request JSON body
2017 that should contain a key `callbackRequests` of type `list`.
2118 Returns:
2219 A `Response` object.
2320 On success, the response is a list of state-change requests
2421 grouped by contributions.
2522 """
26- assert_is_not_none ("ext_ctx" , ext_ctx )
23+ assert_is_instance_of ("ext_ctx" , ext_ctx , ExtensionContext )
2724 assert_is_instance_of ("data" , data , dict )
2825
2926 # TODO: validate data
3027 callback_requests : list [dict ] = data .get ("callbackRequests" ) or []
3128
32- # TODO: assert correctness, set status code on error
3329 state_change_requests : list [dict [str , Any ]] = []
3430 for callback_request in callback_requests :
3531 contrib_point_name : str = callback_request ["contribPoint" ]
@@ -80,6 +76,7 @@ def get_callback_results(
8076 }
8177 )
8278
79+ # find an existing state change request
8380 existing_scr : dict [str , Any ] | None = None
8481 for scr in state_change_requests :
8582 if (
@@ -88,9 +85,12 @@ def get_callback_results(
8885 ):
8986 existing_scr = scr
9087 break
88+
9189 if existing_scr is not None :
90+ # merge with existing state change request
9291 existing_scr ["stateChanges" ].extend (state_changes )
9392 else :
93+ # append new state change request
9494 state_change_requests .append (
9595 {
9696 "contribPoint" : contrib_point_name ,
0 commit comments