[refactor] prepare openapi swagger: rename RawRequestInput type to ApiRequestBody, add ApiResponseBodyFailure and ApiResponseBodySuccess types
Browse files- src/io/lambda_helpers.py +5 -5
- src/utilities/type_hints.py +13 -1
src/io/lambda_helpers.py
CHANGED
|
@@ -6,7 +6,7 @@ from xyzservices import providers
|
|
| 6 |
from src import app_logger
|
| 7 |
from src.io.coordinates_pixel_conversion import get_latlng_to_pixel_coordinates
|
| 8 |
from src.utilities.constants import CUSTOM_RESPONSE_MESSAGES
|
| 9 |
-
from src.utilities.type_hints import
|
| 10 |
from src.utilities.utilities import base64_decode
|
| 11 |
|
| 12 |
|
|
@@ -42,7 +42,7 @@ def get_response(status: int, start_time: float, request_id: str, response_body:
|
|
| 42 |
return dumps(response)
|
| 43 |
|
| 44 |
|
| 45 |
-
def get_parsed_bbox_points(request_input:
|
| 46 |
"""
|
| 47 |
Parse the raw input request into bbox, prompt and zoom
|
| 48 |
|
|
@@ -88,7 +88,7 @@ def _get_parsed_prompt_list(bbox_ne, bbox_sw, zoom, prompt_list):
|
|
| 88 |
elif prompt.type == "rectangle":
|
| 89 |
new_prompt_data = _get_new_prompt_data_rectangle(bbox_ne, bbox_sw, prompt, zoom)
|
| 90 |
else:
|
| 91 |
-
msg = "Valid prompt type: 'point' or 'rectangle', not '{}'. Check
|
| 92 |
raise TypeError(msg.format(prompt.type))
|
| 93 |
app_logger.debug(f"new_prompt_data: {type(new_prompt_data)}, value:{new_prompt_data}.")
|
| 94 |
new_prompt["data"] = new_prompt_data
|
|
@@ -118,7 +118,7 @@ def _get_new_prompt_data_rectangle(bbox_ne, bbox_sw, prompt, zoom):
|
|
| 118 |
]
|
| 119 |
|
| 120 |
|
| 121 |
-
def get_parsed_request_body(event: Dict or str) ->
|
| 122 |
"""
|
| 123 |
Validator for the raw input request lambda event
|
| 124 |
|
|
@@ -144,7 +144,7 @@ def get_parsed_request_body(event: Dict or str) -> RawRequestInput:
|
|
| 144 |
raw_body = loads(body_decoded_str)
|
| 145 |
app_logger.info(f"body, #2: {type(raw_body)}, {raw_body}...")
|
| 146 |
|
| 147 |
-
parsed_body =
|
| 148 |
log_level = "DEBUG" if parsed_body.debug else "INFO"
|
| 149 |
app_logger.setLevel(log_level)
|
| 150 |
app_logger.warning(f"set log level to {getLevelName(app_logger.log_level)}.")
|
|
|
|
| 6 |
from src import app_logger
|
| 7 |
from src.io.coordinates_pixel_conversion import get_latlng_to_pixel_coordinates
|
| 8 |
from src.utilities.constants import CUSTOM_RESPONSE_MESSAGES
|
| 9 |
+
from src.utilities.type_hints import ApiRequestBody
|
| 10 |
from src.utilities.utilities import base64_decode
|
| 11 |
|
| 12 |
|
|
|
|
| 42 |
return dumps(response)
|
| 43 |
|
| 44 |
|
| 45 |
+
def get_parsed_bbox_points(request_input: ApiRequestBody) -> Dict:
|
| 46 |
"""
|
| 47 |
Parse the raw input request into bbox, prompt and zoom
|
| 48 |
|
|
|
|
| 88 |
elif prompt.type == "rectangle":
|
| 89 |
new_prompt_data = _get_new_prompt_data_rectangle(bbox_ne, bbox_sw, prompt, zoom)
|
| 90 |
else:
|
| 91 |
+
msg = "Valid prompt type: 'point' or 'rectangle', not '{}'. Check ApiRequestBody parsing/validation."
|
| 92 |
raise TypeError(msg.format(prompt.type))
|
| 93 |
app_logger.debug(f"new_prompt_data: {type(new_prompt_data)}, value:{new_prompt_data}.")
|
| 94 |
new_prompt["data"] = new_prompt_data
|
|
|
|
| 118 |
]
|
| 119 |
|
| 120 |
|
| 121 |
+
def get_parsed_request_body(event: Dict or str) -> ApiRequestBody:
|
| 122 |
"""
|
| 123 |
Validator for the raw input request lambda event
|
| 124 |
|
|
|
|
| 144 |
raw_body = loads(body_decoded_str)
|
| 145 |
app_logger.info(f"body, #2: {type(raw_body)}, {raw_body}...")
|
| 146 |
|
| 147 |
+
parsed_body = ApiRequestBody.model_validate(raw_body)
|
| 148 |
log_level = "DEBUG" if parsed_body.debug else "INFO"
|
| 149 |
app_logger.setLevel(log_level)
|
| 150 |
app_logger.warning(f"set log level to {getLevelName(app_logger.log_level)}.")
|
src/utilities/type_hints.py
CHANGED
|
@@ -75,7 +75,7 @@ class RawPromptRectangle(BaseModel):
|
|
| 75 |
return self.type
|
| 76 |
|
| 77 |
|
| 78 |
-
class
|
| 79 |
"""Input lambda request validator type (not yet parsed)"""
|
| 80 |
id: str = ""
|
| 81 |
bbox: RawBBox
|
|
@@ -84,3 +84,15 @@ class RawRequestInput(BaseModel):
|
|
| 84 |
source_type: str = "Satellite"
|
| 85 |
debug: bool = False
|
| 86 |
url_tile: str = DEFAULT_TMS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
return self.type
|
| 76 |
|
| 77 |
|
| 78 |
+
class ApiRequestBody(BaseModel):
|
| 79 |
"""Input lambda request validator type (not yet parsed)"""
|
| 80 |
id: str = ""
|
| 81 |
bbox: RawBBox
|
|
|
|
| 84 |
source_type: str = "Satellite"
|
| 85 |
debug: bool = False
|
| 86 |
url_tile: str = DEFAULT_TMS
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
class ApiResponseBodyFailure(BaseModel):
|
| 90 |
+
duration_run: float
|
| 91 |
+
message: str
|
| 92 |
+
request_id: str
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
class ApiResponseBodySuccess(ApiResponseBodyFailure):
|
| 96 |
+
n_predictions: int
|
| 97 |
+
geojson: str
|
| 98 |
+
n_shapes_geojson: int
|