-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
138 lines (99 loc) · 2.75 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from typing import Optional
from fastapi import FastAPI, Request, Depends
from pydantic import BaseModel
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
import os
Base = declarative_base()
app = FastAPI()
class User(Base):
__tablename__ = "user"
id = Column('id', Integer, primary_key=True, autoincrement=True)
username = Column('username', String, unique=True)
class Alert(Base):
__tablename__ = "alert"
id = Column('id', Integer, primary_key=True, autoincrement=True)
alert = Column('alert', String)
engine = create_engine(
# 'postgresql://puser:ppassword@localhost:5432',
# echo=True
# 'sqlite:///users.db
os.environ['DATABASE_URL'],
echo=True
)
Base.metadata.create_all(bind=engine)
Session = sessionmaker(bind=engine)
# user = User()
# user.username = "Charntil"
# session = Session()
# alert = Alert()
# alerts = session.query(Alert).delete()
# session.commit()
class Item(BaseModel):
name: str
price: float
is_offer: Optional[bool] = None
# class Alert(BaseModel):
# name: str
# description: Optional[str] = None
# dateTime: int = None
# tax: Optional[float] = None
class City(BaseModel):
name: str
timezone: str
class MyUser(BaseModel):
username: str
# db = []
@app.get("/")
def read_root():
return {
"Hello": "FastApi"
}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
return {"item_id": item_id, "q": q}
@app.post("/users")
def add_user(this_user: MyUser):
session = Session()
user = User()
user.username = this_user.username
session.add(user)
session.commit()
users = session.query(User).all()
for user in users:
print(user.username, user.id)
session.close()
return users
# @app.get('/cities')
# def get_cities():
# return db
@app.get("/all-alerts")
def get_alerts():
session = Session()
alerts = session.query(Alert).all()
# for user in users:
# print(user.username, user.id)
session.close()
return alerts
# @app.get('/cities/{city_id}')
# @app.post('/cities')
# def create_city(city: City):
# db.append(city.dict())
# return db[-1]
# @app.delete('/cities')
@app.put("/items/{item_id}")
def udpate_item(item_id: int, item: Item):
return {"item_name": item.name, "item_id": item_id}
@app.post("/sdwan/alerts")
def meraki(post: dict):
session = Session()
alert = Alert()
alert.alert = str(post)
session.add(alert)
session.commit()
# users = session.query(User).all()
# for user in users:
# print(user.username, user.id)
session.close()
return {"alert": post}