aletrn commited on
Commit
150cbc9
·
1 Parent(s): 28ea6eb

[refactor] remove JSONResponse

Browse files
Files changed (1) hide show
  1. src/app.py +17 -7
src/app.py CHANGED
@@ -2,6 +2,7 @@ import json
2
  import time
3
  from http import HTTPStatus
4
  from aws_lambda_powertools import Logger
 
5
  from aws_lambda_powertools.utilities.typing import LambdaContext
6
  from pydantic import BaseModel, ValidationError
7
 
@@ -16,7 +17,9 @@ logger = Logger()
16
  class BBoxWithPointInput(BaseModel):
17
  bbox: input_floatlist
18
  points: input_floatlist2
 
19
  message: str = ""
 
20
 
21
 
22
  def get_response(status: int, start_time: float, request_id: str, output: BBoxWithPointInput = None) -> str:
@@ -34,15 +37,22 @@ def get_response(status: int, start_time: float, request_id: str, output: BBoxWi
34
 
35
  """
36
  duration_run = time.time() - start_time
37
- if output and status == 200:
38
- output.message = f"{CUSTOM_RESPONSE_MESSAGES[status]} - duration_run: {duration_run}, request_id: {request_id}."
39
- return output.model_dump_json()
 
 
 
40
  elif status == 200:
41
- raise KeyError("missing BBoxWithPointInput...")
42
- return json.dumps({
 
43
  "statusCode": status,
44
- "message": f"{CUSTOM_RESPONSE_MESSAGES[status]} - duration_run: {duration_run}, request_id: {request_id}."
45
- })
 
 
 
46
 
47
 
48
  def lambda_handler(event: dict, context: LambdaContext):
 
2
  import time
3
  from http import HTTPStatus
4
  from aws_lambda_powertools import Logger
5
+ from aws_lambda_powertools.event_handler import content_types
6
  from aws_lambda_powertools.utilities.typing import LambdaContext
7
  from pydantic import BaseModel, ValidationError
8
 
 
17
  class BBoxWithPointInput(BaseModel):
18
  bbox: input_floatlist
19
  points: input_floatlist2
20
+ duration_run: float = 0
21
  message: str = ""
22
+ request_id: str = ""
23
 
24
 
25
  def get_response(status: int, start_time: float, request_id: str, output: BBoxWithPointInput = None) -> str:
 
37
 
38
  """
39
  duration_run = time.time() - start_time
40
+ body = f"{CUSTOM_RESPONSE_MESSAGES[status]} - duration_run: {duration_run}, request_id: {request_id}."
41
+ if output is not None and status == 200:
42
+ output.duration_run = duration_run
43
+ output.message = CUSTOM_RESPONSE_MESSAGES[status]
44
+ output.request_id = request_id
45
+ body = output.model_dump_json()
46
  elif status == 200:
47
+ # should never be here...
48
+ raise KeyError("status 200, but missing BBoxWithPointInput argument.")
49
+ response = {
50
  "statusCode": status,
51
+ "Content-Type": content_types.APPLICATION_JSON if status == 200 else content_types.TEXT_PLAIN,
52
+ "body": body
53
+ }
54
+ logger.info(f"response type:{type(response)} => {response}.")
55
+ return json.dumps(response)
56
 
57
 
58
  def lambda_handler(event: dict, context: LambdaContext):