@@ -65,19 +65,24 @@ async def init():
6565# Get all customers
6666@app .route ('/api/customers' , methods = ['GET' ])
6767async def get_customers ():
68- customers_list : List [customers ] = []
68+ customers_list : List [Customer ] = []
6969 async for customer in await customers .values ():
7070 customers_list .append (customer )
7171
7272 return quart .Response (jsonpickle .encode (customers_list , unpicklable = False ), mimetype = "application/json" )
7373
74+
7475# Create a person with JSON as body
7576@app .route ('/api/customers' , methods = ['POST' ])
7677async def create_customer ():
77- data = await request .get_json (force = True )
78- name : str = data ['name' ]
79- id : int = data ['id' ]
80- balance : float = data ['balance' ]
78+ try :
79+ data = await request .get_json (force = True )
80+ name : str = data ['name' ]
81+ id : int = int (data ['id' ])
82+ balance : float = float (data ['balance' ])
83+ except :
84+ return quart .Response (f"Invalid JSON" , status = 400 )
85+
8186 person : Customer = Customer (id , name , balance )
8287 await customers .put (person .id , person )
8388
@@ -87,6 +92,7 @@ async def create_customer():
8792 mimetype = 'application/json'
8893 )
8994
95+
9096# Get a single person
9197@app .route ('/api/customers/<id>' , methods = ['GET' ])
9298async def get_person (id : str ):
@@ -118,7 +124,7 @@ def handle_event(e) -> None:
118124 oldValue = e .old
119125
120126 print (
121- f"Event { e .type } for key={ key } , new=$ { newValue } , old=$ { oldValue } " )
127+ f"Event { e .type } for key={ key } , new={ newValue } , old={ oldValue } " )
122128
123129
124130def handle_delete_event (e ) -> None :
@@ -131,7 +137,7 @@ def handle_delete_event(e) -> None:
131137 oldValue = e .old
132138
133139 print (
134- f"Event delete large balance for key={ key } , old=$ { oldValue } " )
140+ f"Event delete large balance for key={ key } , old={ oldValue } " )
135141
136142
137143# ----- main ----------------------------------------------------------------
0 commit comments