Cat Feeder  1.0.0
The Cat feeder project
Loading...
Searching...
No Matches
explorer_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: explorer_provider.py
14# CREATION DATE: 26-11-2025
15# LAST Modified: 14:44:44 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: OpenAPI Explorer documentation provider implementation.
21# // AR
22# +==== END CatFeeder =================+
23"""
24from fastapi import Request, Response
25from display_tty import Disp, initialise_logger
26from . import explorer_constants as EXPLORER_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 """OpenAPI Explorer documentation provider.
35
36 Provides interactive API explorer with request/response console.
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 OpenAPI Explorer 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 OpenAPI Explorer 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("OpenAPI Explorer provider initialised")
63
64 async def get_documentation(self, request: Request) -> Response:
65 """Serve OpenAPI Explorer documentation page.
66
67 Args:
68 request (Request): The incoming request.
69
70 Returns:
71 Response: HTML response with OpenAPI Explorer interface.
72 """
73 func_title = "get_documentation"
74 self.disp.log_debug(
75 "Serving OpenAPI Explorer 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 options_js = ""
82 for key, value in EXPLORER_CONST.EXPLORER_OPTIONS.items():
83 if isinstance(value, bool):
84 if value:
85 js_value = "true"
86 else:
87 js_value = "false"
88 elif isinstance(value, str):
89 js_value = f'"{value}"'
90 else:
91 js_value = str(value)
92 options_js = options_js + f" {key}: {js_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.0">
100 <title>{self.api_title} - OpenAPI Explorer</title>
101 <link rel="stylesheet" href="{EXPLORER_CONST.EXPLORER_CDN_CSS}">
102 <style>
103 body {{
104 margin: 0;
105 padding: 0;
106 }}
107 #explorer {{
108 width: 100%;
109 height: 100vh;
110 }}
111 </style>
112</head>
113<body>
114 <div id="explorer"></div>
115 <script src="{EXPLORER_CONST.EXPLORER_CDN_JS}"></script>
116 <script src="{EXPLORER_CONST.EXPLORER_CDN_PRESET}"></script>
117 <script>
118 window.onload = function() {{
119 window.ui = SwaggerUIBundle({{
120 url: '{self.openapi_url}',
121 dom_id: '#explorer',
122{options_js} presets: [
123 SwaggerUIBundle.presets.apis,
124 SwaggerUIStandalonePreset
125 ],
126 plugins: [
127 SwaggerUIBundle.plugins.DownloadUrl
128 ]
129 }});
130 }};
131 </script>
132</body>
133</html>
134"""
135 return HCI.success(content=html_content, content_type=HttpDataTypes.HTML)
136
137 def get_url(self) -> str:
138 """Get the URL path for this documentation provider.
139
140 Returns:
141 str: The URL path.
142 """
143 return EXPLORER_CONST.EXPLORER_URL
None __init__(self, str openapi_url, str api_title, bool debug=False)