feat(docs.py): add stoplight elements as docs tool

This commit is contained in:
Shahar Ilany 2022-07-18 22:57:17 +03:00
parent 50fb34bf55
commit 25b57844bc
1 changed files with 64 additions and 0 deletions

View File

@ -1,5 +1,6 @@
import json
from typing import Any, Dict, Optional
from enum import Enum
from fastapi.encoders import jsonable_encoder
from starlette.responses import HTMLResponse
@ -185,3 +186,66 @@ def get_swagger_ui_oauth2_redirect_html() -> HTMLResponse:
</script>
"""
return HTMLResponse(content=html)
class TryItCredentialPolicyOptions(Enum):
OMIT = "omit"
include = "include"
SAME_ORIGIN = "same-origin"
class LayoutOptions(Enum):
SIDEBAR = "sidebar"
STACKED = "stacked"
class RouterOptions(Enum):
HISTORY = "history"
HASH = "hash"
MEMORY = "memory"
STATIC = "static"
def get_stoplight_elements_html(
*,
openapi_url: str,
title: str,
stoplight_elements_js_url: str = "https://unpkg.com/@stoplight/elements/web-components.min.js",
stoplight_elements_css_url: str = "https://unpkg.com/@stoplight/elements/styles.min.css",
stoplight_elements_favicon_url: str = "https://fastapi.tiangolo.com/img/favicon.png",
api_description_document: str = None,
base_path: str = None,
hide_internal:bool = False,
hide_try_it:bool = False,
try_it_cors_proxy:str = None,
try_it_credential_policy: TryItCredentialPolicyOptions = TryItCredentialPolicyOptions.OMIT,
layout: LayoutOptions = LayoutOptions.SIDEBAR,
logo: str = None,
router: RouterOptions = RouterOptions.HISTORY
) -> HTMLResponse:
html = f"""
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>{title}</title>
<link rel="shortcut icon" href="{stoplight_elements_favicon_url}">
<script src="{stoplight_elements_js_url}"></script>
<link rel="stylesheet" href="{stoplight_elements_css_url}">
</head>
<body>
<elements-api
{f'apiDescriptionUrl="{openapi_url}"' if openapi_url is not None else ''}
{f'apiDescriptionDocument="{api_description_document}"' if api_description_document is not None else ''}
{f'basePath="{base_path}"' if base_path is not None else ''}
{'hideInternal="true"' if hide_internal == True else ''}
{'hideTryIt="true"' if hide_try_it == True else ''}
{f'tryItCorsProxy="{try_it_cors_proxy}"' if try_it_cors_proxy is not None else ''}
tryItCredentialPolicy="{try_it_credential_policy}"
layout="{layout}"
{f'logo="{logo}"' if logo is not None else ''}
router="{router}"
/>
</body>
</html>
"""
return HTMLResponse(html)