forked from 0b01001001/spectree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarlette_demo.py
68 lines (52 loc) · 1.33 KB
/
starlette_demo.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
import uvicorn
from pydantic import BaseModel, Field
from starlette.applications import Starlette
from starlette.endpoints import HTTPEndpoint
from starlette.responses import JSONResponse
from starlette.routing import Mount, Route
from spectree import Response, SpecTree
api = SpecTree("starlette")
class Query(BaseModel):
text: str
class Resp(BaseModel):
label: int = Field(
...,
ge=0,
le=9,
)
score: float = Field(
...,
gt=0,
lt=1,
)
class Data(BaseModel):
uid: str
limit: int
vip: bool
@api.validate(query=Query, json=Data, resp=Response(HTTP_200=Resp), tags=["api"])
async def predict(request):
"""
async api
descriptions about this function
"""
print(request.path_params)
print(request.context)
return JSONResponse({"label": 5, "score": 0.5})
class Ping(HTTPEndpoint):
@api.validate(tags=["health check", "api"])
def get(self, request):
"""
health check
"""
return JSONResponse({"msg": "pong"})
if __name__ == "__main__":
app = Starlette(
routes=[
Route("/ping", Ping),
Mount(
"/api", routes=[Route("/predict/{luck:int}", predict, methods=["POST"])]
),
]
)
api.register(app)
uvicorn.run(app, log_level="info")