aletrn commited on
Commit
da5737b
·
1 Parent(s): 9e6626c

[fix] try handling cases with b64encoded body

Browse files
Files changed (2) hide show
  1. src/app.py +19 -4
  2. src/utilities/utilities.py +14 -17
src/app.py CHANGED
@@ -7,7 +7,7 @@ from aws_lambda_powertools.utilities.typing import LambdaContext
7
  from pydantic import BaseModel, ValidationError
8
 
9
  from src.utilities.type_hints import input_floatlist, input_floatlist2
10
-
11
 
12
  logger = Logger()
13
 
@@ -59,11 +59,26 @@ def lambda_handler(event: dict, context: LambdaContext):
59
  logger.info(f"start with aws_request_id:{context.aws_request_id}.")
60
  start_time = time.time()
61
  try:
62
- logger.debug(f"event:{json.dumps(event)}...")
63
- logger.debug(f"context:{context}...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  try:
66
- bbox_points = BBoxWithPointInput(bbox=event["bbox"], points=event["points"])
67
  logger.info(f"validation ok, bbox_points:{bbox_points}...")
68
  response = get_response(HTTPStatus.OK.value, start_time, context.aws_request_id, bbox_points)
69
  except ValidationError as ve:
 
7
  from pydantic import BaseModel, ValidationError
8
 
9
  from src.utilities.type_hints import input_floatlist, input_floatlist2
10
+ from src.utilities.utilities import base64_decode
11
 
12
  logger = Logger()
13
 
 
59
  logger.info(f"start with aws_request_id:{context.aws_request_id}.")
60
  start_time = time.time()
61
  try:
62
+ logger.info(f"event:{json.dumps(event)}...")
63
+ logger.info(f"context:{context}...")
64
+
65
+ try:
66
+ body = event["body"]
67
+ except Exception as e_constants1:
68
+ logger.error(f"e_constants1:{e_constants1}.")
69
+ body = event
70
+
71
+ logger.info(f"body: {type(body)}, {body}...")
72
+
73
+ if isinstance(body, str):
74
+ body = base64_decode(body)
75
+ logger.info(f"body #2: {type(body)}, {body}...")
76
+ body = json.loads(body)
77
+
78
+ logger.info(f"body:{body}...")
79
 
80
  try:
81
+ bbox_points = BBoxWithPointInput(bbox=body["bbox"], points=body["points"])
82
  logger.info(f"validation ok, bbox_points:{bbox_points}...")
83
  response = get_response(HTTPStatus.OK.value, start_time, context.aws_request_id, bbox_points)
84
  except ValidationError as ve:
src/utilities/utilities.py CHANGED
@@ -4,26 +4,23 @@ import loguru
4
  from src.utilities.constants import ROOT
5
 
6
 
7
- def setup_logging(debug: bool = False, formatter: str = "{time} - {level} - ({extra[request_id]}) {message} ") -> loguru.logger:
8
- """
9
- Create a logging instance with log string formatter.
10
 
11
- Args:
12
- debug: logging debug argument
13
- formatter: log string formatter
 
 
14
 
15
- Returns:
16
- Logger
17
 
18
- """
19
- import sys
20
-
21
- logger = loguru.logger
22
- logger.remove()
23
- level_logger = "DEBUG" if debug else "INFO"
24
- logger.add(sys.stdout, format=formatter, level=level_logger)
25
- logger.debug(f"type_logger:{type(logger)}.")
26
- return logger
27
 
28
 
29
  def get_constants(event: dict, debug=False) -> dict:
 
4
  from src.utilities.constants import ROOT
5
 
6
 
7
+ def is_base64(s):
8
+ import base64
 
9
 
10
+ try:
11
+ return base64.b64encode(base64.b64decode(s, validate=True)) == s
12
+ except Exception as e:
13
+ print("e:", e, "#")
14
+ return False
15
 
 
 
16
 
17
+ def base64_decode(s):
18
+ import base64
19
+
20
+ if isinstance(s, str) and is_base64(s):
21
+ return base64.b64decode(s, validate=True).decode("utf-8")
22
+
23
+ return s
 
 
24
 
25
 
26
  def get_constants(event: dict, debug=False) -> dict: