Skip to content

Commit 776814c

Browse files
authored
Refactor: Add support for response interpretation for payload format v2 (#11)
Refactor: Add support for response interpretation for payload format v2 Refactor: Add install requires moesifapi and moesifpythonrequest libs Refactor: Bump version to 1.0.9
1 parent 81c896c commit 776814c

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

moesif_aws_lambda/middleware.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,26 @@ def build_uri(self, event, payload_format_version_1_0):
138138
uri = uri + '?' + event['rawQueryString']
139139
return uri
140140

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+
141151
def process_body(self, body_wrapper):
142152
"""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')):
144161
return None, 'json'
145162

146163
body = None
@@ -156,13 +173,7 @@ def process_body(self, body_wrapper):
156173
body = body_wrapper.get('body')
157174
transfer_encoding = 'json'
158175
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'])
166177
return body, transfer_encoding
167178

168179
def before(self, event, context):
@@ -308,8 +319,8 @@ def after(self, retval):
308319

309320
# Event Response object
310321
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" },
313324
body = resp_body,
314325
transfer_encoding = resp_transfer_encoding)
315326

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# Versions should comply with PEP440. For a discussion on single-sourcing
2929
# the version across setup.py and the project code, see
3030
# https://packaging.python.org/en/latest/single_source_version.html
31-
version='1.0.8',
31+
version='1.0.9',
3232

3333
description='Moesif Middleware to automatically log API calls from AWS Lambda functions',
3434
long_description=long_description,
@@ -84,7 +84,7 @@
8484
# your project is installed. For an analysis of "install_requires" vs pip's
8585
# requirements files see:
8686
# https://packaging.python.org/en/latest/requirements.html
87-
install_requires=['moesifapi', 'lambda_decorators', 'moesifpythonrequest'],
87+
install_requires=['moesifapi>=1.4.0', 'lambda_decorators', 'moesifpythonrequest>=0.2.0'],
8888

8989
# List additional groups of dependencies here (e.g. development
9090
# dependencies). You can install these using the following syntax,

0 commit comments

Comments
 (0)