Skip to content

Commit 35358ed

Browse files
author
Michael Brewer
committed
refactor(trigger): Split decompress and parse
For handling CloudWatchLogsEvent log data split the Decode and decompress from the parse as CloudWatchLogsDecodedData
1 parent 3c41f36 commit 35358ed

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

aws_lambda_powertools/utilities/trigger/cloud_watch_logs_event.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,12 @@ def aws_logs_data(self) -> str:
8383
"""The value of the `data` field is a Base64 encoded ZIP archive."""
8484
return self["awslogs"]["data"]
8585

86-
def decode_cloud_watch_logs_data(self) -> CloudWatchLogsDecodedData:
87-
"""Decode, unzip and parse json data"""
86+
@property
87+
def decompress_logs_data(self) -> bytes:
88+
"""Decode and decompress log data"""
8889
payload = base64.b64decode(self.aws_logs_data)
89-
decoded: dict = json.loads(zlib.decompress(payload, zlib.MAX_WBITS | 32).decode("UTF-8"))
90-
return CloudWatchLogsDecodedData(decoded)
90+
return zlib.decompress(payload, zlib.MAX_WBITS | 32)
91+
92+
def parse_logs_data(self) -> CloudWatchLogsDecodedData:
93+
"""Decode, decompress and parse json data as CloudWatchLogsDecodedData"""
94+
return CloudWatchLogsDecodedData(json.loads(self.decompress_logs_data.decode("UTF-8")))

tests/functional/test_lambda_trigger_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def load_event(file_name: str) -> dict:
4242
def test_cloud_watch_trigger_event():
4343
event = CloudWatchLogsEvent(load_event("cloudWatchLogEvent.json"))
4444

45-
decoded_data = event.decode_cloud_watch_logs_data()
45+
decoded_data = event.parse_logs_data()
4646
log_events = decoded_data.log_events
4747
log_event = log_events[0]
4848

0 commit comments

Comments
 (0)