Skip to main content

Sanic 框架实用API工具集,拥有自动生成文档、参数校验、配置的导入、日志功能的优化等功能,更好的助力接口的开发

Project description

MIT License Python Sanic

Sanic-API

让您的sanic服务程序更好的支持API文档、参数校验、日志打印、响应规范等

特性

  • 无需任何多余改动,全自动生成openapi文档,使用更加方便 (新版方案正在积极优化开发中)

  • 基于pydantic的参数校验器,让接口的请求及响应更符合你的预期,使用更方便

  • 使用loguru库代替官方logging日志库,并对访问日志进行扩展,支持写入文件及推送loki

  • 使用了基于pydantic-settings的项目配置方案,支持json、yml、ini、.env等多种格式

  • 对sanic的启动进行了简单的封装,可快速启动项目

截图

路线图

  • 全自动生成openapi文档

  • 编写详细文档

安装

使用 pip 安装 sanic-api

  pip install sanic-api

使用方法/示例

最小示例

from sanic_api.app import BaseApp


class App(BaseApp):
    """
    最小的sanic服务示例
    """


if __name__ == "__main__":
    App.run()

带参数校验的示例

from pydantic import BaseModel, Field
from sanic import Blueprint, Sanic, json
from sanic.log import logger

from sanic_api.api import BaseRespTml, Request
from sanic_api.app import BaseApp

user_blueprint = Blueprint("user", "/user")


class UserInfoModel(BaseModel):
    user_id: int = Field(title="用户ID")


class UserInfoResponse(BaseRespTml):
    user_name: str = Field(title="用户名")


class UseLoginRequest(Request):
    form_data: UserInfoModel


@user_blueprint.post("info")
async def user_info(request: Request, json_data: UserInfoModel):
    """
    获取用户信息
    """
    logger.info(f"data: {json_data}")
    info = UserInfoResponse(user_name="张三")
    info.temp_data.code = "0000"
    info.temp_data.msg = "查询成功"
    return info.resp()


@user_blueprint.post("login")
async def user_login(request: UseLoginRequest):
    """
    用户登录
    """
    logger.info(f"user_id: {request.form_data.user_id}")
    return json(request.form_data.model_dump())


class App(BaseApp):
    """
    服务示例
    """

    async def setup_route(self, app: Sanic):
        api = Blueprint.group(url_prefix="api")
        api.append(user_blueprint)
        app.blueprint(api)


if __name__ == "__main__":
    App.run()

开发

要部署这个项目,请先安装rye

  rye sync

文档

正在编写中,敬请期待

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sanic_api-0.5.0.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sanic_api-0.5.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file sanic_api-0.5.0.tar.gz.

File metadata

  • Download URL: sanic_api-0.5.0.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for sanic_api-0.5.0.tar.gz
Algorithm Hash digest
SHA256 b61ee304475e8f6f6e1e96a59f10f74007e17156636b788daa2a3165981bab74
MD5 121684a76cd934f2967be19b07a4dde8
BLAKE2b-256 7644868223c94be4eca86385db16078feaf2b99610fb62f0c985aafea0d39825

See more details on using hashes here.

File details

Details for the file sanic_api-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: sanic_api-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for sanic_api-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f1cd21c825d338db9b122f34631125a27db95d28f77b53863a19f98d85223642
MD5 2d7ee81c25f1ddedff4b7c7479a5da31
BLAKE2b-256 c4a242dda2817d96c6f2bd9d2fdc38383de150897e4da51ff20b7b4083a78446

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page