DeepHorizons
DeepHorizons

Reputation: 63

Getting flask-restful routes from within a blueprint

Is there a way to get the routes defined in a blueprint? I know this (http://flask.pocoo.org/snippets/117/) snippit exists, but requires to have the app initalized to use url_map. Is there a way to view the routes without an application? I'm developing an api with flask-restful and I would like to display the routes from within the blueprint without the app to keep it self contained.

Upvotes: 2

Views: 2502

Answers (1)

DeepHorizons
DeepHorizons

Reputation: 63

Using the information provided by polyfunc, I was able to come up with this solution:

from flask import Blueprint, current_app, url_for
from flask_restful import Resource, Api

api_blueprint = Blueprint('api', __name__)
api = Api(api_blueprint)

@api.resource('/foo')
class Foo(Resource):
    def get(self):
        prefix = api_blueprint.name + '.'  # registered classes will have this as their prefix

        # Get current rules
        rules = current_app.url_map.iter_rules()

        # Get list from routes registered under the blueprint
        routes = [url_for(rule.endpoint) for rule in rules if rule.endpoint.startswith(prefix)]

Upvotes: 1

Related Questions