import json from typing import Annotated, Any from annotated_doc import Doc from fastapi.encoders import jsonable_encoder from starlette.responses import HTMLResponse def _html_safe_json(value: Any) -> str: """Serialize a value to JSON with HTML special characters escaped. This prevents injection when the JSON is embedded inside a """ return HTMLResponse(html) def get_redoc_html( *, openapi_url: Annotated[ str, Doc( """ The OpenAPI URL that ReDoc should load and use. This is normally done automatically by FastAPI using the default URL `/openapi.json`. Read more about it in the [FastAPI docs for Conditional OpenAPI](https://fastapi.tiangolo.com/how-to/conditional-openapi/#conditional-openapi-from-settings-and-env-vars) """ ), ], title: Annotated[ str, Doc( """ The HTML `` content, normally shown in the browser tab. Read more about it in the [FastAPI docs for Custom Docs UI Static Assets](https://fastapi.tiangolo.com/how-to/custom-docs-ui-assets/) """ ), ], redoc_js_url: Annotated[ str, Doc( """ The URL to use to load the ReDoc JavaScript. It is normally set to a CDN URL. Read more about it in the [FastAPI docs for Custom Docs UI Static Assets](https://fastapi.tiangolo.com/how-to/custom-docs-ui-assets/) """ ), ] = "https://cdn.jsdelivr.net/npm/redoc@2/bundles/redoc.standalone.js", redoc_favicon_url: Annotated[ str, Doc( """ The URL of the favicon to use. It is normally shown in the browser tab. """ ), ] = "https://fastapi.tiangolo.com/img/favicon.png", with_google_fonts: Annotated[ bool, Doc( """ Load and use Google Fonts. """ ), ] = True, redoc_ui_parameters: Annotated[ Optional[Dict[str, Any]], Doc( """ Configuration parameters for ReDoc It defaults to None. """ ), ] = None, ) -> HTMLResponse: """ Generate and return the HTML response that loads ReDoc for the alternative API docs (normally served at `/redoc`). You would only call this function yourself if you needed to override some parts, for example the URLs to use to load ReDoc's JavaScript and CSS. Read more about it in the [FastAPI docs for Custom Docs UI Static Assets (Self-Hosting)](https://fastapi.tiangolo.com/how-to/custom-docs-ui-assets/). """ config_string = "" if redoc_ui_parameters: for key, value in redoc_ui_parameters.items(): config_string += f' {key}="{value}"' html = f""" <!DOCTYPE html> <html> <head> <title>{title} """ if with_google_fonts: html += """ """ html += f""" """ return HTMLResponse(html) def get_swagger_ui_oauth2_redirect_html() -> HTMLResponse: """ Generate the HTML response with the OAuth2 redirection for Swagger UI. You normally don't need to use or change this. """ # copied from https://github.com/swagger-api/swagger-ui/blob/v4.14.0/dist/oauth2-redirect.html html = """ Swagger UI: OAuth2 Redirect """ return HTMLResponse(content=html)