-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequestBodyValidation.py
46 lines (38 loc) · 1.27 KB
/
RequestBodyValidation.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
import uvicorn
from fastapi import FastAPI, Query, Path, Body
from pydantic import BaseModel, Field
from fastapi.middleware.cors import CORSMiddleware
import nest_asyncio
from pyngrok import ngrok
app = FastAPI(debug = True)
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*'],
)
class Item(BaseModel):
name: str
description: str = Field(None, title="Description of the item", max_length=250) #Validation
price: float = Field(..., gt=0, le=100, description="The price must be greater than zero and lesser than or equal to 100") # Validation
tax: float = None
class User(BaseModel):
username: str
full_name: str = None
@app.get(/)
async def root():
return {"message" : "Hello World"}
@app.put("/items/{item_id}")
async def update_item(*, item_id: int, item: Item = Body(..., embed=True, example={ # as the name implies it is an example
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},)):
results = {"item_id": item_id, "item": item}
return results
ngrok_tunnel = ngrok.connect(8000)
print('Public URL:', ngrok_tunnel.public_url)
nest_asyncio.apply()
uvicorn.run(app, port=8000)