2 File containing boilerplate responses that could be used by the server in it's endpoints_initialised.
4from typing
import Union, Dict, Any
5from fastapi
import Response
6from display_tty
import Disp, TOML_CONF, FILE_DESCRIPTOR, SAVE_TO_FILE, FILE_NAME
7from ..components
import HCI, RuntimeData, CONST
14 def __init__(self, runtime_data: RuntimeData, debug: bool =
False) ->
None:
18 debug (bool, optional): _description_. Defaults to False.
28 logger=self.__class__.__name__
31 def build_response_body(self, title: str, message: str, resp: Any, token: Union[str,
None], error: bool =
False) -> Dict[str, Any]:
33 This is a function that will create a response body for the queries of the server.
35 title (str): _description_: The title of the message in the body
36 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"])
37 resp (any): _description_: The section where you can put more coder side data.
38 token Union[str, None]: _description_: The user token or None if not present
39 error (bool, optional): _description_: If this is an error message or not. Defaults to False.
42 Dict[str, any]: _description_: the final version of the body message
44 func_title =
"build_response_body"
46 msg = f
"title={title}, message={message}, resp={resp},"
47 msg += f
"token={token}, error={error}"
48 self.
disp.log_debug(msg, func_title)
50 json_body[CONST.JSON_TITLE] = title
51 json_body[CONST.JSON_MESSAGE] = message
53 json_body[CONST.JSON_RESP] = resp
55 json_body[CONST.JSON_ERROR] = resp
56 self.
disp.log_debug(f
"token = {token}", func_title)
64 This is a function that will return an invalid token response.
67 title (str): _description_: The title of the called endpoint
70 Response: _description_: The response ready to be sent back to the user
74 message=
"The token you entered is invalid.",
79 return HCI.invalid_token(content=body, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
81 def no_access_token(self, title: str, token: Union[str,
None] =
None) -> Response:
83 This is a function that will return a no access token response.
86 title (str): _description_: The name of the endpoint that is concerned
87 token (str): _description_: The token corresponding to the user being logged in
90 Response: _description_: A pre-made http response ready to go.
94 message=
"Access token not found.",
95 resp=
"No access token",
99 return HCI.bad_request(content=body, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
103 This is a function that will return a provider not found response.
106 title (str): _description_: The title of the called endpoint
107 token (Union[str, None], optional): _description_. Defaults to None.: The token provided by the user of the called endpoint
110 Response: _description_: The response ready to be sent back to the user
114 message=
"The provider you are looking for was not found.",
115 resp=
"Provider not found",
119 return HCI.not_found(content=body, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
123 This is a function that will return a provider not found response.
126 title (str): _description_: The title of the called endpoint
127 token (Union[str, None], optional): _description_. Defaults to None.: The token provided by the user of the called endpoint
130 Response: _description_: The response ready to be sent back to the user
134 message=
"You have not given a provider.",
135 resp=
"Provider missing",
139 return HCI.bad_request(content=body, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
143 This is a function that will return a not logged in response.
146 title (str): _description_: The title of the called endpoint
149 Response: _description_: The response ready to be sent back to the user
153 message=
"You need to be logged in to be able to run this endpoint.",
154 resp=
"User not logged in",
158 return HCI.unauthorized(content=body, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
162 This is a function that will return a failed login response.
165 title (str): _description_: The title of the called endpoint
168 Response: _description_: The response ready to be sent back to the user
172 message=
"Login failed, invalid credentials or username.",
173 resp=
"Invalid credentials or username.",
177 return HCI.unauthorized(content=body, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
181 This is a function that will return an insuffisant rights response.
184 title (str): _description_: The title of the called endpoint
185 token (Union[str, None], optional): _description_. Defaults to None.: The token provided by the user of the called endpoint
188 Response: _description_: The response ready to be sent back to the user
192 message=
"You do not have enough permissions to execute this endpoint.",
193 resp=
"Insufficient rights for given account.",
197 return HCI.forbidden(content=body, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
199 def bad_request(self, title: str, token: Union[str,
None] =
None) -> Response:
201 This is a function that will return a bad request response.
204 title (str): _description_: The title of the called endpoint
205 token (Union[str, None], optional): _description_. Defaults to None.: The token provided by the user of the called endpoint
208 Response: _description_: The response ready to be sent back to the user
212 message=
"The request was not formatted correctly.",
217 return HCI.bad_request(content=body, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
221 This is a function that will return an internal server error response.
224 title (str): _description_: The title of the called endpoint
225 token (Union[str, None], optional): _description_. Defaults to None.: The token provided by the user of the called endpoint
228 Response: _description_: The response ready to be sent back to the user
232 message=
"The server has encountered an error.",
233 resp=
"Internal server error",
237 return HCI.internal_server_error(content=body, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
239 def unauthorized(self, title: str, token: Union[str,
None] =
None) -> Response:
241 This is a function that will return an unauthorized response.
244 title (str): _description_: The title of the called endpoint
245 token (Union[str, None], optional): _description_. Defaults to None.: The token provided by the user of the called endpoint
248 Response: _description_: The response ready to be sent back to the user
252 message=
"You do not have permission to run this endpoint.",
253 resp=
"Access denied",
257 return HCI.unauthorized(content=body, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
261 This is a function that will return an invalid verification code response.
264 title (str): _description_: The title of the called endpoint
265 token (Union[str, None], optional): _description_. Defaults to None.: The token provided by the user of the called endpoint
268 Response: _description_: The response ready to be sent back to the user
272 message=
"The verification code you have entered is incorrect.",
273 resp=
"Invalid verification code",
277 return HCI.bad_request(content=body, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
279 def user_not_found(self, title: str, token: Union[str,
None] =
None) -> Response:
281 Function that will return a user not found error.
284 title (str): _description_: The title of the endpoint
285 token (Union[str, None], optional): _description_. Defaults to None.: The token if present.
288 Response: _description_: The pre-compiled response (ready to go)
292 message=
"The current user was not found.",
297 return HCI.not_found(content=body, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
301 Function that will return a message saying that there is a missing variable in the provided body.
304 title (str): _description_: The name of the endpoint
305 token (Union[str, None], optional): _description_. Defaults to None.: The token of the account.
308 Response: _description_: The pre-compiled response (ready to go)
312 message=
"A variable is missing in the body of the request.",
313 resp=
"Missing variable",
Response missing_variable_in_body(self, str title, Union[str, None] token=None)
Dict[str, Any] build_response_body(self, str title, str message, Any resp, Union[str, None] token, bool error=False)
Response provider_not_found(self, str title, Union[str, None] token=None)
Response insuffisant_rights(self, str title, Union[str, None] token=None)
Response no_access_token(self, str title, Union[str, None] token=None)
Response login_failed(self, str title)
Response invalid_verification_code(self, str title, Union[str, None] token=None)
Response bad_request(self, str title, Union[str, None] token=None)
Response not_logged_in(self, str title)
None __init__(self, RuntimeData runtime_data, bool debug=False)
Response provider_not_given(self, str title, Union[str, None] token=None)
RuntimeData runtime_data_initialised
Response internal_server_error(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 user_not_found(self, str title, Union[str, None] token=None)