sanic-api 0.3.0a3__tar.gz → 0.3.0a4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/.gitignore +0 -0
  2. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/.pre-commit-config.yaml +18 -18
  3. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/CHANGELOG.md +98 -97
  4. sanic_api-0.3.0a4/COMMIT_GUIDE.MD +10 -0
  5. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/LICENSE.txt +0 -0
  6. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/PKG-INFO +78 -32
  7. sanic_api-0.3.0a4/README.md +124 -0
  8. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/example/__init__.py +0 -0
  9. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/example/app.py +56 -56
  10. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/example/log.txt +1499 -1499
  11. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/example/mini_app.py +11 -11
  12. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/images/logo.png +0 -0
  13. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/pyproject.toml +4 -3
  14. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/requirements-dev.lock +46 -0
  15. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/requirements.lock +1 -0
  16. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/src/sanic_api/__init__.py +3 -3
  17. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/src/sanic_api/api/__init__.py +17 -17
  18. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/src/sanic_api/api/model.py +0 -0
  19. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/src/sanic_api/api/request.py +138 -138
  20. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/src/sanic_api/api/response.py +34 -34
  21. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/src/sanic_api/app.py +301 -275
  22. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/src/sanic_api/config/__init__.py +1 -1
  23. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/src/sanic_api/config/setting.py +71 -71
  24. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/src/sanic_api/logger/__init__.py +0 -0
  25. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/src/sanic_api/logger/config.py +99 -99
  26. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/src/sanic_api/logger/extension.py +129 -129
  27. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/src/sanic_api/openapi/__init__.py +0 -0
  28. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/src/sanic_api/openapi/extension.py +0 -0
  29. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/src/sanic_api/utils/__init__.py +0 -0
  30. {sanic_api-0.3.0a3 → sanic_api-0.3.0a4}/src/sanic_api/utils/enum.py +58 -58
  31. sanic_api-0.3.0a3/COMMIT_GUIDE.MD +0 -10
  32. sanic_api-0.3.0a3/README.md +0 -79
File without changes
@@ -1,18 +1,18 @@
1
- # See https://pre-commit.com for more information
2
- # See https://pre-commit.com/hooks.html for more hooks
3
- repos:
4
- # bandit
5
- - repo: https://github.com/PyCQA/bandit
6
- rev: 1.7.10
7
- hooks:
8
- - id: bandit
9
- args: [ "-c", "pyproject.toml" ]
10
- additional_dependencies: [ "bandit[toml]" ]
11
-
12
- # ruff
13
- - repo: https://github.com/astral-sh/ruff-pre-commit
14
- rev: 'v0.6.9'
15
- hooks:
16
- - id: ruff
17
- args: [ --fix, --exit-non-zero-on-fix, --show-fixes ]
18
- - id: ruff-format
1
+ # See https://pre-commit.com for more information
2
+ # See https://pre-commit.com/hooks.html for more hooks
3
+ repos:
4
+ # bandit
5
+ - repo: https://github.com/PyCQA/bandit
6
+ rev: 1.7.10
7
+ hooks:
8
+ - id: bandit
9
+ args: [ "-c", "pyproject.toml" ]
10
+ additional_dependencies: [ "bandit[toml]" ]
11
+
12
+ # ruff
13
+ - repo: https://github.com/astral-sh/ruff-pre-commit
14
+ rev: 'v0.6.9'
15
+ hooks:
16
+ - id: ruff
17
+ args: [ --fix, --exit-non-zero-on-fix, --show-fixes ]
18
+ - id: ruff-format
@@ -1,97 +1,98 @@
1
- # Changelog
2
-
3
- All notable changes to this project will be documented in this file.
4
-
5
- The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
- and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
-
8
- ## [0.2.6] -2023-07-05
9
-
10
- ### Fixed
11
-
12
- - 修复配置类报错的问题
13
-
14
- ### Changed
15
-
16
- - 升级sanic依赖版本为22.3
17
- - 增加`setuptools`依赖
18
-
19
- ## [0.2.5] - 2023-04-09
20
-
21
- ### Added
22
-
23
- - 支持捕获在中间件期间发生错误的异常情况
24
- - 增加配置类功能
25
- - 示例文件更新
26
- - 工具方法中新增自动蓝图方法
27
-
28
- ### Changed
29
-
30
- - 默认的json dumps方法改为orjson
31
- - sanic 最低版本依赖更新至22.12
32
- - 优化API响应封装及响应文档的生成
33
-
34
- ### Fixed
35
-
36
- - 修复openapi tag重复的问题
37
- - 修复日志级别判断不准问题
38
-
39
- ## [0.2.4] - 2023-02-05
40
-
41
- ### Fixed
42
-
43
- - 修复示例文件不生效的问题
44
-
45
- ## [0.2.3] - 2023-02-03
46
-
47
- ### Fixed
48
-
49
- - 修复无蓝图的api接口blueprint.ctx.desc报错的问题
50
- - 实现_missing_方法,修复枚举基类无法识别枚举值的问题
51
-
52
- ### Changed
53
-
54
- - 请求接口无参数时访问日志中不打印 “args:”
55
- - 枚举基类字段类名称修改,避免重名
56
-
57
- ### Added
58
-
59
- - API接口类支持自定义接口标签和接口描述
60
- - 添加types-ujson包
61
- - 自动生成的文档支持识别必须和可选参数
62
- - 枚举基类增加to_desc方法列出枚举的所有描述
63
-
64
- ## [0.2.0] - 2023-01-19
65
-
66
- ### Changed
67
-
68
- - api json序列化改为使用ujson
69
- - 移除部分3.10以上的语法
70
-
71
- ### Added
72
-
73
- - 日志功能优化改进:
74
- - 实现方式更加简洁优雅
75
- - 可按照类型去区分
76
- - 日志增加打印请求ID
77
- - json响应体格式设置为可配置的
78
- - 添加ujson包,移除orjson包
79
- - 读取蓝图上面的 blueprint.ctx.desc 属性来代替name设置中文tag名
80
- - 引入mypy、autoflake等代码检查工具
81
- - 添加配置文件基类
82
-
83
- ## [0.1.1] - 2022-12-19
84
-
85
- ### Changed
86
-
87
- - pip 首页的文件修改
88
-
89
- ## [0.1.0] - 2022-12-19
90
-
91
- ### Added
92
-
93
- - 接口参数校验
94
- - 接口文档生成
95
- - 日志使用`loguru`代替
96
- - 接口异常拦截
97
- - 接口响应统一化
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+
9
+ ## [0.2.6] - 2023-07-05
10
+
11
+ ### Fixed
12
+
13
+ - 修复配置类报错的问题
14
+
15
+ ### Changed
16
+
17
+ - 升级sanic依赖版本为22.3
18
+ - 增加`setuptools`依赖
19
+
20
+ ## [0.2.5] - 2023-04-09
21
+
22
+ ### Added
23
+
24
+ - 支持捕获在中间件期间发生错误的异常情况
25
+ - 增加配置类功能
26
+ - 示例文件更新
27
+ - 工具方法中新增自动蓝图方法
28
+
29
+ ### Changed
30
+
31
+ - 默认的json dumps方法改为orjson
32
+ - sanic 最低版本依赖更新至22.12
33
+ - 优化API响应封装及响应文档的生成
34
+
35
+ ### Fixed
36
+
37
+ - 修复openapi tag重复的问题
38
+ - 修复日志级别判断不准问题
39
+
40
+ ## [0.2.4] - 2023-02-05
41
+
42
+ ### Fixed
43
+
44
+ - 修复示例文件不生效的问题
45
+
46
+ ## [0.2.3] - 2023-02-03
47
+
48
+ ### Fixed
49
+
50
+ - 修复无蓝图的api接口blueprint.ctx.desc报错的问题
51
+ - 实现_missing_方法,修复枚举基类无法识别枚举值的问题
52
+
53
+ ### Changed
54
+
55
+ - 请求接口无参数时访问日志中不打印 “args:”
56
+ - 枚举基类字段类名称修改,避免重名
57
+
58
+ ### Added
59
+
60
+ - API接口类支持自定义接口标签和接口描述
61
+ - 添加types-ujson包
62
+ - 自动生成的文档支持识别必须和可选参数
63
+ - 枚举基类增加to_desc方法列出枚举的所有描述
64
+
65
+ ## [0.2.0] - 2023-01-19
66
+
67
+ ### Changed
68
+
69
+ - api json序列化改为使用ujson
70
+ - 移除部分3.10以上的语法
71
+
72
+ ### Added
73
+
74
+ - 日志功能优化改进:
75
+ - 实现方式更加简洁优雅
76
+ - 可按照类型去区分
77
+ - 日志增加打印请求ID
78
+ - json响应体格式设置为可配置的
79
+ - 添加ujson包,移除orjson包
80
+ - 读取蓝图上面的 blueprint.ctx.desc 属性来代替name设置中文tag名
81
+ - 引入mypy、autoflake等代码检查工具
82
+ - 添加配置文件基类
83
+
84
+ ## [0.1.1] - 2022-12-19
85
+
86
+ ### Changed
87
+
88
+ - pip 首页的文件修改
89
+
90
+ ## [0.1.0] - 2022-12-19
91
+
92
+ ### Added
93
+
94
+ - 接口参数校验
95
+ - 接口文档生成
96
+ - 日志使用`loguru`代替
97
+ - 接口异常拦截
98
+ - 接口响应统一化
@@ -0,0 +1,10 @@
1
+ - ✨ feat: 一个新特性
2
+ - ➕ improvement: 对现有特性的提升
3
+ - 🐛 fix: 修复Bug
4
+ - 🛠 refactor: 代码重构,注意和特性、重构区分开
5
+ - 📚 docs: 变更文档
6
+ - 💅 style: 修改格式,不影响功能,例如空格、代码格式等
7
+ - 📈 perf: 性能提升
8
+ - 🔧 ci: 修改ci相关配置、脚本等
9
+ - ⏪ revert: 回退版本
10
+ - 🗯 chore: 杂务,不属于以上类型,例如run build、引入或更新软件包等
File without changes
@@ -1,12 +1,13 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: sanic-api
3
- Version: 0.3.0a3
3
+ Version: 0.3.0a4
4
4
  Summary: Sanic 框架实用API工具集,拥有自动生成文档、参数校验、配置的导入、日志功能的优化等功能,更好的助力接口的开发
5
5
  Project-URL: homepage, https://github.com/x-haose/sanic-api
6
6
  Project-URL: repository, https://github.com/x-haose/sanic-api
7
7
  Project-URL: documentation, https://github.com/x-haose/sanic-api
8
8
  Author-email: 昊色居士 <xhrtxh@gmail.com>
9
- License: MIT
9
+ License-Expression: MIT
10
+ License-File: LICENSE.txt
10
11
  Keywords: Sanic,Sanic扩展,异步
11
12
  Classifier: Development Status :: 4 - Beta
12
13
  Classifier: Intended Audience :: Developers
@@ -35,12 +36,9 @@ Requires-Dist: sentry-sdk>=2.17.0
35
36
  Requires-Dist: ujson>=5.7.0
36
37
  Description-Content-Type: text/markdown
37
38
 
38
- ![logo](https://images.haose.pro/2022/12/19/logo_17%3A34%3A07_qkt9yi4d7u.png)
39
-
40
39
  [![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](https://choosealicense.com/licenses/mit/)
41
- [![Python](https://img.shields.io/badge/Python-3.8+-yellow.svg?logo=python)]()
40
+ [![Python](https://img.shields.io/badge/Python-3.10+-yellow.svg?logo=python)]()
42
41
  [![Sanic](https://img.shields.io/badge/framework-Sanic-Server.svg)](http://www.gnu.org/licenses/agpl-3.0)
43
- [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
44
42
 
45
43
  # Sanic-API
46
44
 
@@ -48,28 +46,25 @@ Description-Content-Type: text/markdown
48
46
 
49
47
  ## 特性
50
48
 
51
- - 无需任何多余改动,全自动生成openapi文档,使用更加方便
49
+ - 无需任何多余改动,全自动生成openapi文档,使用更加方便 (新版方案正在积极优化开发中)
52
50
 
53
- - 基于`pydantic`的参数校验器,让接口的请求及响应更符合你的预期
51
+ - 基于`pydantic`的参数校验器,让接口的请求及响应更符合你的预期,使用更方便
54
52
 
55
- - 使用`loguru`库代替官方`logging`日志库,并对访问日志进行扩展,支持打印接口耗时、接口参数
53
+ - 使用`loguru`库代替官方`logging`日志库,并对访问日志进行扩展,支持写入文件及推送loki
56
54
 
57
- - 使用`{code: 0, data: null, msg: ""}`样式的接口返回
55
+ - 使用了基于`pydantic-settings`的项目配置方案,支持json、yml、ini、.env等多种格式
58
56
 
59
- - 对接口中的异常进行拦截,及自定义错误码
57
+ - 对sanic的启动进行了简单的封装,可快速启动项目
60
58
 
61
- - 接口返回样式可自定义配置
62
59
 
63
60
  ## 截图
64
61
 
65
62
  ## 路线图
66
63
 
67
- - 增加一键生成预设项目cli命令
64
+ - 全自动生成openapi文档
68
65
 
69
66
  - 编写详细文档
70
67
 
71
- - API接口增加请求头、URL路径参数收集和校验:
72
-
73
68
  ## 安装
74
69
 
75
70
  使用 pip 安装 sanic-api
@@ -80,37 +75,88 @@ Description-Content-Type: text/markdown
80
75
 
81
76
  ## 使用方法/示例
82
77
 
78
+ ### 最小示例
79
+ ```python
80
+ from sanic_api.app import BaseApp
81
+
82
+
83
+ class App(BaseApp):
84
+ """
85
+ 最小的sanic服务示例
86
+ """
87
+
88
+
89
+ if __name__ == "__main__":
90
+ App.run()
91
+ ```
92
+
93
+ ### 带参数校验的示例
83
94
  ```python
84
- from sanic import Sanic, text
85
- from sanic_api import init_api
95
+ from pydantic import BaseModel, Field
96
+ from sanic import Blueprint, Sanic, json
97
+ from sanic.log import logger
98
+
99
+ from sanic_api.api import BaseRespTml, Request
100
+ from sanic_api.app import BaseApp
86
101
 
87
- app = Sanic("Sanic-API")
102
+ user_blueprint = Blueprint("user", "/user")
88
103
 
89
104
 
90
- @app.get('/')
91
- async def index(request):
92
- return text("Sanic-API Example")
105
+ class UserInfoModel(BaseModel):
106
+ user_id: int = Field(title="用户ID")
93
107
 
94
108
 
95
- def main():
96
- init_api(app)
97
- app.run(access_log=True)
109
+ class UserInfoResponse(BaseRespTml):
110
+ user_name: str = Field(title="用户名")
98
111
 
99
112
 
100
- if __name__ == '__main__':
101
- main()
113
+ class UseLoginRequest(Request):
114
+ form_data: UserInfoModel
102
115
 
116
+
117
+ @user_blueprint.post("info")
118
+ async def user_info(request: Request, json_data: UserInfoModel):
119
+ """
120
+ 获取用户信息
121
+ """
122
+ logger.info(f"data: {json_data}")
123
+ info = UserInfoResponse(user_name="张三")
124
+ info.temp_data.code = "0000"
125
+ info.temp_data.msg = "查询成功"
126
+ return info.resp()
127
+
128
+
129
+ @user_blueprint.post("login")
130
+ async def user_login(request: UseLoginRequest):
131
+ """
132
+ 用户登录
133
+ """
134
+ logger.info(f"user_id: {request.form_data.user_id}")
135
+ return json(request.form_data.model_dump())
136
+
137
+
138
+ class App(BaseApp):
139
+ """
140
+ 服务示例
141
+ """
142
+
143
+ async def setup_route(self, app: Sanic):
144
+ api = Blueprint.group(url_prefix="api")
145
+ api.append(user_blueprint)
146
+ app.blueprint(api)
147
+
148
+
149
+ if __name__ == "__main__":
150
+ App.run()
103
151
  ```
104
152
 
105
153
  ## 开发
106
154
 
107
- 要部署这个项目,请运行
155
+ 要部署这个项目,请先安装rye
108
156
 
109
157
  ```bash
110
- pip install pdm
111
- pdm sync
158
+ rye sync
112
159
  ```
113
160
 
114
161
  ## 文档
115
-
116
- [文档](https://linktodocumentation)
162
+ 正在编写中,敬请期待
@@ -0,0 +1,124 @@
1
+ [![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](https://choosealicense.com/licenses/mit/)
2
+ [![Python](https://img.shields.io/badge/Python-3.10+-yellow.svg?logo=python)]()
3
+ [![Sanic](https://img.shields.io/badge/framework-Sanic-Server.svg)](http://www.gnu.org/licenses/agpl-3.0)
4
+
5
+ # Sanic-API
6
+
7
+ 让您的sanic服务程序更好的支持API文档、参数校验、日志打印、响应规范等
8
+
9
+ ## 特性
10
+
11
+ - 无需任何多余改动,全自动生成openapi文档,使用更加方便 (新版方案正在积极优化开发中)
12
+
13
+ - 基于`pydantic`的参数校验器,让接口的请求及响应更符合你的预期,使用更方便
14
+
15
+ - 使用`loguru`库代替官方`logging`日志库,并对访问日志进行扩展,支持写入文件及推送loki
16
+
17
+ - 使用了基于`pydantic-settings`的项目配置方案,支持json、yml、ini、.env等多种格式
18
+
19
+ - 对sanic的启动进行了简单的封装,可快速启动项目
20
+
21
+
22
+ ## 截图
23
+
24
+ ## 路线图
25
+
26
+ - 全自动生成openapi文档
27
+
28
+ - 编写详细文档
29
+
30
+ ## 安装
31
+
32
+ 使用 pip 安装 sanic-api
33
+
34
+ ```bash
35
+ pip install sanic-api
36
+ ```
37
+
38
+ ## 使用方法/示例
39
+
40
+ ### 最小示例
41
+ ```python
42
+ from sanic_api.app import BaseApp
43
+
44
+
45
+ class App(BaseApp):
46
+ """
47
+ 最小的sanic服务示例
48
+ """
49
+
50
+
51
+ if __name__ == "__main__":
52
+ App.run()
53
+ ```
54
+
55
+ ### 带参数校验的示例
56
+ ```python
57
+ from pydantic import BaseModel, Field
58
+ from sanic import Blueprint, Sanic, json
59
+ from sanic.log import logger
60
+
61
+ from sanic_api.api import BaseRespTml, Request
62
+ from sanic_api.app import BaseApp
63
+
64
+ user_blueprint = Blueprint("user", "/user")
65
+
66
+
67
+ class UserInfoModel(BaseModel):
68
+ user_id: int = Field(title="用户ID")
69
+
70
+
71
+ class UserInfoResponse(BaseRespTml):
72
+ user_name: str = Field(title="用户名")
73
+
74
+
75
+ class UseLoginRequest(Request):
76
+ form_data: UserInfoModel
77
+
78
+
79
+ @user_blueprint.post("info")
80
+ async def user_info(request: Request, json_data: UserInfoModel):
81
+ """
82
+ 获取用户信息
83
+ """
84
+ logger.info(f"data: {json_data}")
85
+ info = UserInfoResponse(user_name="张三")
86
+ info.temp_data.code = "0000"
87
+ info.temp_data.msg = "查询成功"
88
+ return info.resp()
89
+
90
+
91
+ @user_blueprint.post("login")
92
+ async def user_login(request: UseLoginRequest):
93
+ """
94
+ 用户登录
95
+ """
96
+ logger.info(f"user_id: {request.form_data.user_id}")
97
+ return json(request.form_data.model_dump())
98
+
99
+
100
+ class App(BaseApp):
101
+ """
102
+ 服务示例
103
+ """
104
+
105
+ async def setup_route(self, app: Sanic):
106
+ api = Blueprint.group(url_prefix="api")
107
+ api.append(user_blueprint)
108
+ app.blueprint(api)
109
+
110
+
111
+ if __name__ == "__main__":
112
+ App.run()
113
+ ```
114
+
115
+ ## 开发
116
+
117
+ 要部署这个项目,请先安装rye
118
+
119
+ ```bash
120
+ rye sync
121
+ ```
122
+
123
+ ## 文档
124
+ 正在编写中,敬请期待
@@ -1,56 +1,56 @@
1
- from pydantic import BaseModel, Field
2
- from sanic import Blueprint, Sanic, json
3
- from sanic.log import logger
4
-
5
- from sanic_api.api import BaseRespTml, Request
6
- from sanic_api.app import BaseApp
7
-
8
- user_blueprint = Blueprint("user", "/user")
9
-
10
-
11
- class UserInfoModel(BaseModel):
12
- user_id: int = Field(title="用户ID")
13
-
14
-
15
- class UserInfoResponse(BaseRespTml):
16
- user_name: str = Field(title="用户名")
17
-
18
-
19
- class UseLoginRequest(Request):
20
- form_data: UserInfoModel
21
-
22
-
23
- @user_blueprint.post("info")
24
- async def user_info(request: Request, json_data: UserInfoModel):
25
- """
26
- 获取用户信息
27
- """
28
- logger.info(f"data: {json_data}")
29
- info = UserInfoResponse(user_name="张三")
30
- info.temp_data.code = "0000"
31
- info.temp_data.msg = "查询成功"
32
- return info.resp()
33
-
34
-
35
- @user_blueprint.post("login")
36
- async def user_login(request: UseLoginRequest):
37
- """
38
- 用户登录
39
- """
40
- logger.info(f"user_id: {request.form_data.user_id}")
41
- return json(request.form_data.model_dump())
42
-
43
-
44
- class App(BaseApp):
45
- """
46
- 服务示例
47
- """
48
-
49
- async def setup_route(self, app: Sanic):
50
- api = Blueprint.group(url_prefix="api")
51
- api.append(user_blueprint)
52
- app.blueprint(api)
53
-
54
-
55
- if __name__ == "__main__":
56
- App.run()
1
+ from pydantic import BaseModel, Field
2
+ from sanic import Blueprint, Sanic, json
3
+ from sanic.log import logger
4
+
5
+ from sanic_api.api import BaseRespTml, Request
6
+ from sanic_api.app import BaseApp
7
+
8
+ user_blueprint = Blueprint("user", "/user")
9
+
10
+
11
+ class UserInfoModel(BaseModel):
12
+ user_id: int = Field(title="用户ID")
13
+
14
+
15
+ class UserInfoResponse(BaseRespTml):
16
+ user_name: str = Field(title="用户名")
17
+
18
+
19
+ class UseLoginRequest(Request):
20
+ form_data: UserInfoModel
21
+
22
+
23
+ @user_blueprint.post("info")
24
+ async def user_info(request: Request, json_data: UserInfoModel):
25
+ """
26
+ 获取用户信息
27
+ """
28
+ logger.info(f"data: {json_data}")
29
+ info = UserInfoResponse(user_name="张三")
30
+ info.temp_data.code = "0000"
31
+ info.temp_data.msg = "查询成功"
32
+ return info.resp()
33
+
34
+
35
+ @user_blueprint.post("login")
36
+ async def user_login(request: UseLoginRequest):
37
+ """
38
+ 用户登录
39
+ """
40
+ logger.info(f"user_id: {request.form_data.user_id}")
41
+ return json(request.form_data.model_dump())
42
+
43
+
44
+ class App(BaseApp):
45
+ """
46
+ 服务示例
47
+ """
48
+
49
+ async def setup_route(self, app: Sanic):
50
+ api = Blueprint.group(url_prefix="api")
51
+ api.append(user_blueprint)
52
+ app.blueprint(api)
53
+
54
+
55
+ if __name__ == "__main__":
56
+ App.run()