-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumericValidation.py
42 lines (33 loc) · 1.26 KB
/
NumericValidation.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
from fastapi import FastAPI, Query, Path
from fastapi.middleware.cors import CORSMiddleware
import nest_asyncio
from pyngrok import ngrok
import uvicorn
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*'],
)
@app.get('/')
async def root():
return {'hello': 'world'}
## Numeric Validation
# Path Parameter Validation(We can't make path parameter as optional parameter ,it is always required)
@app.get("/items/{item_id}")
async def get_items(item_id: str = Path(..., min_length = 2, max_length = 10, regex="^Item\d{1,6}")):
return {"item_id": item_id}
# path parameter and query parameter
@app.get("/items/{item_id}")
async def get_items(item: str = Query(None), item_id: str = Path(..., min_length = 2, max_length = 10, regex="^Item\d{1,6}")):
return {"item_id": item_id, "item" : item}
# gt = greater than ge = greater than or equal to, similarly for the lt and le
@app.get("/items/{item_id}")
async def get_items(item_id: float = Path(..., le = 10, ge = 2, alias = "Item-id", title = "Item Id")):
return {"item_id": item_id}
ngrok_tunnel = ngrok.connect(8000)
print('Public URL:', ngrok_tunnel.public_url)
nest_asyncio.apply()
uvicorn.run(app, port=8000)