1
+ from http import HTTPStatus
1
2
from typing import Annotated , Any
2
3
3
4
from aws_lambda_env_modeler import get_environment_variables , init_environment_variables
4
- from aws_lambda_powertools .event_handler .openapi .params import Path
5
+ from aws_lambda_powertools .event_handler import Response , content_types
6
+ from aws_lambda_powertools .event_handler .openapi .params import Body
5
7
from aws_lambda_powertools .logging import correlation_paths
6
8
from aws_lambda_powertools .metrics import MetricUnit
7
9
from aws_lambda_powertools .utilities .typing import LambdaContext
8
10
9
- from service .handlers .models .dynamic_configuration import MyConfiguration
10
11
from service .handlers .models .env_vars import MyHandlerEnvVars
11
- from service .handlers .utils .dynamic_configuration import parse_configuration
12
12
from service .handlers .utils .observability import logger , metrics , tracer
13
13
from service .handlers .utils .rest_api_resolver import ORDERS_PATH , app
14
14
from service .logic .delete_order import delete_order
15
+ from service .models .exceptions import OrderNotFoundException
15
16
from service .models .input import DeleteOrderRequest
16
- from service .models .output import DeleteOrderOutput , InternalServerErrorOutput
17
+ from service .models .output import DeleteOrderOutput , InternalServerErrorOutput , OrderNotFoundOutput
17
18
18
19
19
- @app .delete (
20
- ORDERS_PATH + '{order_id}' ,
20
+ @app .post (
21
+ f" { ORDERS_PATH } delete" ,
21
22
summary = 'Delete an order' ,
22
- description = 'Delete an order identified by the order_id ' ,
23
- response_description = 'The deleted order' ,
23
+ description = 'Delete an order identified by the provided order ID ' ,
24
+ response_description = 'The deleted order details ' ,
24
25
responses = {
25
26
200 : {
26
27
'description' : 'The deleted order' ,
27
28
'content' : {'application/json' : {'model' : DeleteOrderOutput }},
28
29
},
30
+ 404 : {
31
+ 'description' : 'Order not found' ,
32
+ 'content' : {'application/json' : {'model' : OrderNotFoundOutput }},
33
+ },
29
34
501 : {
30
35
'description' : 'Internal server error' ,
31
36
'content' : {'application/json' : {'model' : InternalServerErrorOutput }},
32
37
},
33
38
},
34
39
tags = ['CRUD' ],
35
40
)
36
- def handle_delete_order (order_id : Annotated [str , Path ( description = "The ID of the order to delete" )]) -> DeleteOrderOutput :
41
+ def handle_delete_order (delete_input : Annotated [DeleteOrderRequest , Body ( embed = False , media_type = 'application/json' )]) -> DeleteOrderOutput :
37
42
env_vars : MyHandlerEnvVars = get_environment_variables (model = MyHandlerEnvVars )
38
43
logger .debug ('environment variables' , env_vars = env_vars .model_dump ())
39
- logger .info ('got delete order request' , order_id = order_id )
40
-
41
- my_configuration = parse_configuration (model = MyConfiguration )
42
- logger .debug ('fetched dynamic configuration' , configuration = my_configuration .model_dump ())
44
+ logger .info ('got delete order request' , order_id = delete_input .order_id )
43
45
44
46
metrics .add_metric (name = 'ValidDeleteOrderEvents' , unit = MetricUnit .Count , value = 1 )
45
-
46
- delete_request = DeleteOrderRequest (order_id = order_id )
47
-
48
47
response : DeleteOrderOutput = delete_order (
49
- delete_request = delete_request ,
48
+ delete_request = delete_input ,
50
49
table_name = env_vars .TABLE_NAME ,
51
50
context = app .lambda_context ,
52
51
)
@@ -55,6 +54,14 @@ def handle_delete_order(order_id: Annotated[str, Path(description="The ID of the
55
54
return response
56
55
57
56
57
+ @app .exception_handler (OrderNotFoundException )
58
+ def handle_order_not_found_error (ex : OrderNotFoundException ):
59
+ logger .exception ('order not found' )
60
+ return Response (
61
+ status_code = HTTPStatus .NOT_FOUND , content_type = content_types .APPLICATION_JSON , body = OrderNotFoundOutput ().model_dump ()
62
+ )
63
+
64
+
58
65
@init_environment_variables (model = MyHandlerEnvVars )
59
66
@logger .inject_lambda_context (correlation_id_path = correlation_paths .API_GATEWAY_REST )
60
67
@metrics .log_metrics
0 commit comments