2# +==== BEGIN CatFeeder =================+
5# ...............)..(.')
7# ...............\(__)|
8# Inspired by Joan Stark
9# source https://www.asciiart.eu/
14# CREATION DATE: 11-10-2025
15# LAST Modified: 16:8:51 22-01-2026
17# This is the backend server in charge of making the actual website work.
19# COPYRIGHT: (c) Cat Feeder
20# PURPOSE: File containing boilerplate responses that could be used by the server in it's endpoints_initialised.
22# +==== END CatFeeder =================+
25from typing
import Union, Dict, Any, Optional
26from fastapi
import Response
27from display_tty
import Disp, initialise_logger
28from .non_web
import BoilerplateNonHTTP
29from ..core
import FinalSingleton
30from ..utils
import constants
as CONST
31from ..core.runtime_manager
import RuntimeManager, RI
32from ..server_header
import ServerHeaders
33from ..http_codes
import HCI, HTTP_DEFAULT_TYPE
40 disp: Disp = initialise_logger(__qualname__,
False)
42 def __init__(self, debug: bool =
False) ->
None:
46 debug (bool, optional): _description_. Defaults to False.
50 self.
disp.update_disp_debug(debug)
51 self.
disp.log_debug(
"Initialising...")
57 BoilerplateNonHTTP,
None)
61 self.
disp.log_debug(
"Initialised")
63 def build_response_body(self, title: str, message: str, resp: Any, token: Optional[str], error: bool =
False) -> Dict[str, Any]:
65 This is a function that will create a response body for the queries of the server.
67 title (str): _description_: The title of the message in the body
68 message (any): _description_: The actual message you wish to send (this is so that it is human friendly [i.e. "You have successfully logged in"])
69 resp (any): _description_: The section where you can put more coder side data.
70 token Union[str, None]: _description_: The user token or None if not present
71 error (bool, optional): _description_: If this is an error message or not. Defaults to False.
74 Dict[str, any]: _description_: the final version of the body message
76 func_title =
"build_response_body"
78 msg = f
"title={title}, message={message}, resp={resp},"
79 msg += f
"token={token}, error={error}"
80 self.
disp.log_debug(msg, func_title)
82 json_body[CONST.JSON_TITLE] = title
83 json_body[CONST.JSON_MESSAGE] = message
85 json_body[CONST.JSON_RESP] = resp
87 json_body[CONST.JSON_ERROR] = resp
88 self.
disp.log_debug(f
"token = {token}", func_title)
94 self.
disp.log_warning(
95 "Token validation service unavailable, unable to verify authentication status",
98 json_body[CONST.JSON_LOGGED_IN] =
None
101 "self.boilerplate_non_http_initialised is initialised"
104 self.
disp.log_debug(
"Token is present")
109 self.
disp.log_debug(
"Token is absent")
110 json_body[CONST.JSON_LOGGED_IN] =
False
112 f
"Valid token? {json_body[CONST.JSON_LOGGED_IN]}")
113 self.
disp.log_debug(f
"Final response: {json_body}")
118 This is a function that will return an invalid token response.
121 title (str): _description_: The title of the called endpoint
124 Response: _description_: The response ready to be sent back to the user
128 message=
"The token you entered is invalid.",
129 resp=
"Invalid token",
137 This is a function that will return a no access token response.
140 title (str): _description_: The name of the endpoint that is concerned
141 token (str): _description_: The token corresponding to the user being logged in
144 Response: _description_: A pre-made http response ready to go.
148 message=
"Access token not found.",
149 resp=
"No access token",
157 This is a function that will return a provider not found response.
160 title (str): _description_: The title of the called endpoint
161 token (Union[str, None], optional): _description_. Defaults to None.: The token provided by the user of the called endpoint
164 Response: _description_: The response ready to be sent back to the user
168 message=
"The provider you are looking for was not found.",
169 resp=
"Provider not found",
177 This is a function that will return a provider not found response.
180 title (str): _description_: The title of the called endpoint
181 token (Union[str, None], optional): _description_. Defaults to None.: The token provided by the user of the called endpoint
184 Response: _description_: The response ready to be sent back to the user
188 message=
"You have not given a provider.",
189 resp=
"Provider missing",
197 This is a function that will return a not logged in response.
200 title (str): _description_: The title of the called endpoint
203 Response: _description_: The response ready to be sent back to the user
207 message=
"You need to be logged in to be able to run this endpoint.",
208 resp=
"User not logged in",
216 This is a function that will return a failed login response.
219 title (str): _description_: The title of the called endpoint
222 Response: _description_: The response ready to be sent back to the user
226 message=
"Login failed, invalid credentials or username.",
227 resp=
"Invalid credentials or username.",
235 This is a function that will return an insuffisant rights response.
238 title (str): _description_: The title of the called endpoint
239 token (Union[str, None], optional): _description_. Defaults to None.: The token provided by the user of the called endpoint
242 Response: _description_: The response ready to be sent back to the user
246 message=
"You do not have enough permissions to execute this endpoint.",
247 resp=
"Insufficient rights for given account.",
253 def bad_request(self, title: str, token: Union[str,
None] =
None) -> Response:
255 This is a function that will return a bad request response.
258 title (str): _description_: The title of the called endpoint
259 token (Union[str, None], optional): _description_. Defaults to None.: The token provided by the user of the called endpoint
262 Response: _description_: The response ready to be sent back to the user
266 message=
"The request was not formatted correctly.",
275 This is a function that will return an internal server error response.
278 title (str): _description_: The title of the called endpoint
279 token (Union[str, None], optional): _description_. Defaults to None.: The token provided by the user of the called endpoint
282 Response: _description_: The response ready to be sent back to the user
286 message=
"The server has encountered an error.",
287 resp=
"Internal server error",
291 return HCI.internal_server_error(content=body, content_type=HTTP_DEFAULT_TYPE, headers=self.
server_headers_initialised.for_json())
293 def update_failed(self, title: str, token: Union[str,
None] =
None) -> Response:
294 """Return an HTTP 500 (Internal Server Error) response for update failures.
296 This function is intended as a catch-all for unexpected server-side
297 failures that occur during update operations (for example: database
298 errors, unhandled exceptions, or other transient errors). It returns an
299 HTTP 500 response via `HCI.internal_server_error(...)` with a short
300 `resp` token ("update_failed") and a user-friendly message.
302 When the failure reason is known and predictable, prefer a more
303 specific HTTP status code (use the corresponding helper on this class):
304 - Validation / malformed input: `bad_request` (HTTP 400) or an `unprocessable_entity`/validation helper (HTTP 422) if present.
305 - Conflict (concurrent edit): use a 409 Conflict response (implement a `update_conflict` helper if you need this commonly).
306 - Authentication/authorization: `unauthorized` (401) or `insuffisant_rights`/`forbidden` (403) as appropriate.
309 title (str): The title of the called endpoint (used in the response body).
310 token (Optional[str]): User token to allow `build_response_body` to include logged-in status information.
313 Response: The HTTP response produced by `HCI.internal_server_error(content=body, content_type=..., headers=...)`.
317 message=
"The server failed to update the requested content.",
318 resp=
"update_failed",
322 return HCI.internal_server_error(
324 content_type=HTTP_DEFAULT_TYPE,
328 def unauthorized(self, title: str, token: Union[str,
None] =
None) -> Response:
330 This is a function that will return an unauthorized response.
333 title (str): _description_: The title of the called endpoint
334 token (Union[str, None], optional): _description_. Defaults to None.: The token provided by the user of the called endpoint
337 Response: _description_: The response ready to be sent back to the user
341 message=
"You do not have permission to run this endpoint.",
342 resp=
"Access denied",
350 This is a function that will return an invalid verification code response.
353 title (str): _description_: The title of the called endpoint
354 token (Union[str, None], optional): _description_. Defaults to None.: The token provided by the user of the called endpoint
357 Response: _description_: The response ready to be sent back to the user
361 message=
"The verification code you have entered is incorrect.",
362 resp=
"Invalid verification code",
368 def user_not_found(self, title: str, token: Union[str,
None] =
None) -> Response:
370 Function that will return a user not found error.
373 title (str): _description_: The title of the endpoint
374 token (Union[str, None], optional): _description_. Defaults to None.: The token if present.
377 Response: _description_: The pre-compiled response (ready to go)
381 message=
"The current user was not found.",
390 Function that will return a message saying that there is a missing variable in the provided body.
393 title (str): _description_: The name of the endpoint
394 token (Union[str, None], optional): _description_. Defaults to None.: The token of the account.
397 Response: _description_: The pre-compiled response (ready to go)
401 message=f
"A variable ('{variable_name}') is missing in the body of the request.",
402 resp=
"Missing variable",
410 Function that will return a message saying that there is a missing variable in the provided query.
413 title (str): _description_: The name of the endpoint
414 token (Union[str, None], optional): _description_. Defaults to None.: The token of the account.
416 Response: _description_: The pre-compiled response (ready to go)
420 message=
"A variable is missing in the query of the request.",
421 resp=
"Missing variable",
429 Function that will return a message saying that a resource is missing.
432 title (str): _description_: The name of the endpoint
433 token (Union[str, None], optional): _description_. Defaults to None.: The token of the account.
435 Response: _description_: The pre-compiled response (ready to go)
439 message=
"The requested resource is missing.",
440 resp=
"Missing resource",
446 def service_unavailable(self, service: str =
"<not specified>", title: str =
"<not specified>", token: Union[str,
None] =
None) -> Response:
448 Function that will return a message saying that the service is unavailable.
451 title (str): _description_: The name of the endpoint
452 token (Union[str, None], optional): _description_. Defaults to None.: The token of the account.
454 Response: _description_: The pre-compiled response (ready to go)
458 message=f
"The service ({service}) is currently unavailable.",
459 resp=
"Service unavailable",
Response provider_not_found(self, str title, Union[str, None] token=None)
Response invalid_token(self, str title)
Response unauthorized(self, str title, Union[str, None] token=None)
Response no_access_token(self, str title, Union[str, None] token=None)
Response invalid_verification_code(self, str title, Union[str, None] token=None)
Response missing_resource(self, str title, Union[str, None] token=None)
Response bad_request(self, str title, Union[str, None] token=None)
Response provider_not_given(self, str title, Union[str, None] token=None)
Response missing_variable_in_body(self, str title, Union[str, None] token=None, str variable_name="")
Dict[str, Any] build_response_body(self, str title, str message, Any resp, Optional[str] token, bool error=False)
Response missing_variable_in_query(self, str title, Union[str, None] token=None)
Response user_not_found(self, str title, Union[str, None] token=None)
Response login_failed(self, str title)
RuntimeManager runtime_manager
Optional[BoilerplateNonHTTP] boilerplate_non_http_initialised
Response not_logged_in(self, str title)
Response internal_server_error(self, str title, Union[str, None] token=None)
Response update_failed(self, str title, Union[str, None] token=None)
None __init__(self, bool debug=False)
Response insuffisant_rights(self, str title, Union[str, None] token=None)
Response service_unavailable(self, str service="<not specified>", str title="<not specified>", Union[str, None] token=None)
ServerHeaders server_headers_initialised