Skip to content

Commit a4adc5d

Browse files
committed
Create stub HTML rendered AsyncAPI docs
1 parent ad2395a commit a4adc5d

File tree

1 file changed

+87
-5
lines changed

1 file changed

+87
-5
lines changed

src/http_app/routes/asyncapi_docs.py

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,104 @@
1+
import json
2+
13
import pydantic_asyncapi as pa
24
from fastapi import APIRouter
5+
from starlette.responses import HTMLResponse
36

47
schema = pa.AsyncAPIV3(
5-
id="http://aa.aa.aa",
8+
asyncapi="3.0.0",
69
info=pa.v3.Info(
710
title="Bookstore API",
811
version="1.0.0",
912
description="test",
1013
),
1114
)
1215

13-
router = APIRouter(prefix="/asyncapi")
16+
router = APIRouter(prefix="/docs")
1417

1518
@router.get("/asyncapi.json", response_model_exclude_unset=True)
1619
def asyncapi_raw() -> pa.AsyncAPIV3:
1720
return schema
1821

19-
@router.get("/docs", response_model_exclude_unset=True)
20-
def asyncapi_docs() -> pa.AsyncAPIV3:
21-
return schema
22+
ASYNCAPI_JS_DEFAULT_URL = "https://unpkg.com/@asyncapi/[email protected]/browser/standalone/index.js"
23+
NORMALIZE_CSS_DEFAULT_URL = "https://cdn.jsdelivr.net/npm/modern-normalize/modern-normalize.min.css"
24+
ASYNCAPI_CSS_DEFAULT_URL = (
25+
"https://unpkg.com/@asyncapi/[email protected]/styles/default.min.css"
26+
)
27+
28+
29+
# https://github.com/asyncapi/asyncapi-react/blob/v2.5.0/docs/usage/standalone-bundle.md
30+
@router.get("/asyncapi")
31+
def get_asyncapi_html(
32+
sidebar: bool = True,
33+
info: bool = True,
34+
servers: bool = True,
35+
operations: bool = True,
36+
messages: bool = True,
37+
schemas: bool = True,
38+
errors: bool = True,
39+
expand_message_examples: bool = True,
40+
title: str = "Bookstore API",
41+
) -> HTMLResponse:
42+
"""Generate HTML for displaying an AsyncAPI document."""
43+
schema_json = schema.model_dump_json(exclude_unset=True)
44+
45+
config = {
46+
"schema": schema_json,
47+
"config": {
48+
"show": {
49+
"sidebar": sidebar,
50+
"info": info,
51+
"servers": servers,
52+
"operations": operations,
53+
"messages": messages,
54+
"schemas": schemas,
55+
"errors": errors,
56+
},
57+
"expand": {
58+
"messageExamples": expand_message_examples,
59+
},
60+
"sidebar": {
61+
"showServers": "byDefault",
62+
"showOperations": "byDefault",
63+
},
64+
},
65+
}
66+
67+
return HTMLResponse("""
68+
<!DOCTYPE html>
69+
<html>
70+
<head>
71+
"""
72+
f"""
73+
<title>{title} AsyncAPI</title>
74+
"""
75+
"""
76+
<link rel="icon" href="https://www.asyncapi.com/favicon.ico">
77+
<link rel="icon" type="image/png" sizes="16x16" href="https://www.asyncapi.com/favicon-16x16.png">
78+
<link rel="icon" type="image/png" sizes="32x32" href="https://www.asyncapi.com/favicon-32x32.png">
79+
<link rel="icon" type="image/png" sizes="194x194" href="https://www.asyncapi.com/favicon-194x194.png">
80+
"""
81+
f"""
82+
<link rel="stylesheet" href="{NORMALIZE_CSS_DEFAULT_URL}">
83+
<link rel="stylesheet" href="{ASYNCAPI_CSS_DEFAULT_URL}">
84+
"""
85+
"""
86+
</head>
87+
2288
89+
<body>
90+
<div id="asyncapi"></div>
91+
"""
92+
f"""
93+
<script src="{ASYNCAPI_JS_DEFAULT_URL}"></script>
94+
<script>
95+
"""
96+
f"""
97+
AsyncApiStandalone.render({json.dumps(config)}, document.getElementById('asyncapi'));
98+
"""
99+
"""
100+
</script>
101+
</body>
102+
</html>
103+
"""
104+
)

0 commit comments

Comments
 (0)