from chalice import Chalice from chalicelib import db app = Chalice(app_name='mytodo') app.debug = True _DB = None def get_app_db(): global _DB if _DB is None: _DB = db.InMemoryTodoDB() return _DB @app.route('/todos', methods=['GET']) def get_todos(): return get_app_db().list_items() @app.route('/todos', methods=['POST']) def add_new_todo(): body = app.current_request.json_body return get_app_db().add_item( description=body['description'], metadata=body.get('metadata'), ) @app.route('/todos/{uid}', methods=['GET']) def get_todo(uid): return get_app_db().get_item(uid) @app.route('/todos/{uid}', methods=['DELETE']) def delete_todo(uid): return get_app_db().delete_item(uid) @app.route('/todos/{uid}', methods=['PUT']) def update_todo(uid): body = app.current_request.json_body get_app_db().update_item( uid, description=body.get('description'), state=body.get('state'), metadata=body.get('metadata'))