@@ -41,11 +41,13 @@ def get_user_id(self, event, context):
4141 username = identify_user (event , context )
4242 else :
4343 try :
44- rc_identity_id = event ["requestContext" ]["identity" ]["cognitoIdentityId" ]
45- if rc_identity_id :
46- username = rc_identity_id
44+ if 'requestContext' in event and 'identity' in event ["requestContext" ] and 'cognitoIdentityId' in event ["requestContext" ]["identity" ]:
45+ rc_identity_id = event ["requestContext" ]["identity" ]["cognitoIdentityId" ]
46+ if rc_identity_id :
47+ username = rc_identity_id
4748 except :
48- print ("can not fetch apiKey from cognitoIdentityId event, setting userId to None." )
49+ if self .DEBUG :
50+ print ("can not fetch apiKey from cognitoIdentityId event, setting userId to None." )
4951 except Exception as e :
5052 if self .DEBUG :
5153 print ("can not execute identify_user function, please check moesif settings." )
@@ -70,11 +72,14 @@ def process_request_body(self, raw_request_body):
7072 req_body = None
7173 req_transfer_encoding = None
7274 try :
73- if raw_request_body ['isBase64Encoded' ]:
75+ if 'isBase64Encoded' in raw_request_body and 'body' in raw_request_body and raw_request_body ['isBase64Encoded' ] and raw_request_body [ 'body ' ]:
7476 req_body = raw_request_body ['body' ]
7577 req_transfer_encoding = 'base64'
7678 else :
77- req_body = json .dumps (raw_request_body ['body' ])
79+ if 'body' in raw_request_body and raw_request_body ['body' ] and isinstance (raw_request_body ['body' ], str ):
80+ req_body = json .dumps (json .loads (raw_request_body ['body' ]))
81+ else :
82+ req_body = json .dumps (raw_request_body ['body' ])
7883 req_transfer_encoding = 'json'
7984 except Exception as e :
8085 if self .DEBUG :
@@ -87,11 +92,14 @@ def process_response_body(self, raw_response_body):
8792 resp_body = None
8893 resp_transfer_encoding = None
8994 try :
90- if 'isBase64Encoded' in raw_response_body and raw_response_body ['isBase64Encoded' ]:
95+ if 'isBase64Encoded' in raw_response_body and 'body' in raw_response_body and raw_response_body ['isBase64Encoded' ] and raw_response_body [ 'body ' ]:
9196 resp_body = raw_response_body ['body' ]
9297 resp_transfer_encoding = 'base64'
9398 else :
94- resp_body = json .dumps (raw_response_body ['body' ])
99+ if 'body' in raw_response_body and raw_response_body ['body' ] and isinstance (raw_response_body ['body' ], str ):
100+ resp_body = json .dumps (json .loads (raw_response_body ['body' ]))
101+ else :
102+ resp_body = json .dumps (raw_response_body ['body' ])
95103 resp_transfer_encoding = 'json'
96104 except Exception as e :
97105 if self .DEBUG :
@@ -146,11 +154,12 @@ def before(self, event, context):
146154 self .metadata = get_meta (event , context )
147155 else :
148156 try :
149- self .metadata = {
150- 'trace_id' : context .aws_request_id ,
151- 'function_name' : context .function_name ,
152- 'request_context' : event ['requestContext' ]
153- }
157+ if context .aws_request_id and context .function_name and 'requestContext' in event :
158+ self .metadata = {
159+ 'trace_id' : str (context .aws_request_id ),
160+ 'function_name' : context .function_name ,
161+ 'request_context' : event ['requestContext' ]
162+ }
154163 except :
155164 if self .DEBUG :
156165 print ("can not fetch default function_name and request_context from aws context, setting metadata to None." )
@@ -172,9 +181,10 @@ def before(self, event, context):
172181 self .session_token = get_token (event , context )
173182 else :
174183 try :
175- rc_api_key = event ['requestContext' ]['identity' ]['apiKey' ]
176- if rc_api_key :
177- self .session_token = rc_api_key
184+ if 'requestContext' in event and 'identity' in event ['requestContext' ] and 'apiKey' in event ['requestContext' ]['identity' ] and event ['requestContext' ]['identity' ]['apiKey' ]:
185+ rc_api_key = event ['requestContext' ]['identity' ]['apiKey' ]
186+ if rc_api_key :
187+ self .session_token = rc_api_key
178188 except KeyError :
179189 if self .DEBUG :
180190 print ("can not fetch apiKey from aws event, setting session_token to None." )
@@ -191,10 +201,11 @@ def before(self, event, context):
191201 api_version = get_version (event , context )
192202 else :
193203 try :
194- api_version = context .function_version
204+ if context .function_version :
205+ api_version = context .function_version
195206 except KeyError :
196207 if self .DEBUG :
197- print ("can not fetch default function_version from aws context, setting to None." )
208+ print ("can not fetch default function_version from aws context, setting api_version to None." )
198209 except Exception as e :
199210 if self .DEBUG :
200211 print ("can not execute GET_API_VERSION function, please check moesif settings." )
@@ -203,7 +214,8 @@ def before(self, event, context):
203214 # IpAddress
204215 ip_address = None
205216 try :
206- ip_address = self .client_ip .get_client_address (event ['headers' ], event ['requestContext' ]['identity' ]['sourceIp' ])
217+ if 'headers' in event and 'requestContext' in event and 'identity' in event ['requestContext' ] and 'sourceIp' in event ['requestContext' ]['identity' ] and event ['requestContext' ]['identity' ]['sourceIp' ]:
218+ ip_address = self .client_ip .get_client_address (event ['headers' ], event ['requestContext' ]['identity' ]['sourceIp' ])
207219 except Exception as e :
208220 if self .DEBUG :
209221 print ("Error while fetching Client Ip address, setting Request Ip address to None." )
@@ -296,7 +308,7 @@ def after(self, retval):
296308 # Send event to Moesif
297309 event_send = self .api_client .create_event (event_model )
298310 if self .DEBUG :
299- print ('Event Send successfully' )
311+ print ('Event Sent successfully' )
300312
301313 # Send response
302314 return retval
0 commit comments