获取 Falcon 应用程序中定义的所有路线的列表

Get list of all routes defined in the Falcon app

我在 Falcon 应用程序中有 RESTful 条路线定义如下简化代码。 我的问题是如何获取所有路由及其映射处理程序的列表?

我的 google search results in little-helpful rpages - one similar issue resolved for Flask app here 但没有页面谈论猎鹰。

api = falcon.API(middleware=middleware)
api.add_route('/v1/model_names1', SomeHandlerMappingResource1())
api.add_route('/v1/model_names2', SomeHandlerMappingResource2())

class SomeHandlerMappingResource1:
    def on_get(self, req, resp):
        pass # some biz logic of GET method
    def on_post(self, req, resp):
        pass # some biz logic of POST method
    # etc.

class SomeHandlerMappingResource2:
    pass # similar to handler resource 1 above 

下面的代码将 return 包含 URL 及其各自资源的元组列表:

def get_all_routes(api):
    routes_list = []

    def get_children(node):
        if len(node.children):
            for child_node in node.children:
                get_children(child_node)
        else:
            routes_list.append((node.uri_template, node.resource))
    [get_children(node) for node in api._router._roots]
    return routes_list

输出

[
    ('/v1/things', <v1.v1_app.ThingsResource object at 0x7f555186de10>), 
    ('/v2/things', <v2.v2_app.ThingsResource object at 0x7f5551871470>), 
    ('/v3/things/{name}', <v3.v3_app.ThingsResource object at 0x7f5551871ba8>)
]

I have read the package and derived this, however, I don't know any builtin method that will return this result.


如果你不喜欢上面的功能,你可以通过扩展 API class.

来实现类似的功能

我制作了一个 Github 存储库来对 Falcon 应用程序进行版本控制,您可以从中了解如何将 URL 及其相关资源分开。 Github Link

你可以有一个list of routes and add them by Extended the API class

URLs 和资源将像:

from v1.v1_app import things

urls = [
    ('/things', things),
]