sanic-api 0.4.1__tar.gz → 0.5.0__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 (30) hide show
  1. {sanic_api-0.4.1 → sanic_api-0.5.0}/PKG-INFO +2 -1
  2. {sanic_api-0.4.1 → sanic_api-0.5.0}/pyproject.toml +12 -26
  3. {sanic_api-0.4.1 → sanic_api-0.5.0}/src/sanic_api/__init__.py +1 -1
  4. {sanic_api-0.4.1 → sanic_api-0.5.0}/src/sanic_api/app.py +8 -3
  5. {sanic_api-0.4.1 → sanic_api-0.5.0}/src/sanic_api/config/setting.py +6 -3
  6. {sanic_api-0.4.1 → sanic_api-0.5.0}/src/sanic_api/logger/extension.py +11 -5
  7. sanic_api-0.4.1/.pre-commit-config.yaml +0 -18
  8. sanic_api-0.4.1/CHANGELOG.md +0 -98
  9. sanic_api-0.4.1/COMMIT_GUIDE.MD +0 -10
  10. sanic_api-0.4.1/example/app.py +0 -64
  11. sanic_api-0.4.1/example/log.txt +0 -1584
  12. sanic_api-0.4.1/example/mini_app.py +0 -11
  13. sanic_api-0.4.1/images/logo.png +0 -0
  14. sanic_api-0.4.1/requirements-dev.lock +0 -142
  15. sanic_api-0.4.1/requirements.lock +0 -81
  16. sanic_api-0.4.1/src/sanic_api/utils/__init__.py +0 -0
  17. {sanic_api-0.4.1 → sanic_api-0.5.0}/.gitignore +0 -0
  18. {sanic_api-0.4.1 → sanic_api-0.5.0}/LICENSE.txt +0 -0
  19. {sanic_api-0.4.1 → sanic_api-0.5.0}/README.md +0 -0
  20. {sanic_api-0.4.1 → sanic_api-0.5.0}/src/sanic_api/api/__init__.py +0 -0
  21. {sanic_api-0.4.1 → sanic_api-0.5.0}/src/sanic_api/api/error.py +0 -0
  22. {sanic_api-0.4.1 → sanic_api-0.5.0}/src/sanic_api/api/model.py +0 -0
  23. {sanic_api-0.4.1 → sanic_api-0.5.0}/src/sanic_api/api/request.py +0 -0
  24. {sanic_api-0.4.1 → sanic_api-0.5.0}/src/sanic_api/config/__init__.py +0 -0
  25. {sanic_api-0.4.1/example → sanic_api-0.5.0/src/sanic_api/logger}/__init__.py +0 -0
  26. {sanic_api-0.4.1 → sanic_api-0.5.0}/src/sanic_api/logger/config.py +0 -0
  27. {sanic_api-0.4.1/src/sanic_api/logger → sanic_api-0.5.0/src/sanic_api/openapi}/__init__.py +0 -0
  28. {sanic_api-0.4.1 → sanic_api-0.5.0}/src/sanic_api/openapi/extension.py +0 -0
  29. {sanic_api-0.4.1/src/sanic_api/openapi → sanic_api-0.5.0/src/sanic_api/utils}/__init__.py +0 -0
  30. {sanic_api-0.4.1 → sanic_api-0.5.0}/src/sanic_api/utils/enum.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sanic-api
3
- Version: 0.4.1
3
+ Version: 0.5.0
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
@@ -19,6 +19,7 @@ Classifier: Operating System :: POSIX :: Linux
19
19
  Classifier: Programming Language :: Python :: 3.10
20
20
  Classifier: Programming Language :: Python :: 3.11
21
21
  Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
22
23
  Classifier: Topic :: Internet :: WWW/HTTP
23
24
  Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
24
25
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
@@ -1,8 +1,10 @@
1
1
  [project]
2
2
  name = "sanic-api"
3
- version = "0.4.1"
3
+ version = "0.5.0"
4
4
  keywords = ["Sanic", "异步", "Sanic扩展"]
5
5
  description = "Sanic 框架实用API工具集,拥有自动生成文档、参数校验、配置的导入、日志功能的优化等功能,更好的助力接口的开发"
6
+ readme = "README.md"
7
+ license = "MIT"
6
8
  authors = [
7
9
  { name = "昊色居士", email = "xhrtxh@gmail.com" }
8
10
  ]
@@ -17,14 +19,12 @@ classifiers = [
17
19
  "Programming Language :: Python :: 3.10",
18
20
  "Programming Language :: Python :: 3.11",
19
21
  "Programming Language :: Python :: 3.12",
22
+ "Programming Language :: Python :: 3.13",
20
23
  "Topic :: Internet :: WWW/HTTP",
21
24
  "Topic :: Software Development :: Libraries :: Application Frameworks",
22
25
  "Topic :: Software Development :: Libraries :: Python Modules",
23
26
  ]
24
- license = "MIT"
25
- readme = "README.md"
26
- requires-python = ">= 3.10"
27
-
27
+ requires-python = ">=3.10"
28
28
  dependencies = [
29
29
  # sanic
30
30
  "sanic>=24.12.0",
@@ -48,23 +48,19 @@ homepage = "https://github.com/x-haose/sanic-api"
48
48
  repository = "https://github.com/x-haose/sanic-api"
49
49
  documentation = "https://github.com/x-haose/sanic-api"
50
50
 
51
+ [project.scripts]
52
+ sanic-api = "sanic_api:main"
53
+
51
54
  [build-system]
52
55
  requires = ["hatchling"]
53
56
  build-backend = "hatchling.build"
54
57
 
55
- [tool.rye]
56
- managed = true
57
- dev-dependencies = [
58
- "pre-commit>=4.0.1",
59
- "twine>=6.0.1",
60
- ]
58
+ [tool.hatch.build.targets.sdist]
61
59
  include = [
62
- "src/sanic_api/"
60
+ "src",
61
+ "examples",
63
62
  ]
64
63
 
65
- [tool.hatch.metadata]
66
- allow-direct-references = true
67
-
68
64
  [tool.hatch.build.targets.wheel]
69
65
  packages = ["src/sanic_api"]
70
66
 
@@ -141,14 +137,4 @@ fixable = ["ALL"]
141
137
  unfixable = []
142
138
 
143
139
  [tool.ruff.lint.per-file-ignores]
144
- "__init__.py" = ["F401"]
145
-
146
-
147
- [tool.rye.scripts]
148
- publish_testpypi = { cmd = "rye run twine upload -r testpypi dist/*" }
149
- publish_pypi = { cmd = "rye run twine upload dist/*" }
150
- sb = { cmd = "rye build --clean" }
151
- spt = { chain = ["sb", "publish_testpypi"] }
152
- sp = { chain = ["sb", "publish_pypi"] }
153
- check_i = { cmd = "rye run pre-commit install" }
154
- check = { cmd = "rye run pre-commit run --all-files" }
140
+ "__init__.py" = ["F401"]
@@ -1,3 +1,3 @@
1
1
  from sanic_api.logger.extension import LoggerExtend
2
2
 
3
- __version__ = "0.4.1"
3
+ __version__ = "0.5.0"
@@ -43,7 +43,7 @@ class BaseApp:
43
43
  # 默认启用sanic_ext里面的后台日志记录器
44
44
  motd_display = {"envornment": settings.envornment}
45
45
  config = {"access_log": settings.access_log, "motd_display": motd_display}
46
- if settings.mode == RunModeEnum.DEBNUG:
46
+ if settings.mode == RunModeEnum.DEBUG:
47
47
  config.update({"auto_reload": settings.auto_reload, "workers": 1, "debug": True})
48
48
  else:
49
49
  config.update({"fast": True, "auto_reload": False})
@@ -158,6 +158,7 @@ class BaseApp:
158
158
 
159
159
  """
160
160
  # app.config.LOGGING = True
161
+ app.config.INSPECTOR = True
161
162
  app.config.FALLBACK_ERROR_FORMAT = "json"
162
163
  self._setup_cors(app)
163
164
 
@@ -185,7 +186,7 @@ class BaseApp:
185
186
 
186
187
  """
187
188
  cors = ",".join(self.settings.cors.origins)
188
- if self.settings.mode == RunModeEnum.DEBNUG:
189
+ if self.settings.mode == RunModeEnum.DEBUG:
189
190
  app.config.CORS_SEND_WILDCARD = True
190
191
  app.config.CORS_SUPPORTS_CREDENTIALS = self.settings.cors.supports_credentials
191
192
  app.config.CORS_ORIGINS = cors or "*"
@@ -221,7 +222,11 @@ class BaseApp:
221
222
  retention=log_config.retention,
222
223
  compression=log_config.compression,
223
224
  loki_url=log_config.loki_url,
224
- loki_labels={"application": self.name, "envornment": self.settings.envornment},
225
+ loki_labels={
226
+ "application": self.name,
227
+ "envornment": self.settings.env,
228
+ },
229
+ log_in_json=log_config.log_in_json,
225
230
  )
226
231
  Extend.register(log_ext)
227
232
 
@@ -9,7 +9,7 @@ class RunModeEnum(EnumBase):
9
9
  运行模式
10
10
  """
11
11
 
12
- DEBNUG = EnumField("debug", desc="开发模式")
12
+ DEBUG = EnumField("debug", desc="开发模式")
13
13
  PRODUCTION = EnumField("prod", desc="生产模式")
14
14
 
15
15
 
@@ -36,6 +36,9 @@ class LoggerSettings(BaseModel):
36
36
  # loji的地址。如果存在,则会把日志推送给logki
37
37
  loki_url: HttpUrl | None = Field(default=None)
38
38
 
39
+ # 日志是否以json形式打印
40
+ log_in_json: bool | None = Field(default=None)
41
+
39
42
 
40
43
  class JsonRespSettings(BaseModel):
41
44
  """
@@ -83,11 +86,11 @@ class DefaultSettings(SettingsBase):
83
86
  port: int = Field(default=6969)
84
87
 
85
88
  # 运行模式
86
- mode: RunModeEnum = Field(default=RunModeEnum.DEBNUG)
89
+ mode: RunModeEnum = Field(default=RunModeEnum.DEBUG)
87
90
 
88
91
  # 运行环境,仅作为环境标识。
89
92
  # 尽量不要使用这个字段去做逻辑判断。请使用mode去进行判断,因为测试环境、预发布环境、生产环境都应属于生产模式模式
90
- envornment: str = Field(default="dev")
93
+ env: str = Field(default="dev")
91
94
 
92
95
  # 自动重载。生产模式强制关闭
93
96
  auto_reload: bool = Field(default=False)
@@ -33,6 +33,7 @@ class LoggerExtend(Extension):
33
33
  compression: str | None = None,
34
34
  loki_url: str | None = None,
35
35
  loki_labels: dict[str, str] | None = None,
36
+ log_in_json: bool = False,
36
37
  ):
37
38
  """
38
39
  Args:
@@ -43,6 +44,7 @@ class LoggerExtend(Extension):
43
44
  compression: 日志文件压缩格式: "gz", "bz2", "xz", "lzma", "tar", "tar.gz", "tar.bz2", "tar.xz", "zip"
44
45
  loki_url: 推送loki的url
45
46
  loki_labels:loki推送时的标签
47
+ log_in_json: 日志是否以json形式打印
46
48
 
47
49
  """
48
50
  self.app = app
@@ -52,6 +54,7 @@ class LoggerExtend(Extension):
52
54
  self.compression = compression
53
55
  self.loki_url = loki_url
54
56
  self.loki_labels = loki_labels
57
+ self.log_in_json: bool = log_in_json
55
58
  self.setup()
56
59
 
57
60
  def startup(self, bootstrap) -> None:
@@ -84,9 +87,12 @@ class LoggerExtend(Extension):
84
87
  )
85
88
 
86
89
  # 基本的控制台输出
87
- log_handlers = [
88
- {"sink": sys.stdout, "format": log_format, "colorize": True},
89
- ]
90
+ if self.log_in_json:
91
+ log_handlers = [{"sink": sys.stdout, "serialize": True}]
92
+ else:
93
+ log_handlers = [
94
+ {"sink": sys.stdout, "format": log_format, "colorize": True},
95
+ ]
90
96
 
91
97
  # 日志文件输出
92
98
  if self.log_file:
@@ -109,9 +115,9 @@ class LoggerExtend(Extension):
109
115
  loki_handler = LokiLoggerHandler(
110
116
  url=self.loki_url,
111
117
  labels=self.loki_labels or {},
112
- labelKeys={},
118
+ label_keys={},
113
119
  timeout=10,
114
- defaultFormatter=LoguruFormatter(),
120
+ default_formatter=LoguruFormatter(),
115
121
  )
116
122
  log_handlers.append(
117
123
  {
@@ -1,18 +0,0 @@
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,98 +0,0 @@
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
- - 接口响应统一化
@@ -1,10 +0,0 @@
1
- - ✨ feat: 一个新特性
2
- - ➕ improvement: 对现有特性的提升
3
- - 🐛 fix: 修复Bug
4
- - 🛠 refactor: 代码重构,注意和特性、重构区分开
5
- - 📚 docs: 变更文档
6
- - 💅 style: 修改格式,不影响功能,例如空格、代码格式等
7
- - 📈 perf: 性能提升
8
- - 🔧 ci: 修改ci相关配置、脚本等
9
- - ⏪ revert: 回退版本
10
- - 🗯 chore: 杂务,不属于以上类型,例如run build、引入或更新软件包等
@@ -1,64 +0,0 @@
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 Request
6
- from sanic_api.app import BaseApp
7
- from sanic_api.config import DefaultSettings
8
-
9
- user_blueprint = Blueprint("user", "/user")
10
-
11
-
12
- class UserInfoModel(BaseModel):
13
- user_id: int = Field(title="用户ID")
14
-
15
-
16
- class UserInfoResponse(BaseModel):
17
- user_name: str = Field(title="用户名")
18
-
19
-
20
- class UseLoginRequest(Request):
21
- """
22
- 用户登录
23
- 用户登录描述
24
- 这也是描述
25
- """
26
-
27
- form_data: UserInfoModel
28
-
29
-
30
- @user_blueprint.post("info")
31
- async def user_info(request: Request, json_data: UserInfoModel):
32
- """
33
- 获取用户信息
34
- """
35
- logger.info(f"data: {json_data}")
36
- info = UserInfoResponse(user_name="张三")
37
- return request.json_resp(info, server_code="0000", server_msg="查询成功")
38
-
39
-
40
- @user_blueprint.post("login")
41
- async def user_login(request: UseLoginRequest):
42
- """
43
- 用户登录
44
- """
45
- logger.info(f"user_id: {request.form_data.user_id}")
46
- return json(request.form_data.model_dump())
47
-
48
-
49
- class App(BaseApp):
50
- """
51
- 服务示例
52
- """
53
-
54
- async def setup_route(self, app: Sanic):
55
- api = Blueprint.group(url_prefix="api")
56
- api.append(user_blueprint)
57
- app.blueprint(api)
58
-
59
-
60
- if __name__ == "__main__":
61
- settings = DefaultSettings()
62
- settings.port = 9999
63
- settings.logger.loki_url = "http://127.0.0.1:23100/loki/api/v1/push"
64
- App.run(settings)