Cat Feeder  1.0.0
The Cat feeder project
Loading...
Searching...
No Matches
scalar_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: scalar_provider.py
14# CREATION DATE: 26-11-2025
15# LAST Modified: 14:45:50 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: Scalar documentation provider implementation.
21# // AR
22# +==== END CatFeeder =================+
23"""
24import json
25from display_tty import Disp, initialise_logger
26from fastapi import Request, Response
27from . import scalar_constants as SCALAR_CONST
28from ...http_codes import HCI, HttpDataTypes
29from ...core.runtime_manager import RuntimeManager, RI
30from ...server_header import ServerHeaders
31from ...boilerplates import BoilerplateResponses, BoilerplateIncoming
32
33
35 """Scalar documentation provider.
36
37 Provides modern Scalar API documentation interface with beautiful design.
38 """
39
40 disp: Disp = initialise_logger(__qualname__, False)
41
42 def __init__(self, openapi_url: str, api_title: str, debug: bool = False) -> None:
43 """Initialize Scalar provider.
44
45 Args:
46 openapi_url (str): URL to OpenAPI JSON schema.
47 api_title (str): Title of the API.
48 debug (bool, optional): Enable debug logging. Defaults to False.
49 """
50 self.disp.update_disp_debug(debug)
51 self.disp.log_debug("Initialising Scalar provider...")
52
53 self.openapi_url = openapi_url
54 self.api_title = api_title
55 self.runtime_manager: RuntimeManager = RI
56 self.server_headers_initialised: ServerHeaders = self.runtime_manager.get(
57 ServerHeaders)
58 self.boilerplate_responses_initialised: BoilerplateResponses = self.runtime_manager.get(
59 BoilerplateResponses)
60 self.boilerplate_incoming_initialised: BoilerplateIncoming = self.runtime_manager.get(
61 BoilerplateIncoming)
62
63 self.disp.log_debug("Scalar provider initialised")
64
65 async def get_documentation(self, request: Request) -> Response:
66 """Serve Scalar documentation page.
67
68 Args:
69 request (Request): The incoming request.
70
71 Returns:
72 Response: HTML response with Scalar interface.
73 """
74 func_title = "get_documentation"
75 self.disp.log_debug("Serving Scalar 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 config = {
82 "spec": {"url": self.openapi_url},
83 "theme": SCALAR_CONST.SCALAR_THEME,
84 "layout": SCALAR_CONST.SCALAR_LAYOUT,
85 **SCALAR_CONST.SCALAR_OPTIONS
86 }
87
88 html_content = f"""
89<!DOCTYPE html>
90<html>
91<head>
92 <meta charset="utf-8">
93 <meta name="viewport" content="width=device-width, initial-scale=1">
94 <title>{self.api_title} - Scalar</title>
95 <style>
96 body {{
97 margin: 0;
98 padding: 0;
99 }}
100 </style>
101</head>
102<body>
103 <script id="api-reference" data-url="{self.openapi_url}"></script>
104 <script>
105 var configuration = {json.dumps(config)};
106 document.getElementById('api-reference').dataset.configuration = JSON.stringify(configuration);
107 </script>
108 <script src="{SCALAR_CONST.SCALAR_CDN_URL}"></script>
109</body>
110</html>
111"""
112 return HCI.success(content=html_content, content_type=HttpDataTypes.HTML)
113
114 def get_url(self) -> str:
115 """Get the URL path for this documentation provider.
116
117 Returns:
118 str: The URL path.
119 """
120 return SCALAR_CONST.SCALAR_URL
None __init__(self, str openapi_url, str api_title, bool debug=False)