aiohttp中UrlDispatcher的ResourcesView和RoutesView的作用是什么?

What's the purpose of UrlDispatcher's ResourcesView and RoutesView in aiohttp?

我正在查看 UrlDispatcher.resources() and UrlDispatcher.routes() 方法。

他们分别return ResourcesView and RoutesView对象。

我想知道,这些观点的目的是什么?

我能否使用它们以类似于 Django 或 Django REST Framework 的方式显示我的服务中可用的端点列表?

我假设,它们不是正确的网络视图,因为它们没有对应于 http 方法的方法,例如get(request) 并且不继承自 ViewAbstractView?

这些视图是用于 lookup/iterate 路由和资源的 immutable 对象。

喜欢 dict.keys()dict.items()

在内部,aiohttp 路由器按以下方式组织。

假设我们有一条路线 table,例如:

app.add_routes([
    web.get('/path1', handler1),
    web.post('/path1', handler2),
    web.get('/path2', handler3)])

内部我们有两个资源:/path1/path2

/path1 资源有两个用于 GETPOST HTTP 方法的路由。

/path2 只有 GET.

的路线

app.router.resources 允许迭代资源,app.router.routes 迭代更深一层。