async def get_docs( api_key: str, user: User = Depends(get_current_user) ): if api_key: return get_swagger_ui_html(openapi_url=f"/api/{api_key}/openapi.json", title="fastAPI Docs") else: return get_swagger_ui_html(openapi_url=f"/api/openapi.json", title="fastAPI Docs")
def setup(self) -> None: if self.openapi_url: self.add_route( self.openapi_url, lambda req: JSONResponse(self.openapi()), include_in_schema=False, ) if self.openapi_url and self.docs_url: self.add_route( self.docs_url, lambda r: get_swagger_ui_html( openapi_url=self.openapi_prefix + self.openapi_url, title=self.title + " - Swagger UI", ), include_in_schema=False, ) if self.openapi_url and self.redoc_url: self.add_route( self.redoc_url, lambda r: get_redoc_html( openapi_url=self.openapi_prefix + self.openapi_url, title=self.title + " - ReDoc", ), include_in_schema=False, ) self.add_exception_handler(HTTPException, http_exception)
async def get_documentation(api_key: APIKey = Depends(get_api_key)): response = get_swagger_ui_html( openapi_url= f"/api/openapi.json?{settings.API_KEY_NAME}={settings.API_KEY}", title="Documentation", ) return response
async def custom_swagger_ui_html(): return get_swagger_ui_html( openapi_url=app.openapi_url, title=f'{app.title} - Swagger UI', swagger_js_url='/static/js/swagger-ui-bundle.js', swagger_css_url='/static/css/swagger-ui.css' )
def custom_swagger_ui_html(): return get_swagger_ui_html( openapi_url=subapi.openapi_url, title=subapi.title + " - Swagger UI", swagger_js_url="/static/swagger-ui-bundle.js", swagger_css_url="/static/swagger-ui.css", )
async def custom_swagger_ui_html(): return get_swagger_ui_html( openapi_url=app.openapi_url, title=app.title, swagger_favicon_url="/static/favicon.ico", oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url, )
async def custom_swagger_ui_html(): return get_swagger_ui_html( openapi_url=app.openapi_url, title=f"{app.title}[{sys.env}] + - Swagger UI", oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url, swagger_favicon_url=icon )
async def get_documentation( _: fastapimsal.UserIdentity = Depends(user_authenticated), ) -> HTMLResponse: """ Serves swagger API docs """ return get_swagger_ui_html(openapi_url="/openapi.json", title="docs")
async def swagger_ui_html(req: Request) -> HTMLResponse: return get_swagger_ui_html( openapi_url=openapi_url, title=self.title + " - Swagger UI", oauth2_redirect_url=self.swagger_ui_oauth2_redirect_url, init_oauth=self.swagger_ui_init_oauth, )
async def get_documentation(credentials: HTTPBasicCredentials = Depends(security)) -> HTMLResponse: if not _validate_credentials(credentials): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Incorrect email or password", headers={"WWW-Authenticate": "Basic"}, ) return get_swagger_ui_html(openapi_url="/openapi.json", title="docs")
async def custom_swagger_ui_html(): return get_swagger_ui_html( openapi_url=f"{settings.API_V1_STR}/openapi.json", title=webapp.title + " - Swagger UI", oauth2_redirect_url=webapp.swagger_ui_oauth2_redirect_url, swagger_js_url="/static/swagger-ui-bundle.js", swagger_css_url="/static/swagger-ui.css", )
def swagger_monkey_patch(*args, **kwargs): return get_swagger_ui_html( *args, **kwargs, swagger_js_url= "https://cdn.jsdelivr.net/npm/[email protected]/swagger-ui-bundle.js", swagger_css_url= "https://cdn.jsdelivr.net/npm/[email protected]/swagger-ui.css")
async def custom_swagger_ui_html(): return get_swagger_ui_html( openapi_url=data_service_app.openapi_url, title=data_service_app.title + " - Swagger UI", oauth2_redirect_url=data_service_app.swagger_ui_oauth2_redirect_url, swagger_js_url="/static/swagger-ui-bundle.js", swagger_css_url="/static/swagger-ui.css", )
async def custom_swagger_ui_html(): return get_swagger_ui_html( swagger_favicon_url="/static/favicon.ico", openapi_url=rest_app.openapi_url, title=rest_app.title, swagger_css_url="/static/swagger-ui.css", oauth2_redirect_url=rest_app.swagger_ui_oauth2_redirect_url, swagger_js_url="/static/swagger-ui-bundle.js")
async def swagger_ui_html(req: Request) -> HTMLResponse: return get_swagger_ui_html( openapi_url=app.openapi_url, title=app.title + ' - Swagger UI', oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url, swagger_js_url='/static/swagger-ui-bundle.js', swagger_css_url='/static/swagger-ui.css', )
async def swagger_ui_html(): return get_swagger_ui_html( title=f"{settings.PROJECT_NAME} - Swagger UI", openapi_url=app.openapi_url, swagger_js_url="/static/swagger-ui-bundle.js", swagger_css_url="/static/swagger-ui.css", swagger_favicon_url="/static/favicon.ico", )
async def custom_swagger_ui_html(): return get_swagger_ui_html( openapi_url=app.openapi_url, title="XMEME" + " - Swagger UI", oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url, swagger_js_url="/static/js/swagger-ui.js", swagger_css_url="/static/css/swagger-ui.css", )
async def custom_swagger_ui_html(): return get_swagger_ui_html( openapi_url=OPENAPI_URL, title=APP_TITLE + " - Swagger UI", oauth2_redirect_url=DOCS_AUTH_URL, swagger_js_url="/static/swagger-ui-bundle.js", swagger_css_url="/static/swagger-ui.css", )
async def custom_swagger_ui_html(): return get_swagger_ui_html( openapi_url=app.openapi_url, title=app.title + " - Swagger UI", oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url, swagger_js_url="/static/swagger-ui-bundle.js", swagger_css_url="/static/swagger-ui.css", swagger_favicon_url='/static/favicon.png')
async def get_documentation(): response = get_swagger_ui_html( openapi_url="/swaggerfile", title="docs", swagger_js_url="/static/js/swagger-ui-bundle.js", swagger_css_url="/static/css/swagger-ui.css", ) return response
async def custom_swagger_ui_html(): return get_swagger_ui_html( openapi_url="openapi.json", title="Fastapi Swagger UI", swagger_js_url="/static/js/swagger-ui-bundle.js", swagger_css_url="/static/css/swagger-ui.css", swagger_favicon_url="/static/images/esun.png", )
def swagger_monkey_patch(*args, **kwargs): """ Wrap the function which is generating the HTML for the /docs endpoint and overwrite the default values for the swagger js and css. """ return get_swagger_ui_html( *args, **kwargs, swagger_js_url="https://cdn.jsdelivr.net/npm/[email protected]/swagger-ui-bundle.js", swagger_css_url="https://cdn.jsdelivr.net/npm/[email protected]/swagger-ui.css")
async def custom_swagger_ui_html(): return get_swagger_ui_html( openapi_url=str(app.openapi_url), title=app.title + " - Swagger UI", oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url, swagger_js_url="/static/swagger-ui-bundle.js", swagger_css_url="/static/swagger-ui.css", swagger_favicon_url="/static/logo/trident_neptune_logo.ico", )
async def get_documentation(): response = get_swagger_ui_html( openapi_url="/swaggerfile", title="docs", # swagger_js_url="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.34.0/swagger-ui-bundle.min.js", swagger_js_url="/static/js/swagger-ui-bundle.min.js", swagger_css_url="/static/css/swagger-ui.css", ) return response
async def custom_swagger_ui_html() -> HTMLResponse: return get_swagger_ui_html( openapi_url=settings.openapi_file, title=settings.title + " - Swagger UI", oauth2_redirect_url=settings.oauth2_redirect_url, swagger_js_url="/static/docs/swagger-ui-bundle.js", swagger_css_url="/static/docs/swagger-ui.css", swagger_favicon_url="/static/favicon.ico", )
def test_strings_in_generated_swagger(): sig = inspect.signature(get_swagger_ui_html) swagger_js_url = sig.parameters.get("swagger_js_url").default swagger_css_url = sig.parameters.get("swagger_css_url").default swagger_favicon_url = sig.parameters.get("swagger_favicon_url").default html = get_swagger_ui_html(openapi_url="/docs", title="title") body_content = html.body.decode() assert swagger_js_url in body_content assert swagger_css_url in body_content assert swagger_favicon_url in body_content
async def get_custom_swagger_ui_html() -> HTMLResponse: """Get a custom Swagger UI HTML. :return: Custom Swagger UI HTML """ return get_swagger_ui_html( openapi_url=app.openapi_url, title=app.title + " - Swagger UI", swagger_css_url="/assets/public/css/swagger-ui.css", swagger_favicon_url=swagger_favicon_url)
async def get_documentation(api_key: APIKey = Depends(get_api_key)): response = get_swagger_ui_html(openapi_url="/openapi.json", title="docs") response.set_cookie( API_KEY_NAME, value=api_key, httponly=True, max_age=1800, expires=1800, ) return response
async def custom_swagger_ui_html(req: Request) -> HTMLResponse: """Customize Swagger UI.""" root_path = req.scope.get("root_path", "").rstrip("/") openapi_url = root_path + APP.openapi_url swagger_favicon_url = root_path + "/static/favicon.svg" return get_swagger_ui_html( openapi_url=openapi_url, title=APP.title + " - Swagger UI", oauth2_redirect_url=APP.swagger_ui_oauth2_redirect_url, swagger_favicon_url=swagger_favicon_url, )
async def swagger_ui_html(req: Request) -> HTMLResponse: openapi_url = app.openapi_prefix + app.openapi_url return get_swagger_ui_html( openapi_url=openapi_url, title=app.title + " - Swagger UI", oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url, init_oauth=app.swagger_ui_init_oauth, swagger_js_url="/static/swagger-ui-bundle.js", swagger_css_url="/static/swagger-ui.css", swagger_favicon_url="/static/favicon.png" )