File tree Expand file tree Collapse file tree 2 files changed +8
-19
lines changed
aws_lambda_powertools/utilities/data_classes Expand file tree Collapse file tree 2 files changed +8
-19
lines changed Original file line number Diff line number Diff line change @@ -150,13 +150,16 @@ def body(self) -> Optional[str]:
150
150
@cached_property
151
151
def json_body (self ) -> Any :
152
152
"""Parses the submitted body as json"""
153
- return self ._json_deserializer (self .decoded_body )
153
+ if self .decoded_body :
154
+ return self ._json_deserializer (self .decoded_body )
155
+
156
+ return None
154
157
155
158
@cached_property
156
- def decoded_body (self ) -> str :
157
- """Dynamically base64 decode body as a str """
158
- body : str = self [ " body" ]
159
- if self .is_base64_encoded :
159
+ def decoded_body (self ) -> Optional [ str ] :
160
+ """Decode the body from base64 if encoded, otherwise return it as is. """
161
+ body : Optional [ str ] = self . body
162
+ if self .is_base64_encoded and body :
160
163
return base64 .b64decode (body .encode ()).decode ()
161
164
return body
162
165
Original file line number Diff line number Diff line change @@ -324,27 +324,13 @@ def test_base_proxy_event_get_header_value_case_insensitive():
324
324
assert value is None
325
325
326
326
327
- def test_base_proxy_event_json_body_key_error ():
328
- event = BaseProxyEvent ({})
329
- with pytest .raises (KeyError ) as ke :
330
- assert not event .json_body
331
- assert str (ke .value ) == "'body'"
332
-
333
-
334
327
def test_base_proxy_event_json_body ():
335
328
data = {"message" : "Foo" }
336
329
event = BaseProxyEvent ({"body" : json .dumps (data )})
337
330
assert event .json_body == data
338
331
assert event .json_body ["message" ] == "Foo"
339
332
340
333
341
- def test_base_proxy_event_decode_body_key_error ():
342
- event = BaseProxyEvent ({})
343
- with pytest .raises (KeyError ) as ke :
344
- assert not event .decoded_body
345
- assert str (ke .value ) == "'body'"
346
-
347
-
348
334
def test_base_proxy_event_decode_body_encoded_false ():
349
335
data = "Foo"
350
336
event = BaseProxyEvent ({"body" : data , "isBase64Encoded" : False })
You can’t perform that action at this time.
0 commit comments