Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving The REST API With Quart and quart-cors #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions main.py
Original file line number Diff line number Diff line change
@@ -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/<string:username>")
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/<string:username>")
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/<string:username>")
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__":
Expand Down