mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 20:29:06 +00:00
server/www: simplify operationId in openapi and update www
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi_pagination import add_pagination
|
||||
from fastapi.routing import APIRoute
|
||||
from reflector.views.rtc_offer import router as rtc_offer_router
|
||||
from reflector.views.transcripts import router as transcripts_router
|
||||
from reflector.events import subscribers_startup, subscribers_shutdown
|
||||
@@ -49,6 +50,42 @@ app.include_router(rtc_offer_router)
|
||||
app.include_router(transcripts_router, prefix="/v1")
|
||||
add_pagination(app)
|
||||
|
||||
|
||||
# simpler openapi id
|
||||
def use_route_names_as_operation_ids(app: FastAPI) -> None:
|
||||
"""
|
||||
Simplify operation IDs so that generated API clients have simpler function
|
||||
names.
|
||||
|
||||
Should be called only after all routes have been added.
|
||||
"""
|
||||
ensure_uniq_operation_ids = set()
|
||||
for route in app.routes:
|
||||
if isinstance(route, APIRoute):
|
||||
# opid
|
||||
|
||||
# extract version out of path if exists
|
||||
# /v1/transcripts -> v1
|
||||
# /transcripts -> None
|
||||
version = None
|
||||
if route.path.startswith("/v"):
|
||||
version = route.path.split("/")[1]
|
||||
opid = f"{version}_{route.name}"
|
||||
else:
|
||||
opid = route.name
|
||||
|
||||
if opid in ensure_uniq_operation_ids:
|
||||
raise ValueError(
|
||||
f"Operation ID '{route.name}' is not unique. "
|
||||
"Please rename the route or the view function."
|
||||
)
|
||||
route.operation_id = opid
|
||||
ensure_uniq_operation_ids.add(route.name)
|
||||
|
||||
|
||||
use_route_names_as_operation_ids(app)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
|
||||
Reference in New Issue
Block a user