-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
121 lines (80 loc) · 3.15 KB
/
Copy pathmain.py
File metadata and controls
121 lines (80 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from pathlib import Path
from fastapi import FastAPI, HTTPException
app = FastAPI()
# username: password
creds = {}
# heading: [description, username]
data = {}
# returns error if invalid, otherwise it returns nothing
def login(username: str, password: str):
correct_password = creds.get(username)
if correct_password is None:
raise HTTPException(status_code=401, detail="Incorrect Username!")
if correct_password != password:
raise HTTPException(status_code=401, detail="Incorrect Password!")
# no need auth for get data
@app.get("/data")
async def get_data():
results = []
for heading in data:
description, username = data[heading]
results.append([heading, description, username])
return {"data": results}
@app.get("/search/{query}")
async def search_data(query: str):
results = []
for heading in data:
description, username = data[heading]
# Add the result only once, even if the query
# appears in both heading and description.
if (query.lower() in heading.lower()) or (query.lower() in description.lower()):
results.append([heading, description, username])
return {"data": results}
# validates their credentials. if new username, it creates one.
@app.post("/login")
async def login_user(username: str, password: str):
correct_password = creds.get(username)
if correct_password is None:
creds[username] = password
return {"message": "Register successful"}
if correct_password == password:
return {"message": "Login successful"}
else:
raise HTTPException(status_code=401, detail="Password Incorrect")
@app.post("/upload")
async def upload_data(username: str, password: str, heading: str, description: str):
# here's where the AUTH is important!
login(username, password)
data[heading] = [description, username]
return {"message": "Data uploaded successfully", "data": data}
@app.post("/update")
async def update_data(
username: str,
password: str,
heading: str,
new_heading: str,
new_description: str,
):
login(username, password)
if heading not in data:
raise HTTPException(status_code=404, detail="Heading not found")
description, post_username = data[heading]
if post_username != username:
raise HTTPException(status_code=403, detail="Not your heading")
if new_heading != heading and new_heading in data:
raise HTTPException(status_code=409, detail="Heading already exists")
# delete old heading
data.pop(heading)
# create data again with new heading
data[new_heading] = [new_description, username]
return {"message": "Data updated successfully", "data": data}
@app.post("/delete")
async def delete_data(username: str, password: str, heading: str):
login(username, password)
if heading not in data:
raise HTTPException(status_code=404, detail="Heading not found")
description, post_username = data[heading]
if post_username != username:
raise HTTPException(status_code=403, detail="Not your heading")
data.pop(heading)
return {"message": "Data deleted successfully", "data": data}