我正在使用sanic构建简单的API。我定义了两个端点:
from sanic import Blueprint, response
from sanic_openapi import doc
bp = Blueprint('default', url_prefix="", version=2, strict_slashes=True)
@bp.get("")
@doc.summary('Default GET endpoint for API v2')
async def route_get(request):
resp = {"message": "New API - GET", "version": 2.0}
return response.json(resp)
@bp.post("")
@doc.summary('Default POST endpoint for API v2')
async def route_post(request):
resp = {"message": "New API - POST", "version": 2.0}
return response.json(resp)
生成的swagger文档验证失败,并显示以下消息
错误:
"attribute paths.'/v2/'(post).operationId is repeated",
我希望有一些路由,这些路由具有多个HTTP动词,它们都指向同一路径:
GET /v2/product
POST /v2/product
DELETE /v2/product/{id}
PUT /v2/product/{id}
我也已经使用这些端点进行了测试,并且遇到了有关重复operationId的两个错误。一种用于/v2/product
路径,另一种用于/v2/product/{id}
如何解决此错误?
答案 0 :(得分:1)
类似下面的代码可以帮助您,以便您可以将相同的路径分别用于POST,GET,PUT,DELETE并编写自己的逻辑。
我对您的代码做了一些修改,下面是代码:
from sanic import Blueprint, response
from sanic_openapi import doc
bp = Blueprint('default', url_prefix="", version=2, strict_slashes=True)
@bp.get("/<product_id:int>", strict_slashes=True)
@doc.summary('Default GET endpoint for API v2')
async def route_get(request):
resp = {"message": "New API - GET", "version": 2.0}
return response.json(resp)
@bp.put("/<product_id:int>", strict_slashes=True)
@doc.summary('Default POST endpoint for API v2')
async def route_post(request):
resp = {"message": "New API - POST", "version": 2.0}
return response.json(resp)
如果必须将model对象与product一起使用,则可以使用类似以下的内容,我已经创建了完整的示例供您参考。
文件夹结构:
Project
blueprints
product.py
data.py
main.py
models.py
product.py
from sanic.blueprints import Blueprint
from sanic.response import json
from sanic_openapi import doc
from models import Product
from data import test_product
blueprint = Blueprint('Products', '/products')
@blueprint.get("/<product_id:int>", strict_slashes=True)
@doc.summary("Fetches a product with product Id")
def route_get(request, product_id):
resp = {"message": "New API - GET", "version": 2.0}
return json(resp)
@blueprint.put("/<product_id:int>", strict_slashes=True)
@doc.summary("Updates a Product with the Updated product Details from the contends from Body")
@doc.consumes(Product, location='body')
def route_post(request, product_id):
resp = {"message": "New API - POST", "version": 2.0}
return json(resp)
data.py
from models import Product
import datetime
test_product = Product()
test_product = {
'id': 1,
'name': 'Gross'
}
main.py
from sanic import Sanic
from sanic_openapi import swagger_blueprint
from blueprints.product import blueprint as product_blueprint
app = Sanic()
app.blueprint(product_blueprint)
app.config.API_VERSION = '1.0.0'
app.config.API_TITLE = 'Product API'
app.run(host="0.0.0.0", debug=True)
models.py
from sanic_openapi import doc
class Product:
id = int
name = str
现在运行python main.py
控制台输出:
[2019-11-22 16:20:53 +0530] [15744] [INFO] Goin' Fast @ http://0.0.0.0:8000
[2019-11-22 16:20:53 +0530] [15744] [WARNING] Sanic tried to use loop.add_signal_handler but it is not implemented on this platform.
[2019-11-22 16:20:53 +0530] [15744] [WARNING] Sanic tried to use loop.add_signal_handler but it is not implemented on this platform.
[2019-11-22 16:20:53 +0530] [15744] [INFO] Starting worker [15744]
使用API访问Swagger UI:
如果您必须使用基于类的视图Demonstration,以下是两个可以提供帮助的文件。
Project Structure
Project
main.py
blueprint.py
main.py
from sanic_openapi import doc
from sanic_openapi import swagger_blueprint
from sanic import Sanic
from sanic.response import json
from blueprint import blueprint
app = Sanic()
app.blueprint(swagger_blueprint)
app.blueprint(blueprint)
@app.get("/product", strict_slashes=True)
@doc.summary("Displays the Product Details ")
async def get_product(request):
return json({})
@app.post("/product", strict_slashes=True)
@doc.summary("Creates a product in Repositary")
@doc.consumes({"product": {"name": str}}, location="body")
async def create_product(request):
return json({})
app.config.API_VERSION = 'pre-alpha'
app.config.API_TITLE = 'Display Products Demonstration API'
app.run(host="0.0.0.0", debug=True)
blueprint.py
from sanic.blueprints import Blueprint
from sanic.views import HTTPMethodView
from sanic_openapi import doc
from sanic.response import json
blueprint = Blueprint('Class-based View', url_prefix='/class-based-view')
class MyView(HTTPMethodView):
@doc.summary("Get my view")
def get(self, request):
return json({"method": "GET"})
@doc.summary("Post my view")
@doc.consumes({"view": {"name": str}}, location="body")
def post(self, request):
return json({"method": "POST"})
blueprint.add_route(MyView.as_view(), '/view', strict_slashes=True)
现在运行python main.py
[2019-11-22 16:50:41 +0530] [12212] [INFO] Goin' Fast @ http://0.0.0.0:8000
[2019-11-22 16:50:41 +0530] [12212] [WARNING] Sanic tried to use loop.add_signal_handler but it is not implemented on this platform.
[2019-11-22 16:50:41 +0530] [12212] [WARNING] Sanic tried to use loop.add_signal_handler but it is not implemented on this platform.
[2019-11-22 16:50:41 +0530] [12212] [INFO] Starting worker [12212]
您现在可以在http://localhost:8000/swagger/上看到SWAGGER UI