@@ -48,8 +48,8 @@ class KinesisFirehoseResponseRecord:
48
48
metadata : Optional [KinesisFirehoseResponseRecordMetadata ] = None
49
49
"""Json data for caching json.dump result"""
50
50
_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
53
53
54
54
def data_from_byte (self , data : bytes ):
55
55
"""Populate data field using a byte like data"""
@@ -59,16 +59,13 @@ def data_from_text(self, data: str):
59
59
"""Populate data field using a string like data"""
60
60
self .data_from_byte (data .encode ("utf-8" ))
61
61
62
- def data_from_json (
63
- self ,
64
- data : Any ,
65
- ):
62
+ def data_from_json (self , data : Any ):
66
63
"""Populate data field using any structure that could be converted to json"""
67
64
self .data_from_text (data = self .json_serializer (data ))
68
65
69
66
@property
70
67
def asdict (self ) -> Dict :
71
- r = {
68
+ r : Dict [ str , Any ] = {
72
69
"recordId" : self .record_id ,
73
70
"result" : self .result ,
74
71
"data" : self .data ,
@@ -78,18 +75,24 @@ def asdict(self) -> Dict:
78
75
return r
79
76
80
77
@property
81
- def data_as_bytes (self ) -> bytes :
78
+ def data_as_bytes (self ) -> Optional [ bytes ] :
82
79
"""Decoded base64-encoded data as bytes"""
80
+ if not self .data :
81
+ return None
83
82
return base64 .b64decode (self .data )
84
83
85
84
@property
86
- def data_as_text (self ) -> str :
85
+ def data_as_text (self ) -> Optional [ str ] :
87
86
"""Decoded base64-encoded data as text"""
87
+ if not self .data_as_bytes :
88
+ return None
88
89
return self .data_as_bytes .decode ("utf-8" )
89
90
90
91
@property
91
- def data_as_json (self ) -> Dict :
92
+ def data_as_json (self ) -> Optional [ Dict ] :
92
93
"""Decoded base64-encoded data loaded to json"""
94
+ if not self .data_as_text :
95
+ return None
93
96
if self ._json_data is None :
94
97
self ._json_data = self .json_deserializer (self .data_as_text )
95
98
return self ._json_data
@@ -114,6 +117,8 @@ def add_record(self, record: KinesisFirehoseResponseRecord):
114
117
115
118
@property
116
119
def asdict (self ) -> Dict :
120
+ if not self .records :
121
+ return {}
117
122
return {"records" : [r .asdict for r in self .records ]}
118
123
119
124
@@ -193,7 +198,7 @@ def data_as_json(self) -> dict:
193
198
def create_firehose_response_record (
194
199
self ,
195
200
result : Literal ["Ok" , "Dropped" , "ProcessingFailed" ],
196
- data : str = None ,
201
+ data : Optional [ str ] = None ,
197
202
) -> KinesisFirehoseResponseRecord :
198
203
return KinesisFirehoseResponseRecord (record_id = self .record_id , result = result , data = data )
199
204
0 commit comments