[python3] tornado 使用swagger编写接口文档
- python3.12
- tornado 6.1
- swagger 1.2.3
pip install tornado-swagger==1.2.3
示例代码
import tornado.ioloop
import tornado.web
from tornado_swagger.setup import setup_swagger
class ItemHandler(tornado.web.RequestHandler):
def get(self, item_id):
"""
---
tags:
- Items
summary: Get an item by ID
description: Retrieve item details by its ID
parameters:
- name: item_id
in: path
required: true
description: ID of the item
type: integer
responses:
200:
description: Successful operation
schema:
type: object
properties:
name:
type: string
price:
type: number
404:
description: Item not found
"""
item = {"name": "ExampleItem", "price": 99.99}
print('item_id: ', item_id)
if int(item_id) == 1:
self.write(item)
else:
self.write({"error": "Item not found"})
def make_app():
routes = [
tornado.web.url(r"/item/([0-9]+)", ItemHandler),
]
setup_swagger(routes, swagger_url="/api/doc",
description="使用 setup_swagger 设置 Swagger 文档")
return tornado.web.Application(routes)
if __name__ == "__main__":
app = make_app()
app.listen(38881)
tornado.ioloop.IOLoop.current().start()
浏览器打开
http://IP:38881/api/doc
原文地址:https://blog.csdn.net/qq_44810930/article/details/143688646
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!