Skip to content

Commit 2566f62

Browse files
committed
fix mypy
1 parent 5f55aa7 commit 2566f62

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

aws_lambda_powertools/utilities/data_classes/kinesis_firehose_event.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ class KinesisFirehoseResponseRecord:
4848
metadata: Optional[KinesisFirehoseResponseRecordMetadata] = None
4949
"""Json data for caching json.dump result"""
5050
_json_data: Optional[Any] = None
51-
json_serializer: Optional[Callable] = json.dumps
52-
json_deserializer: Optional[Callable] = json.loads
51+
json_serializer: Callable = json.dumps
52+
json_deserializer: Callable = json.loads
5353

5454
def data_from_byte(self, data: bytes):
5555
"""Populate data field using a byte like data"""
@@ -59,16 +59,13 @@ def data_from_text(self, data: str):
5959
"""Populate data field using a string like data"""
6060
self.data_from_byte(data.encode("utf-8"))
6161

62-
def data_from_json(
63-
self,
64-
data: Any,
65-
):
62+
def data_from_json(self, data: Any):
6663
"""Populate data field using any structure that could be converted to json"""
6764
self.data_from_text(data=self.json_serializer(data))
6865

6966
@property
7067
def asdict(self) -> Dict:
71-
r = {
68+
r: Dict[str, Any] = {
7269
"recordId": self.record_id,
7370
"result": self.result,
7471
"data": self.data,
@@ -78,18 +75,24 @@ def asdict(self) -> Dict:
7875
return r
7976

8077
@property
81-
def data_as_bytes(self) -> bytes:
78+
def data_as_bytes(self) -> Optional[bytes]:
8279
"""Decoded base64-encoded data as bytes"""
80+
if not self.data:
81+
return None
8382
return base64.b64decode(self.data)
8483

8584
@property
86-
def data_as_text(self) -> str:
85+
def data_as_text(self) -> Optional[str]:
8786
"""Decoded base64-encoded data as text"""
87+
if not self.data_as_bytes:
88+
return None
8889
return self.data_as_bytes.decode("utf-8")
8990

9091
@property
91-
def data_as_json(self) -> Dict:
92+
def data_as_json(self) -> Optional[Dict]:
9293
"""Decoded base64-encoded data loaded to json"""
94+
if not self.data_as_text:
95+
return None
9396
if self._json_data is None:
9497
self._json_data = self.json_deserializer(self.data_as_text)
9598
return self._json_data
@@ -114,6 +117,8 @@ def add_record(self, record: KinesisFirehoseResponseRecord):
114117

115118
@property
116119
def asdict(self) -> Dict:
120+
if not self.records:
121+
return {}
117122
return {"records": [r.asdict for r in self.records]}
118123

119124

@@ -193,7 +198,7 @@ def data_as_json(self) -> dict:
193198
def create_firehose_response_record(
194199
self,
195200
result: Literal["Ok", "Dropped", "ProcessingFailed"],
196-
data: str = None,
201+
data: Optional[str] = None,
197202
) -> KinesisFirehoseResponseRecord:
198203
return KinesisFirehoseResponseRecord(record_id=self.record_id, result=result, data=data)
199204

examples/event_sources/src/kinesis_firehose_delivery_stream.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import json
2-
31
from aws_lambda_powertools.utilities.data_classes import (
42
KinesisFirehoseEvent,
53
KinesisFirehoseResponse,
@@ -16,12 +14,10 @@ def lambda_handler(event: KinesisFirehoseEvent, context: LambdaContext):
1614
# if data was delivered as json; caches loaded value
1715
data = record.data_as_json
1816

19-
json_data = json.loads(data)
20-
2117
## do all kind of stuff with data
2218

2319
processed_record = record.create_firehose_response_record(result="Ok")
24-
processed_record.data_from_json(data=json_data)
20+
processed_record.data_from_json(data=data)
2521

2622
result.add_record(processed_record)
2723

0 commit comments

Comments
 (0)