Terarea  2
The automation project
Loading...
Searching...
No Matches
responses.py
Go to the documentation of this file.
1"""_summary_
2 File containing boilerplate responses that could be used by the server in it's endpoints_initialised.
3"""
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
8
9
11 """_summary_
12 """
13
14 def __init__(self, runtime_data: RuntimeData, debug: bool = False) -> None:
15 """_summary_
16
17 Args:
18 debug (bool, optional): _description_. Defaults to False.
19 """
20 self.debug: bool = debug
21 self.runtime_data_initialised: RuntimeData = runtime_data
22 self.disp: Disp = Disp(
23 TOML_CONF,
24 SAVE_TO_FILE,
25 FILE_NAME,
26 FILE_DESCRIPTOR,
27 debug=self.debug,
28 logger=self.__class__.__name__
29 )
30
31 def build_response_body(self, title: str, message: str, resp: Any, token: Union[str, None], error: bool = False) -> Dict[str, Any]:
32 """_summary_
33 This is a function that will create a response body for the queries of the server.
34 Args:
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.
40
41 Returns:
42 Dict[str, any]: _description_: the final version of the body message
43 """
44 func_title = "build_response_body"
45 json_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)
49
50 json_body[CONST.JSON_TITLE] = title
51 json_body[CONST.JSON_MESSAGE] = message
52 if error is False:
53 json_body[CONST.JSON_RESP] = resp
54 else:
55 json_body[CONST.JSON_ERROR] = resp
56 self.disp.log_debug(f"token = {token}", func_title)
57 json_body[CONST.JSON_LOGGED_IN] = self.runtime_data_initialised.boilerplate_non_http_initialised.is_token_correct(
58 token
59 )
60 return json_body
61
62 def invalid_token(self, title: str) -> Response:
63 """_summary_
64 This is a function that will return an invalid token response.
65
66 Args:
67 title (str): _description_: The title of the called endpoint
68
69 Returns:
70 Response: _description_: The response ready to be sent back to the user
71 """
72 body = self.build_response_body(
73 title=title,
74 message="The token you entered is invalid.",
75 resp="Invalid token",
76 token="",
77 error=True
78 )
79 return HCI.invalid_token(content=body, content_type=CONST.CONTENT_TYPE, headers=self.runtime_data_initialised.json_header)
80
81 def no_access_token(self, title: str, token: Union[str, None] = None) -> Response:
82 """_summary_
83 This is a function that will return a no access token response.
84
85 Args:
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
88
89 Returns:
90 Response: _description_: A pre-made http response ready to go.
91 """
92 body = self.build_response_body(
93 title=title,
94 message="Access token not found.",
95 resp="No access token",
96 token=token,
97 error=True
98 )
99 return HCI.bad_request(content=body, content_type=CONST.CONTENT_TYPE, headers=self.runtime_data_initialised.json_header)
100
101 def provider_not_found(self, title: str, token: Union[str, None] = None) -> Response:
102 """_summary_
103 This is a function that will return a provider not found response.
104
105 Args:
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
108
109 Returns:
110 Response: _description_: The response ready to be sent back to the user
111 """
112 body = self.build_response_body(
113 title=title,
114 message="The provider you are looking for was not found.",
115 resp="Provider not found",
116 token=token,
117 error=True
118 )
119 return HCI.not_found(content=body, content_type=CONST.CONTENT_TYPE, headers=self.runtime_data_initialised.json_header)
120
121 def provider_not_given(self, title: str, token: Union[str, None] = None) -> Response:
122 """_summary_
123 This is a function that will return a provider not found response.
124
125 Args:
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
128
129 Returns:
130 Response: _description_: The response ready to be sent back to the user
131 """
132 body = self.build_response_body(
133 title=title,
134 message="You have not given a provider.",
135 resp="Provider missing",
136 token=token,
137 error=True
138 )
139 return HCI.bad_request(content=body, content_type=CONST.CONTENT_TYPE, headers=self.runtime_data_initialised.json_header)
140
141 def not_logged_in(self, title: str) -> Response:
142 """_summary_
143 This is a function that will return a not logged in response.
144
145 Args:
146 title (str): _description_: The title of the called endpoint
147
148 Returns:
149 Response: _description_: The response ready to be sent back to the user
150 """
151 body = self.build_response_body(
152 title=title,
153 message="You need to be logged in to be able to run this endpoint.",
154 resp="User not logged in",
155 token="",
156 error=True
157 )
158 return HCI.unauthorized(content=body, content_type=CONST.CONTENT_TYPE, headers=self.runtime_data_initialised.json_header)
159
160 def login_failed(self, title: str) -> Response:
161 """_summary_
162 This is a function that will return a failed login response.
163
164 Args:
165 title (str): _description_: The title of the called endpoint
166
167 Returns:
168 Response: _description_: The response ready to be sent back to the user
169 """
170 body = self.build_response_body(
171 title=title,
172 message="Login failed, invalid credentials or username.",
173 resp="Invalid credentials or username.",
174 token="",
175 error=True
176 )
177 return HCI.unauthorized(content=body, content_type=CONST.CONTENT_TYPE, headers=self.runtime_data_initialised.json_header)
178
179 def insuffisant_rights(self, title: str, token: Union[str, None] = None) -> Response:
180 """_summary_
181 This is a function that will return an insuffisant rights response.
182
183 Args:
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
186
187 Returns:
188 Response: _description_: The response ready to be sent back to the user
189 """
190 body = self.build_response_body(
191 title=title,
192 message="You do not have enough permissions to execute this endpoint.",
193 resp="Insufficient rights for given account.",
194 token=token,
195 error=True
196 )
197 return HCI.forbidden(content=body, content_type=CONST.CONTENT_TYPE, headers=self.runtime_data_initialised.json_header)
198
199 def bad_request(self, title: str, token: Union[str, None] = None) -> Response:
200 """_summary_
201 This is a function that will return a bad request response.
202
203 Args:
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
206
207 Returns:
208 Response: _description_: The response ready to be sent back to the user
209 """
210 body = self.build_response_body(
211 title=title,
212 message="The request was not formatted correctly.",
213 resp="Bad request",
214 token=token,
215 error=True
216 )
217 return HCI.bad_request(content=body, content_type=CONST.CONTENT_TYPE, headers=self.runtime_data_initialised.json_header)
218
219 def internal_server_error(self, title: str, token: Union[str, None] = None) -> Response:
220 """_summary_
221 This is a function that will return an internal server error response.
222
223 Args:
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
226
227 Returns:
228 Response: _description_: The response ready to be sent back to the user
229 """
230 body = self.build_response_body(
231 title=title,
232 message="The server has encountered an error.",
233 resp="Internal server error",
234 token=token,
235 error=True
236 )
237 return HCI.internal_server_error(content=body, content_type=CONST.CONTENT_TYPE, headers=self.runtime_data_initialised.json_header)
238
239 def unauthorized(self, title: str, token: Union[str, None] = None) -> Response:
240 """_summary_
241 This is a function that will return an unauthorized response.
242
243 Args:
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
246
247 Returns:
248 Response: _description_: The response ready to be sent back to the user
249 """
250 body = self.build_response_body(
251 title=title,
252 message="You do not have permission to run this endpoint.",
253 resp="Access denied",
254 token=token,
255 error=True
256 )
257 return HCI.unauthorized(content=body, content_type=CONST.CONTENT_TYPE, headers=self.runtime_data_initialised.json_header)
258
259 def invalid_verification_code(self, title: str, token: Union[str, None] = None) -> Response:
260 """_summary_
261 This is a function that will return an invalid verification code response.
262
263 Args:
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
266
267 Returns:
268 Response: _description_: The response ready to be sent back to the user
269 """
270 body = self.build_response_body(
271 title=title,
272 message="The verification code you have entered is incorrect.",
273 resp="Invalid verification code",
274 token=token,
275 error=True
276 )
277 return HCI.bad_request(content=body, content_type=CONST.CONTENT_TYPE, headers=self.runtime_data_initialised.json_header)
278
279 def user_not_found(self, title: str, token: Union[str, None] = None) -> Response:
280 """_summary_
281 Function that will return a user not found error.
282
283 Args:
284 title (str): _description_: The title of the endpoint
285 token (Union[str, None], optional): _description_. Defaults to None.: The token if present.
286
287 Returns:
288 Response: _description_: The pre-compiled response (ready to go)
289 """
290 body = self.build_response_body(
291 title=title,
292 message="The current user was not found.",
293 resp="Not found",
294 token=token,
295 error=True
296 )
297 return HCI.not_found(content=body, content_type=CONST.CONTENT_TYPE, headers=self.runtime_data_initialised.json_header)
298
299 def missing_variable_in_body(self, title: str, token: Union[str, None] = None) -> Response:
300 """_summary_
301 Function that will return a message saying that there is a missing variable in the provided body.
302
303 Args:
304 title (str): _description_: The name of the endpoint
305 token (Union[str, None], optional): _description_. Defaults to None.: The token of the account.
306
307 Returns:
308 Response: _description_: The pre-compiled response (ready to go)
309 """
310 body = self.build_response_body(
311 title=title,
312 message="A variable is missing in the body of the request.",
313 resp="Missing variable",
314 token=token,
315 error=True
316 )
317 return HCI.bad_request(body, content_type=CONST.CONTENT_TYPE, headers=self.runtime_data_initialised.json_header)
Response missing_variable_in_body(self, str title, Union[str, None] token=None)
Definition responses.py:299
Dict[str, Any] build_response_body(self, str title, str message, Any resp, Union[str, None] token, bool error=False)
Definition responses.py:31
Response provider_not_found(self, str title, Union[str, None] token=None)
Definition responses.py:101
Response insuffisant_rights(self, str title, Union[str, None] token=None)
Definition responses.py:179
Response no_access_token(self, str title, Union[str, None] token=None)
Definition responses.py:81
Response invalid_verification_code(self, str title, Union[str, None] token=None)
Definition responses.py:259
Response bad_request(self, str title, Union[str, None] token=None)
Definition responses.py:199
None __init__(self, RuntimeData runtime_data, bool debug=False)
Definition responses.py:14
Response provider_not_given(self, str title, Union[str, None] token=None)
Definition responses.py:121
Response internal_server_error(self, str title, Union[str, None] token=None)
Definition responses.py:219
Response unauthorized(self, str title, Union[str, None] token=None)
Definition responses.py:239
Response user_not_found(self, str title, Union[str, None] token=None)
Definition responses.py:279