1
- import uuid
2
-
3
- import boto3
4
- from botocore .exceptions import ClientError
5
- from cachetools import TTLCache , cached
6
- from mypy_boto3_dynamodb import DynamoDBServiceResource
7
- from mypy_boto3_dynamodb .service_resource import Table
8
-
1
+ from service .dal .db_handler import create_order_in_db
2
+ from service .dal .schemas .db import OrderEntry
9
3
from service .handlers .schemas .dynamic_configuration import FeatureFlagsNames
10
4
from service .handlers .utils .dynamic_configuration import get_dynamic_configuration_store
11
5
from service .handlers .utils .observability import logger , tracer
12
- from service .schemas .exceptions import InternalServerException
13
- from service .schemas .output import Output
6
+ from service .schemas .output import CreateOrderOutput
14
7
15
8
16
9
@tracer .capture_method (capture_response = False )
17
- def handle_create_request (customer_name : str , order_item_count : int , table_name : str ) -> Output :
10
+ def handle_create_request (customer_name : str , order_item_count : int , table_name : str ) -> CreateOrderOutput :
18
11
logger .info ('starting to handle create request' , extra = {'order_item_count' : order_item_count , 'customer_name' : customer_name })
19
12
20
13
# feature flags example
@@ -32,26 +25,6 @@ def handle_create_request(customer_name: str, order_item_count: int, table_name:
32
25
)
33
26
logger .debug ('premium feature flag value' , extra = {'premium' : premium })
34
27
35
- return _create_order_in_db (table_name , customer_name , order_item_count )
36
-
37
-
38
- # cache dynamodb connection data for no longer than 5 minutes
39
- @cached (cache = TTLCache (maxsize = 1 , ttl = 300 ))
40
- def _get_db_handler (table_name : str ) -> Table :
41
- dynamodb : DynamoDBServiceResource = boto3 .resource ('dynamodb' )
42
- logger .info ('opening connection to dynamodb table' , extra = {'table_name' : table_name })
43
- return dynamodb .Table (table_name )
44
-
45
-
46
- def _create_order_in_db (table_name : str , customer_name : str , order_item_count : int ):
47
- order_id = str (uuid .uuid4 ())
48
- try :
49
- table : Table = _get_db_handler (table_name )
50
- table .put_item (Item = {'order_id' : order_id , 'customer_name' : customer_name , 'count' : order_item_count })
51
- except ClientError as exc :
52
- error_msg = 'failed to create order'
53
- logger .exception (error_msg , extra = {'exception' : str (exc ), 'customer_name' : customer_name })
54
- raise InternalServerException (error_msg ) from exc
55
-
56
- logger .info ('finished create order' , extra = {'order_id' : order_id , 'order_item_count' : order_item_count , 'customer_name' : customer_name })
57
- return Output (customer_name = customer_name , order_item_count = order_item_count , order_id = order_id )
28
+ order : OrderEntry = create_order_in_db (table_name , customer_name , order_item_count )
29
+ # convert from db entry to output, they won't always be the same
30
+ return CreateOrderOutput (customer_name = order .customer_name , order_item_count = order .order_item_count , order_id = order .order_id )
0 commit comments