Как я могу перечислить все определенные URL-пути в FastAPI? - PullRequest
1 голос
/ 01 августа 2020

Предположим, у меня есть проект FastAPI, содержащий более 100 конечных точек API. Как я могу перечислить все API / пути?

1 Ответ

1 голос
/ 01 августа 2020

Вы можете попробовать это,

from application import app

url_list = [
    {'path': route.path, 'name': route.name}
    for route in app.routes
]

здесь app - это экземпляр класса FastAPI.

Полный пример:

from fastapi import FastAPI

app = FastAPI()


@app.get(path='/', name='API Foo')
def foo():
    return {'message': 'this is API Foo'}


@app.post(path='/bar', name='API Bar')
def bar():
    return {'message': 'this is API Bar'}


@app.get('/url-list')
def get_all_urls():
    url_list = [
        {'path': route.path, 'name': route.name}
        for route in app.routes
    ]
    return url_list
...