From 35402bbf500901b3915052aad80afd021ae390ff Mon Sep 17 00:00:00 2001 From: Amirhomayoon Date: Thu, 13 Apr 2023 07:42:33 +0330 Subject: [PATCH] Improving The REST API With Quart and quart-cors The updated code includes several improvements, including the use of type hints for better readability and maintainability, the use of jsonify method for easier JSON serialization, and the use of context managers to automatically close files. Unused variables were also removed and the get_todos method was simplified using a boolean expression. These changes help make the code more efficient, readable, and maintainable. --- main.py | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/main.py b/main.py index a408731a..8e37f0bc 100644 --- a/main.py +++ b/main.py @@ -1,55 +1,54 @@ import json +from typing import Dict, List import quart import quart_cors -from quart import request +from quart import Quart, Response, jsonify, request -app = quart_cors.cors(quart.Quart(__name__), allow_origin="https://chat.openai.com") +app = quart_cors.cors(Quart(__name__), allow_origin="https://chat.openai.com") # Keep track of todo's. Does not persist if Python session is restarted. -_TODOS = {} +_TODOS: Dict[str, List[str]] = {} @app.post("/todos/") -async def add_todo(username): - request = await quart.request.get_json(force=True) +async def add_todo(username: str) -> Response: + data = await request.get_json(force=True) if username not in _TODOS: _TODOS[username] = [] - _TODOS[username].append(request["todo"]) - return quart.Response(response='OK', status=200) + _TODOS[username].append(data["todo"]) + return Response(response='OK', status=200) @app.get("/todos/") -async def get_todos(username): - return quart.Response(response=json.dumps(_TODOS.get(username, [])), status=200) +async def get_todos(username: str) -> Response: + return jsonify(_TODOS.get(username) or []) @app.delete("/todos/") -async def delete_todo(username): - request = await quart.request.get_json(force=True) - todo_idx = request["todo_idx"] +async def delete_todo(username: str) -> Response: + data = await request.get_json(force=True) + todo_idx = data["todo_idx"] # fail silently, it's a simple plugin - if 0 <= todo_idx < len(_TODOS[username]): + if 0 <= todo_idx < len(_TODOS.get(username, [])): _TODOS[username].pop(todo_idx) - return quart.Response(response='OK', status=200) + return Response(response='OK', status=200) @app.get("/logo.png") -async def plugin_logo(): +async def plugin_logo() -> Response: filename = 'logo.png' return await quart.send_file(filename, mimetype='image/png') @app.get("/.well-known/ai-plugin.json") -async def plugin_manifest(): - host = request.headers['Host'] +async def plugin_manifest() -> Response: with open("./.well-known/ai-plugin.json") as f: text = f.read() - return quart.Response(text, mimetype="text/json") + return Response(text, mimetype="text/json") @app.get("/openapi.yaml") -async def openapi_spec(): - host = request.headers['Host'] +async def openapi_spec() -> Response: with open("openapi.yaml") as f: text = f.read() - return quart.Response(text, mimetype="text/yaml") + return Response(text, mimetype="text/yaml") -def main(): +def main() -> None: app.run(debug=True, host="0.0.0.0", port=5003) if __name__ == "__main__":