Skip to content

Commit bb780ed

Browse files
committed
Added Jobs and Attachments endpoint
1 parent 69a1cdc commit bb780ed

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/main.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from fastapi import FastAPI, Request, Response
1+
from fastapi import FastAPI, Request, Response, HTTPException
22
import os
33
import sqlite3
4+
from os.path import isfile
45

56
app = FastAPI()
67
con = sqlite3.connect(':memory:')
@@ -36,12 +37,33 @@ async def root():
3637
return {"message": "Hello World"}
3738

3839

39-
@app.get("/login")
40+
@app.post("/login")
4041
async def login(email: str, password: str):
4142
cur = con.cursor()
4243
cur.execute("SELECT * FROM users WHERE email = '%s' and password = '%s'" % (email, password))
43-
return cur.fetchone() is not None
44+
if not cur.fetchone():
45+
raise HTTPException(status_code=401, detail="Unknown username/password")
46+
47+
return email;
4448

4549
@app.get("/logout")
4650
async def root(email: str):
4751
return {"message": "Logged out %s!" % email}
52+
53+
@app.get("/attachment")
54+
async def attachment(attachment_name: str):
55+
attachment_path = 'attachments/' + attachment_name
56+
if not isfile(attachment_path):
57+
raise HTTPException(status_code=404, detail="Attachment not found")
58+
59+
with open(attachment_path) as f:
60+
return f.readlines()
61+
62+
@app.post("/job")
63+
async def job(job_name: str):
64+
job_path = 'jobs/' + job_name
65+
if not isfile(job_path):
66+
raise HTTPException(status_code=404, detail="Job not found")
67+
68+
exec(open(job_path).read())
69+

0 commit comments

Comments
 (0)