|
| 1 | +import json |
| 2 | + |
1 | 3 | import pydantic_asyncapi as pa
|
2 | 4 | from fastapi import APIRouter
|
| 5 | +from starlette.responses import HTMLResponse |
3 | 6 |
|
4 | 7 | schema = pa.AsyncAPIV3(
|
5 |
| - id="http://aa.aa.aa", |
| 8 | + asyncapi="3.0.0", |
6 | 9 | info=pa.v3.Info(
|
7 | 10 | title="Bookstore API",
|
8 | 11 | version="1.0.0",
|
9 | 12 | description="test",
|
10 | 13 | ),
|
11 | 14 | )
|
12 | 15 |
|
13 |
| -router = APIRouter(prefix="/asyncapi") |
| 16 | +router = APIRouter(prefix="/docs") |
14 | 17 |
|
15 | 18 | @router.get("/asyncapi.json", response_model_exclude_unset=True)
|
16 | 19 | def asyncapi_raw() -> pa.AsyncAPIV3:
|
17 | 20 | return schema
|
18 | 21 |
|
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 | +
|
22 | 88 |
|
| 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