-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.py
58 lines (38 loc) · 1.36 KB
/
index.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
from __future__ import print_function
import time
from typing import Any, Dict, Optional
from fastapi import FastAPI
from typing import Optional
from sqlmodel import Field, Session, SQLModel, create_engine, select
class User(SQLModel, table=True):
# TODO change the default value to the timestamp that the user is created
id: Optional[int] = Field(default=None, primary_key=True)
name: str
secret_name: str
age: Optional[str] = None
engine = create_engine("sqlite:///database.db")
SQLModel.metadata.create_all(engine)
# Create an instance of the API class
app = FastAPI()
@app.get("/")
async def root():
# TODO include the user's name if they are logged in
return {"message": "Hello {}".format("World")}
@app.post("/user")
async def create_new_user(*, user: User):
with Session(engine) as session:
session.add(user)
session.commit()
# TODO return the User ID
return {"message": "User created"}
@app.get("/user/{id}")
async def get_user(id: int):
with Session(engine) as session:
# TODO return the user based on the ID (and an error if not)
statement = select(User).where(User.name == id)
user = session.exec(statement).first()
return {"user": user}
@app.get("/api/webhooks")
async def handle_hook(*, event: Any):
id = event.payload.conversation.id
return {"message": "Hello World"}