forked from sumanentc/python-sample-FastAPI-application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
183 lines (148 loc) · 6.3 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from typing import List, Optional
import uvicorn
from fastapi import Depends, FastAPI, HTTPException
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from sqlalchemy.orm import Session
import universities
import time
import asyncio
import sql_app.models as models
import sql_app.schemas as schemas
from db import get_db, engine
from sql_app.repositories import ItemRepo, StoreRepo
app = FastAPI(title="Sample FastAPI Application",
description="Sample FastAPI Application with Swagger and Sqlalchemy",
version="1.0.0", )
models.Base.metadata.create_all(bind=engine)
@app.exception_handler(Exception)
def validation_exception_handler(request, err):
base_error_message = f"Failed to execute: {request.method}: {request.url}"
return JSONResponse(status_code=400, content={"message": f"{base_error_message}. Detail: {err}"})
@app.middleware("http")
async def add_process_time_header(request, call_next):
print('inside middleware!')
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
response.headers["X-Process-Time"] = str(f'{process_time:0.4f} sec')
return response
@app.post('/items', tags=["Item"], response_model=schemas.Item, status_code=201)
async def create_item(item_request: schemas.ItemCreate, db: Session = Depends(get_db)):
"""
Create an Item and store it in the database
"""
db_item = ItemRepo.fetch_by_name(db, name=item_request.name)
if db_item:
raise HTTPException(status_code=400, detail="Item already exists!")
return await ItemRepo.create(db=db, item=item_request)
@app.get('/items', tags=["Item"], response_model=List[schemas.Item])
def get_all_items(name: Optional[str] = None, db: Session = Depends(get_db)):
"""
Get all the Items stored in database
"""
if name:
items = []
db_item = ItemRepo.fetch_by_name(db, name)
items.append(db_item)
return items
else:
return ItemRepo.fetch_all(db)
@app.get('/items/{item_id}', tags=["Item"], response_model=schemas.Item)
def get_item(item_id: int, db: Session = Depends(get_db)):
"""
Get the Item with the given ID provided by User stored in database
"""
db_item = ItemRepo.fetch_by_id(db, item_id)
if db_item is None:
raise HTTPException(status_code=404, detail="Item not found with the given ID")
return db_item
@app.delete('/items/{item_id}', tags=["Item"])
async def delete_item(item_id: int, db: Session = Depends(get_db)):
"""
Delete the Item with the given ID provided by User stored in database
"""
db_item = ItemRepo.fetch_by_id(db, item_id)
if db_item is None:
raise HTTPException(status_code=404, detail="Item not found with the given ID")
await ItemRepo.delete(db, item_id)
return "Item deleted successfully!"
@app.put('/items/{item_id}', tags=["Item"], response_model=schemas.Item)
async def update_item(item_id: int, item_request: schemas.Item, db: Session = Depends(get_db)):
"""
Update an Item stored in the database
"""
db_item = ItemRepo.fetch_by_id(db, item_id)
if db_item:
update_item_encoded = jsonable_encoder(item_request)
db_item.name = update_item_encoded['name']
db_item.price = update_item_encoded['price']
db_item.description = update_item_encoded['description']
db_item.store_id = update_item_encoded['store_id']
return await ItemRepo.update(db=db, item_data=db_item)
else:
raise HTTPException(status_code=400, detail="Item not found with the given ID")
@app.post('/stores', tags=["Store"], response_model=schemas.Store, status_code=201)
async def create_store(store_request: schemas.StoreCreate, db: Session = Depends(get_db)):
"""
Create a Store and save it in the database
"""
db_store = StoreRepo.fetch_by_name(db, name=store_request.name)
print(db_store)
if db_store:
raise HTTPException(status_code=400, detail="Store already exists!")
return await StoreRepo.create(db=db, store=store_request)
@app.get('/stores', tags=["Store"], response_model=List[schemas.Store])
def get_all_stores(name: Optional[str] = None, db: Session = Depends(get_db)):
"""
Get all the Stores stored in database
"""
if name:
stores = []
db_store = StoreRepo.fetch_by_name(db, name)
print(db_store)
stores.append(db_store)
return stores
else:
return StoreRepo.fetch_all(db)
@app.get('/stores/{store_id}', tags=["Store"], response_model=schemas.Store)
def get_store(store_id: int, db: Session = Depends(get_db)):
"""
Get the Store with the given ID provided by User stored in database
"""
db_store = StoreRepo.fetch_by_id(db, store_id)
if db_store is None:
raise HTTPException(status_code=404, detail="Store not found with the given ID")
return db_store
@app.delete('/stores/{store_id}', tags=["Store"])
async def delete_store(store_id: int, db: Session = Depends(get_db)):
"""
Delete the Item with the given ID provided by User stored in database
"""
db_store = StoreRepo.fetch_by_id(db, store_id)
if db_store is None:
raise HTTPException(status_code=404, detail="Store not found with the given ID")
await StoreRepo.delete(db, store_id)
return "Store deleted successfully!"
@app.get("/universities/", tags=["University"])
def get_universities() -> dict:
"""
Return the List of universities for some random countries in sync way
"""
data: dict = {}
data.update(universities.get_all_universities_for_country("turkey"))
data.update(universities.get_all_universities_for_country("india"))
data.update(universities.get_all_universities_for_country("australia"))
return data
@app.get("/universities/async", tags=["University"])
async def get_universities_async() -> dict:
"""
Return the List of universities for some random countries in async way
"""
data: dict = {}
await asyncio.gather(universities.get_all_universities_for_country_async("turkey", data),
universities.get_all_universities_for_country_async("india", data),
universities.get_all_universities_for_country_async("australia", data))
return data
if __name__ == "__main__":
uvicorn.run("main:app", port=9000, reload=True)