From bb780edb50555056308ec39ee4d36f64e076f0ac Mon Sep 17 00:00:00 2001 From: Vincent Lussenburg Date: Mon, 9 Jan 2023 11:16:35 -0800 Subject: [PATCH] Added Jobs and Attachments endpoint --- src/main.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/main.py b/src/main.py index 522799a..7251576 100644 --- a/src/main.py +++ b/src/main.py @@ -1,6 +1,7 @@ -from fastapi import FastAPI, Request, Response +from fastapi import FastAPI, Request, Response, HTTPException import os import sqlite3 +from os.path import isfile app = FastAPI() con = sqlite3.connect(':memory:') @@ -36,12 +37,33 @@ async def root(): return {"message": "Hello World"} -@app.get("/login") +@app.post("/login") async def login(email: str, password: str): cur = con.cursor() cur.execute("SELECT * FROM users WHERE email = '%s' and password = '%s'" % (email, password)) - return cur.fetchone() is not None + if not cur.fetchone(): + raise HTTPException(status_code=401, detail="Unknown username/password") + + return email; @app.get("/logout") async def root(email: str): return {"message": "Logged out %s!" % email} + +@app.get("/attachment") +async def attachment(attachment_name: str): + attachment_path = 'attachments/' + attachment_name + if not isfile(attachment_path): + raise HTTPException(status_code=404, detail="Attachment not found") + + with open(attachment_path) as f: + return f.readlines() + +@app.post("/job") +async def job(job_name: str): + job_path = 'jobs/' + job_name + if not isfile(job_path): + raise HTTPException(status_code=404, detail="Job not found") + + exec(open(job_path).read()) +