sanic-openapi, 使用UI轻松记录你的Sanic API

分享于 

3分钟阅读

GitHub

  繁體 雙語
Easily document your Sanic API with a UI
  • 源代码名称:sanic-openapi
  • 源代码网址:http://www.github.com/channelcat/sanic-openapi
  • sanic-openapi源代码文档
  • sanic-openapi源代码下载
  • Git URL:
    git://www.github.com/channelcat/sanic-openapi.git
    Git Clone代码到本地:
    git clone http://www.github.com/channelcat/sanic-openapi
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/channelcat/sanic-openapi
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    Sanic OpenAPI

    Build StatusPyPIPyPI

    为 Sanic API提供一个UI和OpenAPI文档,所有这些都是免费 !

    Example Swagger UI

    安装

    pip install sanic-openapi

    添加OpenAPI和Swagger用户界面:

    from sanic_openapi import swagger_blueprint, openapi_blueprint
    app.blueprint(openapi_blueprint)
    app.blueprint(swagger_blueprint)

    你现在在 URL /swagger 中有一个Swagger的UI。
    你的路线将由他们的蓝图自动分类。

    示例

    有关 Swagger UI的示例,请参见宠物存储库。

    用法

    使用简单的decorator来记录路由:

    from sanic_openapi import doc@app.get("/user/<user_id:int>")@doc.summary("Fetches a user by ID")@doc.produces({ "user": { "name": str, "id": int } })asyncdefget_user(request, user_id):
     ...@app.post("/user")@doc.summary("Creates a user")@doc.consumes({"user": { "name": str }}, location="body")asyncdefcreate_user(request):
     ...

    你的输入/输出模型

    classCar:
     make =str model =str year =intclassGarage:
     spaces =int cars = [Car]@app.get("/garage")@doc.summary("Gets the whole garage")@doc.produces(Garage)asyncdefget_garage(request):
     return json({
     "spaces": 2,
     "cars": [{"make": "Nissan", "model": "370Z"}]
     })

    获得更多描述

    classCar:
     make = doc.String("Who made the car")
     model = doc.String("Type of car. This will vary by make")
     year = doc.Integer("4-digit year of the car", required=False)classGarage:
     spaces = doc.Integer("How many cars can fit in the garage")
     cars = doc.List(Car, description="All cars in the garage")

    配置所有内容

    app.config.API_VERSION='1.0.0'app.config.API_TITLE='Car API'app.config.API_DESCRIPTION='Car API'app.config.API_TERMS_OF_SERVICE='Use with caution!'app.config.API_PRODUCES_CONTENT_TYPES= ['application/json']
    app.config.API_CONTACT_EMAIL='channelcat@gmail.com'

    API  DOC  SAN  
    相关文章