-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
129 lines (103 loc) · 3.05 KB
/
example.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
from typing import List, Optional
from pydantic import BaseModel, Field, root_validator
from simple_starlette import Include, Request, SimpleStarlette
from simple_starlette.args import (BodyParams, QueryParams, ResponseParams,
register_args)
from simple_starlette.middleware.cors import CorsMiddlewareGenFunc
from simple_starlette.responses import Response, ResTypeEnum
from simple_starlette.exceptions import RequestArgsNoMatch
app = SimpleStarlette(
__name__,
run_env="dev", # dev模式开启在线文档
middleware=[CorsMiddlewareGenFunc(allow_origins=["*"])],
)
# include api
api_user = Include(app, "/api/user")
api_admin = Include(app, "/api/admin")
@register_args()
class GetAdminOrUserOneArgs(QueryParams):
"""
通过 id 或 name 查询admin
"""
id_: Optional[int] = Field(description="id_ 和 name 必传其中一个")
name: Optional[str]
@root_validator
def validate_all(cls, values):
if not (values["name"] or values["id_"]):
raise RequestArgsNoMatch("id_ 和 name不能全为空")
return values
@api_admin.route("/one", allow_methods=["GET"])
async def get_admin_one(
requset: Request, query_args: GetAdminOrUserOneArgs
):
"""
获取admin详情接口
"""
# --do-something--
return Response(
{"admin_id": query_args.id_, "admin_name": query_args.name},
ResTypeEnum.JSON,
)
class GetAdminListArgs(QueryParams):
offset: int = 0
limit: int = 20
class AdminOneArgs(BaseModel):
admin_id: int
admin_name: str
class GetAdminListResponse(ResponseParams):
success_code: int = 200
message: str
admin_list: List[AdminOneArgs]
@api_admin.route("/list", allow_methods=["GET"])
async def get_admin_list(
request: Request,
query_args: GetAdminListArgs,
_: GetAdminListResponse,
):
"""
获取admin列表数据
"""
# --do-something--
return Response(
{
"success_code": 200,
"message": "success",
"admin_list": [{"admin_id": 1, "admin_name": "name"}],
},
ResTypeEnum.JSON,
)
@api_user.route("/one")
class GetUserOne:
class ResponseArgs(ResponseParams):
"""
user one 返回值
"""
class ResponseUserItem(BaseModel):
name: str
age: int
id_: int
message: str = Field(description="状态值")
status_code: int = Field(description="状态码")
user: ResponseUserItem = Field(description="user对象")
class PostArgs(BodyParams):
name: str = "jack"
def get(
self,
request: Request,
query: GetAdminOrUserOneArgs,
_: ResponseArgs,
):
"""
获取user详情接口
"""
# --do-something--
return Response(
{
"status_code": 200,
"message": "success",
"user": {"name": "user", "id_": 1, "age": 19},
},
ResTypeEnum.JSON,
)
if __name__ == "__main__":
app.run()