gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:39:56 25 4
gpt4 key购买 nike

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

我的google search导致没有什么帮助的 rppages - Flask 应用程序解决了一个类似的问题 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

最佳答案

下面的代码将返回一个包含 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类来实现类似的功能。

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

您可以拥有list of routes并通过扩展 API class 添加它们

URL 和资源将类似于:

from v1.v1_app import things

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

关于python - 获取 Falcon 应用程序中定义的所有路由的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53559115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com