python中swagger入门
2022/2/23 14:22:21
本文主要是介绍python中swagger入门,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
安装命令:
pip install flasgger ``` 话不多说,直接上demo ```python import json from flask import Flask, request, Response from flasgger import swag_from, Swagger def create_app(): app = Flask(__name__, instance_relative_config=True) app_config = {} app.config.from_mapping(app_config) app.config['SWAGGER'] = { 'title': 'Swagger Callbacks', 'openapi': '3.0.3' } Swagger(app, template={ 'title': 'Swagger Callbacks', 'openapi': '3.0.3', "components": { "securitySchemes": { "BearerAuth": { "in": "header", "scheme": "bearer", "type": "http", "bearerFormat": "JWT" } } } }) return app app = create_app() @app.route('/api/<string:language>/', methods=['POST']) @swag_from('./swagger_form/yunnan/query_index.yml') def index(language): """ This is the language awesomeness API Call this api passing a language name and get back its features --- tags: - Awesomeness Language API parameters: - name: language in: path type: string required: true description: The language name - name: size in: query type: integer description: size of awesomeness responses: 500: description: Error The language is not awesome! 200: description: A language with its awesomeness schema: id: awesome properties: language: type: string description: The language name default: Lua features: type: array description: The awesomeness list items: type: string default: ["perfect", "simple", "lovely"] """ json_data = request.json # print(json_data, '*' * 50) # language = language.lower().strip() language = language.lower().strip() features = [ "awesome", "great", "dynamic", "simple", "powerful", "amazing", "perfect", "beauty", "lovely" ] size = int(request.args.get('size', 1)) if language in ['php', 'vb', 'visualbasic', 'actionscript']: return "An error occurred, invalid language for awesomeness", 500 # return jsonify( # language=language, # features=random.sample(features, size) # ) return Response(json.dumps({ "language": json_data, 'feature': features }), mimetype='application/json') app.run(port=8888, debug=True)
访问路由:
http://127.0.0.1:8888/apidocs/
query_index.yml
[Yunnan index] query to test query_index.yml 查询首页的信息 --- tags: - charge-index-yunnan #security: # - BearerAuth: [] requestBody: description: body required: true content: application/json: schema: example: {"PageNo": 1, "PageSize": 10, 'LastQueryTime': '2022-01-15'} responses: 200: description: index test content: application/json: schema: example: {} # "PageNo": 1, # "PageCount": 2, # "ItemSize": 3, # "StationInfos": [], # }
这篇关于python中swagger入门的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-20Python编程入门指南
- 2024-12-20Python编程基础与进阶
- 2024-12-19Python基础编程教程
- 2024-12-19python 文件的后缀名是什么 怎么运行一个python文件?-icode9专业技术文章分享
- 2024-12-19使用python 把docx转为pdf文件有哪些方法?-icode9专业技术文章分享
- 2024-12-19python怎么更换换pip的源镜像?-icode9专业技术文章分享
- 2024-12-19Python资料:新手入门的全面指南
- 2024-12-19Python股票自动化交易实战入门教程
- 2024-12-19Python股票自动化交易入门教程
- 2024-12-18Python量化入门教程:轻松掌握量化交易基础知识