-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringValidation.py
39 lines (34 loc) · 1.49 KB
/
StringValidation.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
from fastapi import FastAPI ,Query
from fastapi.middleware.cors import CORSMiddleware
import nest_asyncio
from pyngrok import ngrok
import uvicorn
from typing import List
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*'],
)
@app.get('/')
async def root():
return {'hello': 'world'}
# max min length and regular expression validation ,default value (put value instead of ... this)and optional parameter(put None instead of ... this)
@app.get("/items/")
async def get_items(item_id: str = Query(..., min_length = 2, max_length = 10, regex="^Item\d{1,6}")):#... means required value
return {"item": item_id}
# passing a list as a default value,metadata about the properties(deprecation ,title and description) and alias name
@app.get("/items/")
async def get_items(item_id: List[str] = Query(["Pen", "Pencil"], # list as a parameter
title = "Item List", # metadata
description = "List of items to be returned.",# metadata
min_length = 2, max_length = 10, deprecated = True, # depricarting parameters
alias = "item-id")): # Alias name
results = {"items": item_id}
return results
ngrok_tunnel = ngrok.connect(8000)
print('Public URL:', ngrok_tunnel.public_url)
nest_asyncio.apply()
uvicorn.run(app, port=8000)