@@ -138,9 +138,26 @@ def build_uri(self, event, payload_format_version_1_0):
138
138
uri = uri + '?' + event ['rawQueryString' ]
139
139
return uri
140
140
141
+ def base64_body (cls , data ):
142
+ """Function to transfer body into base64 encoded"""
143
+ body = base64 .b64encode (str (data ).encode ("utf-8" ))
144
+ if isinstance (body , str ):
145
+ return str (body ).encode ("utf-8" ), 'base64'
146
+ elif isinstance (body , (bytes , bytearray )):
147
+ return str (body , "utf-8" ), 'base64'
148
+ else :
149
+ return str (body ), 'base64'
150
+
141
151
def process_body (self , body_wrapper ):
142
152
"""Function to process body"""
143
- if not (self .LOG_BODY and body_wrapper .get ('body' )):
153
+
154
+ if self .LOG_BODY and isinstance (body_wrapper , dict ) and 'body' not in body_wrapper :
155
+ return body_wrapper , 'json'
156
+
157
+ if self .LOG_BODY and not isinstance (body_wrapper , dict ) and 'body' not in body_wrapper and isinstance (body_wrapper , str ):
158
+ return self .base64_body (body_wrapper )
159
+
160
+ if not (self .LOG_BODY and isinstance (body_wrapper , dict ) and body_wrapper .get ('body' )):
144
161
return None , 'json'
145
162
146
163
body = None
@@ -156,13 +173,7 @@ def process_body(self, body_wrapper):
156
173
body = body_wrapper .get ('body' )
157
174
transfer_encoding = 'json'
158
175
except Exception as e :
159
- body = base64 .b64encode (str (body_wrapper ['body' ]).encode ("utf-8" ))
160
- if isinstance (body , str ):
161
- return str (body ).encode ("utf-8" ), 'base64'
162
- elif isinstance (body , (bytes , bytearray )):
163
- return str (body , "utf-8" ), 'base64'
164
- else :
165
- return str (body ), 'base64'
176
+ return self .base64_body (body_wrapper ['body' ])
166
177
return body , transfer_encoding
167
178
168
179
def before (self , event , context ):
@@ -308,8 +319,8 @@ def after(self, retval):
308
319
309
320
# Event Response object
310
321
event_rsp = EventResponseModel (time = datetime .utcnow ().strftime ("%Y-%m-%dT%H:%M:%S.%f" )[:- 3 ],
311
- status = retval .get ('statusCode' , 599 ),
312
- headers = retval .get ('headers' , {}),
322
+ status = retval .get ('statusCode' , 599 ) if 'statusCode' in retval else 200 ,
323
+ headers = retval .get ('headers' , {}) if 'headers' in retval else { "content-type" : "application/json" } ,
313
324
body = resp_body ,
314
325
transfer_encoding = resp_transfer_encoding )
315
326
0 commit comments