Cat Feeder  1.0.0
The Cat feeder project
Loading...
Searching...
No Matches
editor_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: editor_provider.py
14# CREATION DATE: 26-11-2025
15# LAST Modified: 14:44:9 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: Swagger Editor documentation provider implementation.
21# // AR
22# +==== END CatFeeder =================+
23"""
24from fastapi import Request, Response
25from display_tty import Disp, initialise_logger
26from . import editor_constants as EDITOR_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 """Swagger Editor documentation provider.
35
36 Provides interactive Swagger Editor for viewing and editing OpenAPI specs.
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 Swagger Editor 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 Swagger Editor 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("Swagger Editor provider initialised")
63
64 async def get_documentation(self, request: Request) -> Response:
65 """Serve Swagger Editor documentation page.
66
67 Args:
68 request (Request): The incoming request.
69
70 Returns:
71 Response: HTML response with Swagger Editor interface.
72 """
73 func_title = "get_documentation"
74 self.disp.log_debug("Serving Swagger Editor documentation", func_title)
75
76 token = self.boilerplate_incoming_initialised.get_token_if_present(
77 request)
78 self.disp.log_debug(f"token = {token}", func_title)
79
80 html_content = f"""
81<!DOCTYPE html>
82<html lang="en">
83<head>
84 <meta charset="UTF-8">
85 <meta name="viewport" content="width=device-width, initial-scale=1.0">
86 <title>{self.api_title} - API Editor</title>
87 <link rel="stylesheet" href="{EDITOR_CONST.EDITOR_CDN_CSS}">
88 <style>
89 html, body {{
90 margin: 0;
91 padding: 0;
92 height: 100%;
93 width: 100%;
94 }}
95 #swagger-ui {{
96 height: 100%;
97 overflow-y: auto;
98 }}
99 </style>
100</head>
101<body>
102 <div id="swagger-ui"></div>
103 <script src="{EDITOR_CONST.EDITOR_CDN_JS}"></script>
104 <script src="{EDITOR_CONST.EDITOR_CDN_PRESET}"></script>
105 <script>
106 window.onload = function() {{
107 if (typeof SwaggerUIBundle === 'undefined') {{
108 document.getElementById('swagger-ui').innerHTML =
109 '<div style="padding: 20px; color: red;">Editor scripts failed to load. Please check your internet connection.</div>';
110 return;
111 }}
112
113 window.ui = SwaggerUIBundle({{
114 url: '{self.openapi_url}',
115 dom_id: '#swagger-ui',
116 presets: [
117 SwaggerUIBundle.presets.apis,
118 SwaggerUIStandalonePreset
119 ],
120 plugins: [
121 SwaggerUIBundle.plugins.DownloadUrl
122 ],
123 deepLinking: {str(EDITOR_CONST.EDITOR_OPTIONS.get('deepLinking', True)).lower()},
124 displayOperationId: {str(EDITOR_CONST.EDITOR_OPTIONS.get('displayOperationId', True)).lower()},
125 displayRequestDuration: {str(EDITOR_CONST.EDITOR_OPTIONS.get('displayRequestDuration', True)).lower()},
126 filter: {str(EDITOR_CONST.EDITOR_OPTIONS.get('filter', True)).lower()},
127 showExtensions: {str(EDITOR_CONST.EDITOR_OPTIONS.get('showExtensions', True)).lower()},
128 showCommonExtensions: {str(EDITOR_CONST.EDITOR_OPTIONS.get('showCommonExtensions', True)).lower()},
129 tryItOutEnabled: {str(EDITOR_CONST.EDITOR_OPTIONS.get('tryItOutEnabled', True)).lower()},
130 layout: "{EDITOR_CONST.EDITOR_OPTIONS.get('layout', 'BaseLayout')}"
131 }});
132 }};
133 </script>
134</body>
135</html>
136"""
137 return HCI.success(content=html_content, content_type=HttpDataTypes.HTML)
138
139 def get_url(self) -> str:
140 """Get the URL path for this documentation provider.
141
142 Returns:
143 str: The URL path.
144 """
145 return EDITOR_CONST.EDITOR_URL
None __init__(self, str openapi_url, str api_title, bool debug=False)