-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmain.py
92 lines (69 loc) · 1.97 KB
/
main.py
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
#Twitter: Bek Brace
#Instagram: Bek_Brace
import uvicorn
from fastapi import FastAPI, Body, Depends
from app.model import PostSchema, UserSchema, UserLoginSchema
from app.auth.auth_bearer import JWTBearer
from app.auth.auth_handler import signJWT
posts = [
{
"id": 1,
"title": "Penguins ",
"text": "Penguins are a group of aquatic flightless birds."
},
{
"id": 2,
"title": "Tigers ",
"text": "Tigers are the largest living cat species and a memeber of the genus panthera."
},
{
"id": 3,
"title": "Koalas ",
"text": "Koala is arboreal herbivorous maruspial native to Australia."
},
]
users = []
app = FastAPI()
def check_user(data: UserLoginSchema):
for user in users:
if user.email == data.email and user.password == data.password:
return True
return False
# route handlers
# testing
@app.get("/", tags=["test"])
def greet():
return {"hello": "world!."}
# Get Posts
@app.get("/posts", tags=["posts"])
def get_posts():
return { "data": posts }
@app.get("/posts/{id}", tags=["posts"])
def get_single_post(id: int):
if id > len(posts):
return {
"error": "No such post with the supplied ID."
}
for post in posts:
if post["id"] == id:
return {
"data": post
}
@app.post("/posts", dependencies=[Depends(JWTBearer())], tags=["posts"])
def add_post(post: PostSchema):
post.id = len(posts) + 1
posts.append(post.dict())
return {
"data": "post added."
}
@app.post("/user/signup", tags=["user"])
def create_user(user: UserSchema = Body(...)):
users.append(user) # replace with db call, making sure to hash the password first
return signJWT(user.email)
@app.post("/user/login", tags=["user"])
def user_login(user: UserLoginSchema = Body(...)):
if check_user(user):
return signJWT(user.email)
return {
"error": "Wrong login details!"
}