-
Hey! Is it possible somehow to do different links for multiple clients using redoc, docs swagger? We have a couple of clients and we would like some clients not to show the handler of other clients. |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 1 reply
-
Yes. How'd like to separate? By a list of tags? |
Beta Was this translation helpful? Give feedback.
-
@Kludex Yes, it is possible by tags, but I want to make different docs links, docs for each client, and not keep one common one. That is, to fasten certain routes to the link. |
Beta Was this translation helpful? Give feedback.
-
I found, sorry - https://fastapi.tiangolo.com/advanced/sub-applications/ |
Beta Was this translation helpful? Give feedback.
-
Well... In case you already have the whole setup ready... This function is probably more helpful: from typing import Dict, List, Optional
from fastapi import FastAPI, Request
from fastapi.openapi.docs import get_redoc_html, get_swagger_ui_html
from fastapi.responses import HTMLResponse
app = FastAPI()
app.router.tags = ["Potato"]
@app.get("/")
def home():
return "Hi"
def build_docs(
app: FastAPI,
prefix: str,
tags: List[str],
custom_openapi: Optional[Dict[str, str]] = {},
) -> None:
async def get_openapi(request: Request):
nonlocal custom_openapi
if prefix not in custom_openapi:
custom_openapi[prefix] = request.app.openapi()
# Remove not valid tags on openapi schema.
for path in custom_openapi[prefix]["paths"].copy():
for method in custom_openapi[prefix]["paths"][path].copy():
_tags = custom_openapi[prefix]["paths"][path][method]["tags"]
if not set(_tags) <= set(tags):
del custom_openapi[prefix]["paths"][path][method]
# Clean empty paths.
for path in custom_openapi[prefix]["paths"].copy():
if not custom_openapi[prefix]["paths"][path]:
del custom_openapi[prefix]["paths"][path]
return custom_openapi[prefix]
get_openapi.__name__ = get_openapi.__name__ + prefix
app.add_api_route(prefix + "/openapi.json", get_openapi, include_in_schema=False)
async def get_redoc() -> HTMLResponse:
return get_redoc_html(
openapi_url=prefix + "/openapi.json", title="Developer Documentation"
)
get_redoc.__name__ = get_redoc.__name__ + prefix
app.add_api_route(prefix + "/redoc", get_redoc, include_in_schema=False)
async def swagger_ui_html(req: Request) -> HTMLResponse:
return get_swagger_ui_html(
openapi_url=prefix + "/openapi.json",
title=app.title + " - Swagger UI",
oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
init_oauth=app.swagger_ui_init_oauth,
)
swagger_ui_html.__name__ = swagger_ui_html.__name__ + prefix
app.add_api_route(prefix + "/docs", swagger_ui_html, include_in_schema=False)
build_docs(app, "/hi", tags=["Potato"])
build_docs(app, "/hello", tags=["Potato"]) You can call EDIT: Maybe a |
Beta Was this translation helpful? Give feedback.
-
@Kludex Thanks! |
Beta Was this translation helpful? Give feedback.
-
Thanks for the help here @Kludex ! 👏 🙇 Thanks for reporting back and closing the issue @Exzentttt 👍
|
Beta Was this translation helpful? Give feedback.
-
@Kludex Your solution also replaces the default |
Beta Was this translation helpful? Give feedback.
Well... In case you already have the whole setup ready...
This function is probably more helpful: