我想为python应用程序生成openAPI规范。我可以生成swagger.json
import logging
import json
from apispec import APISpec
from init import get_slack_url
from flask import Flask
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.flask import FlaskPlugin
logging.basicConfig(level=logging.DEBUG)
hook_url = get_slack_url()
spec = APISpec(
title="Notification Service",
version="1.0.0",
openapi_version="3.0.2",
plugins=[FlaskPlugin(), MarshmallowPlugin()]
)
app = Flask(__name__)
@app.route('/slack_notification', methods=['POST'])
def send_slack_notification():
return 'wathever'
with app.test_request_context():
spec.path(view=send_slack_notification)
with open('swagger.json', 'w') as f:
json.dump(spec.to_dict(), f)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001, debug=False)
swagger.json
被生成。
{"paths": {"/slack_notification": {}}, "info": {"title": "Notification Service", "version": "1.0.0"}, "openapi": "3.0.2"}
但我无法加载swagger
或apidocs
端点。我收到错误。
INFO:werkzeug: * Running on http://0.0.0.0:5001/ (Press CTRL+C to quit)
INFO:werkzeug:127.0.0.1 - - [27/Oct/2021 21:03:50] "GET /swagger/ HTTP/1.1" 404 -
INFO:werkzeug:127.0.0.1 - - [27/Oct/2021 21:03:53] "GET /openapi HTTP/1.1" 404 -
INFO:werkzeug:127.0.0.1 - - [27/Oct/2021 21:03:57] "GET / HTTP/1.1" 404 -
关于如何在UI上看到这一点,有什么建议吗?
我尝试的另一种选择是Flasgger。我可以看到UI,但规格有swagger: 2.0
,我特别想要OpenApi 3.0
。
参考:https://donofden.com/blog/2020/06/14/Python-Flask-automatically-generated-Swagger-3-0-openapi-Document
1条答案
按热度按时间0ejtzxu11#
我可以尝试使用这个方法,它类似于FastAPI
https://pypi.org/project/flask-toolkits/