Cat Feeder  1.0.0
The Cat feeder project
Loading...
Searching...
No Matches
elements_provider.py
Go to the documentation of this file.
1r"""
2# +==== BEGIN CatFeeder =================+
3# LOGO:
4# ..............(..../\
5# ...............)..(.')
6# ..............(../..)
7# ...............\‍(__)|
8# Inspired by Joan Stark
9# source https://www.asciiart.eu/
10# animals/cats
11# /STOP
12# PROJECT: CatFeeder
13# FILE: elements_provider.py
14# CREATION DATE: 26-11-2025
15# LAST Modified: 14:44:27 19-12-2025
16# DESCRIPTION:
17# This is the backend server in charge of making the actual website work.
18# /STOP
19# COPYRIGHT: (c) Cat Feeder
20# PURPOSE: Stoplight Elements documentation provider implementation.
21# // AR
22# +==== END CatFeeder =================+
23"""
24from fastapi import Request, Response
25from display_tty import Disp, initialise_logger
26from . import elements_constants as ELEMENTS_CONST
27from ...http_codes import HCI, HttpDataTypes
28from ...core.runtime_manager import RuntimeManager, RI
29from ...server_header import ServerHeaders
30from ...boilerplates import BoilerplateResponses, BoilerplateIncoming
31
32
34 """Stoplight Elements documentation provider.
35
36 Provides beautiful API documentation using Stoplight Elements web components.
37 """
38
39 disp: Disp = initialise_logger(__qualname__, False)
40
41 def __init__(self, openapi_url: str, api_title: str, debug: bool = False) -> None:
42 """Initialize Stoplight Elements provider.
43
44 Args:
45 openapi_url (str): URL to OpenAPI JSON schema.
46 api_title (str): Title of the API.
47 debug (bool, optional): Enable debug logging. Defaults to False.
48 """
49 self.disp.update_disp_debug(debug)
50 self.disp.log_debug("Initialising Stoplight Elements provider...")
51
52 self.openapi_url = openapi_url
53 self.api_title = api_title
54 self.runtime_manager: RuntimeManager = RI
55 self.server_headers_initialised: ServerHeaders = self.runtime_manager.get(
56 ServerHeaders)
57 self.boilerplate_responses_initialised: BoilerplateResponses = self.runtime_manager.get(
58 BoilerplateResponses)
59 self.boilerplate_incoming_initialised: BoilerplateIncoming = self.runtime_manager.get(
60 BoilerplateIncoming)
61
62 self.disp.log_debug("Stoplight Elements provider initialised")
63
64 async def get_documentation(self, request: Request) -> Response:
65 """Serve Stoplight Elements documentation page.
66
67 Args:
68 request (Request): The incoming request.
69
70 Returns:
71 Response: HTML response with Stoplight Elements interface.
72 """
73 func_title = "get_documentation"
74 self.disp.log_debug(
75 "Serving Stoplight Elements documentation", func_title)
76
77 token = self.boilerplate_incoming_initialised.get_token_if_present(
78 request)
79 self.disp.log_debug(f"token = {token}", func_title)
80
81 attributes_str = ""
82 for key, value in ELEMENTS_CONST.ELEMENTS_OPTIONS.items():
83 attr_name = key
84 if isinstance(value, bool):
85 if value:
86 attr_value = "true"
87 else:
88 attr_value = "false"
89 else:
90 attr_value = str(value)
91 attributes_str = attributes_str + \
92 f' {attr_name}="{attr_value}"\n'
93
94 html_content = f"""
95<!DOCTYPE html>
96<html lang="en">
97<head>
98 <meta charset="utf-8">
99 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
100 <title>{self.api_title} - Stoplight Elements</title>
101 <link rel="stylesheet" href="{ELEMENTS_CONST.ELEMENTS_CDN_CSS}">
102 <style>
103 body {{
104 margin: 0;
105 padding: 0;
106 }}
107 </style>
108</head>
109<body>
110 <elements-api
111 apiDescriptionUrl="{self.openapi_url}"
112 router="{ELEMENTS_CONST.ELEMENTS_ROUTER}"
113 layout="{ELEMENTS_CONST.ELEMENTS_LAYOUT}"
114{attributes_str} />
115 <script src="{ELEMENTS_CONST.ELEMENTS_CDN_JS}"></script>
116</body>
117</html>
118"""
119 return HCI.success(content=html_content, content_type=HttpDataTypes.HTML)
120
121 def get_url(self) -> str:
122 """Get the URL path for this documentation provider.
123
124 Returns:
125 str: The URL path.
126 """
127 return ELEMENTS_CONST.ELEMENTS_URL
None __init__(self, str openapi_url, str api_title, bool debug=False)