33import json
44import logging
55from json import JSONDecodeError
6- from typing import Any
6+ from typing import Any , Optional
77
88import botocore
99
1717)
1818from samcli .lib .schemas .schemas_api_caller import SchemasApiCaller
1919from samcli .lib .utils .cloudformation import CloudFormationResourceSummary
20+ from samcli .lib .utils .invocation_type import REQUEST_RESPONSE
2021from samcli .lib .utils .resources import AWS_LAMBDA_FUNCTION
2122
2223LAMBDA_TEST_EVENT_REGISTRY = "lambda-testevent-schemas"
@@ -142,7 +143,14 @@ def limit_versions(self, schema_name: str, registry_name: str = LAMBDA_TEST_EVEN
142143 )
143144 self ._api_caller .delete_version (registry_name , schema_name , version_to_delete )
144145
145- def add_event_to_schema (self , schema : str , event : str , event_name : str , replace_if_exists : bool = False ) -> str :
146+ def add_event_to_schema (
147+ self ,
148+ schema : str ,
149+ event : str ,
150+ event_name : str ,
151+ metadata : Optional [dict [str , str ]],
152+ replace_if_exists : bool = False ,
153+ ) -> str :
146154 """
147155 Adds an event to a schema
148156
@@ -176,6 +184,8 @@ def add_event_to_schema(self, schema: str, event: str, event_name: str, replace_
176184 schema_dict ["components" ]["examples" ][event_name ] = {
177185 "value" : event_dict ,
178186 }
187+ if metadata :
188+ schema_dict ["components" ]["examples" ][event_name ]["x-metadata" ] = metadata
179189
180190 schema = json .dumps (schema_dict )
181191
@@ -200,7 +210,7 @@ def get_event_from_schema(self, schema: str, event_name: str) -> Any:
200210 the name of the event to retrieve from the schema
201211 Returns
202212 -------
203- The event data of the event "event_name"
213+ The event data and metadata of the event "event_name"
204214 """
205215 try :
206216 schema_dict = json .loads (schema )
@@ -211,7 +221,7 @@ def get_event_from_schema(self, schema: str, event_name: str) -> Any:
211221 raise ResourceNotFound (f"Event { event_name } not found" )
212222
213223 # can use square brackets here since we know each of these subdicts exist
214- return existing_events [event_name ][ "value" ]
224+ return existing_events [event_name ]
215225
216226 except JSONDecodeError as ex :
217227 LOG .error ("Error decoding schema when getting event %s" , event_name )
@@ -263,6 +273,7 @@ def create_event(
263273 event_name : str ,
264274 function_resource : CloudFormationResourceSummary ,
265275 event_data : str ,
276+ invocation_type : str = REQUEST_RESPONSE ,
266277 force : bool = False ,
267278 registry_name : str = LAMBDA_TEST_EVENT_REGISTRY ,
268279 ) -> str :
@@ -277,6 +288,8 @@ def create_event(
277288 The function where the event will be created
278289 event_data: str
279290 The JSON data of the event to be created
291+ invocation_type: str
292+ The Lambda invocation type of the event to be created
280293 registry_name: str
281294 The name of the registry that contains the schema
282295 """
@@ -287,16 +300,19 @@ def create_event(
287300
288301 schema_name = self ._get_schema_name (function_resource )
289302 original_schema = api_caller .get_schema (registry_name , schema_name )
303+ metadata = {"invocationType" : invocation_type }
290304
291305 if original_schema :
292306 LOG .debug ("Schema %s already exists, adding event %s" , schema_name , event_name )
293307 self .limit_versions (schema_name , registry_name )
294- schema = self .add_event_to_schema (original_schema , event_data , event_name , replace_if_exists = force )
308+ schema = self .add_event_to_schema (
309+ original_schema , event_data , event_name , metadata , replace_if_exists = force
310+ )
295311 api_caller .update_schema (schema , registry_name , schema_name , OPEN_API_TYPE )
296312 else :
297313 LOG .debug ("Schema %s does not already exist, creating new" , schema_name )
298314 schema = api_caller .discover_schema (event_data , OPEN_API_TYPE )
299- schema = self .add_event_to_schema (schema , event_data , event_name )
315+ schema = self .add_event_to_schema (schema , event_data , event_name , metadata )
300316 api_caller .create_schema (schema , registry_name , schema_name , OPEN_API_TYPE )
301317
302318 return schema
@@ -346,7 +362,7 @@ def get_event(
346362 event_name : str ,
347363 function_resource : CloudFormationResourceSummary ,
348364 registry_name : str = LAMBDA_TEST_EVENT_REGISTRY ,
349- ) -> str :
365+ ) -> Any :
350366 """
351367 Returns a remote test event
352368
@@ -360,7 +376,7 @@ def get_event(
360376 The name of the registry that contains the schema
361377 Returns
362378 -------
363- The JSON data of the event
379+ The parsed JSON data and metadata of the event
364380 """
365381 api_caller = self ._api_caller
366382
@@ -377,7 +393,7 @@ def get_event(
377393 event = self .get_event_from_schema (schema , event_name )
378394
379395 try :
380- return json .dumps (event )
396+ return { " json" : json .dumps (event [ "value" ]), "metadata" : event . get ( "x-metadata" , {})}
381397
382398 except JSONDecodeError as ex :
383399 LOG .error ("Error parsing event %s from schema %s" , event_name , schema_name )
0 commit comments