From 47bd8c2fdd81dda2c51ccaefd61d066f0f7d8451 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 17:31:38 +0200 Subject: [PATCH] Added basic bettwaage endpoints --- src/endpoints/bettwaage.py | 31 +++++++++++++++++++++++++++++++ src/main.py | 2 ++ 2 files changed, 33 insertions(+) create mode 100644 src/endpoints/bettwaage.py diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py new file mode 100644 index 0000000..4975b53 --- /dev/null +++ b/src/endpoints/bettwaage.py @@ -0,0 +1,31 @@ +from fastapi import APIRouter + +from datetime import datetime +from os.path import exists + +from fastapi.responses import HTMLResponse + +router = APIRouter() + +file_path = "bettwaage.csv" + + +def add_line_to_history(line: str) -> None: + with open(file_path, "+a", encoding="UTF-8") as fp: + fp.writelines([line]) + + +@router.get("/file") +async def get_file(): + with open(file_path, "r", encoding="UTF-8") as fp: + return HTMLResponse("\n".join(fp.readlines())) + + +@router.post("/add") +async def add_weight(tl: int, tr: int, bl: int, br: int): + sum = tl + tr + bl + br + add_line_to_history(f"{str(datetime.now())};{";".join([tl, tr, bl, br, sum])};") + + +if not exists(file_path): + add_line_to_history("timestamp;tl;tr;bl;br;total;") diff --git a/src/main.py b/src/main.py index f88263b..39c4267 100644 --- a/src/main.py +++ b/src/main.py @@ -1,9 +1,11 @@ from fastapi import FastAPI from endpoints.hue import router as hue_router +from endpoints.bettwaage import router as bettwaage_router app = FastAPI() app.include_router(hue_router, prefix="/hue", tags=["hue"]) +app.include_router(bettwaage_router, prefix="/bettwaage") if __name__ == "__main__": app.run()