How to iterate over api routes in a different file than the app initialization file

本小妞迷上赌 提交于 2019-12-11 13:19:54

问题


I have an apiroutes.py file which has many the routes defined like:

@api.route('/api/read', methods = ['GET'])
@api.route('/api/write', methods = ['POST'])

where api is an import "from . import api"

I have registered my application in init.py using

app = Flask(__name__)
from .api_1 import api as api_blueprint
app.register_blueprint(api_blueprint)

where .api_1 is folder name where I have apiroutes.py file located.I want to insert a new API which gives me information of all the apiroutes in a json format and I already have a working API for that as follows:

 @app.route('/api/help', methods = ['GET'])
    def help():
    func_list = {}
    for rule in app.url_map.iter_rules():
        if rule.endpoint != 'static':
            docstring = app.view_functions[rule.endpoint].__doc__
            if docstring:
                func_list[docstring] = rule.rule
    return jsonify(func_list)

I am able to hit a GET request to api/help and it reads all the information I have in '''api_information''' of all the API's I have in the same file annotated by app.route

My question is where should I put this function in order to read routes in apiroutes.py file? In apiroutes.py file all the routes are annotated as api.route and my code needs annotation app.route to read all the api routes information.

Thanks

来源:https://stackoverflow.com/questions/36516408/how-to-iterate-over-api-routes-in-a-different-file-than-the-app-initialization-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!