Coverage for faststream/asgi/factories/asyncapi/docs.py: 90%
8 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
1from collections.abc import Sequence
2from typing import TYPE_CHECKING, Any, Union
4from faststream.asgi.handlers import GetHandler, get
5from faststream.asgi.response import AsgiResponse
6from faststream.asgi.types import Scope
7from faststream.specification.asyncapi.site import (
8 ASYNCAPI_CSS_DEFAULT_URL,
9 ASYNCAPI_JS_DEFAULT_URL,
10 ASYNCAPI_TRY_IT_PLUGIN_URL,
11 get_asyncapi_html,
12)
14if TYPE_CHECKING:
15 from faststream.specification.base import SpecificationFactory
16 from faststream.specification.schema import Tag, TagDict
19def make_asyncapi_asgi(
20 schema: "SpecificationFactory",
21 description: str | None = None,
22 tags: Sequence[Union["Tag", "TagDict", dict[str, Any]]] | None = None,
23 unique_id: str | None = None,
24 include_in_schema: bool = True,
25 *,
26 sidebar: bool = True,
27 info: bool = True,
28 servers: bool = True,
29 operations: bool = True,
30 messages: bool = True,
31 schemas: bool = True,
32 errors: bool = True,
33 expand_message_examples: bool = True,
34 asyncapi_js_url: str = ASYNCAPI_JS_DEFAULT_URL,
35 asyncapi_css_url: str = ASYNCAPI_CSS_DEFAULT_URL,
36 try_it_out_plugin_url: str = ASYNCAPI_TRY_IT_PLUGIN_URL,
37 try_it_out_path: str | None = "asyncapi/try",
38) -> "GetHandler":
39 """Create AsyncAPI documentation ASGI handler."""
40 cached_docs: str | None = None
42 @get(
43 include_in_schema=include_in_schema,
44 description=description,
45 tags=tags,
46 unique_id=unique_id,
47 )
48 async def docs(scope: Scope) -> AsgiResponse: # ruff: ignore[unused-function-argument]
49 nonlocal cached_docs
50 if not cached_docs: 50 ↛ 67line 50 didn't jump to line 67 because the condition on line 50 was always true
51 cached_docs = get_asyncapi_html(
52 schema.to_specification(),
53 sidebar=sidebar,
54 info=info,
55 servers=servers,
56 operations=operations,
57 messages=messages,
58 schemas=schemas,
59 errors=errors,
60 expand_message_examples=expand_message_examples,
61 asyncapi_js_url=asyncapi_js_url,
62 asyncapi_css_url=asyncapi_css_url,
63 try_it_out_plugin_url=try_it_out_plugin_url,
64 try_it_out_path=try_it_out_path,
65 )
67 return AsgiResponse(
68 cached_docs.encode("utf-8"),
69 200,
70 {"Content-Type": "text/html; charset=utf-8"},
71 )
73 return docs