Terarea  2
The automation project
Loading...
Searching...
No Matches
paths.py
Go to the documentation of this file.
1"""_summary_
2 File in charge of referencing all the paths_initialised supported by the server.
3"""
4from typing import Union, List, Dict, Any
5from display_tty import Disp, TOML_CONF, FILE_DESCRIPTOR, SAVE_TO_FILE, FILE_NAME
6from .runtime_data import RuntimeData
7from .constants import PATH_KEY, ENDPOINT_KEY, METHOD_KEY, ALLOWED_METHODS
8
9
11 """_summary_
12 """
13
14 def __init__(self, runtime_data: RuntimeData, success: int = 0, error: int = 84, debug: bool = False) -> None:
15 """_summary_
16
17 Args:
18 success (int, optional): _description_. Defaults to 0.
19 error (int, optional): _description_. Defaults to 84.
20 """
21 self.runtime_data_initialised: RuntimeData = runtime_data
22 self.success = success
23 self.error = error
24 self.routes: List[Dict[str, Any]] = []
25 self.debug: bool = debug
26 # ------------------------ The logging function ------------------------
27 self.disp: Disp = Disp(
28 TOML_CONF,
29 FILE_DESCRIPTOR,
30 SAVE_TO_FILE,
31 FILE_NAME,
32 debug=self.debug,
33 logger=self.__class__.__name__
34 )
35
36 def add_path(self, path: str, endpoint: object, method: Union[str, List[str]]) -> int:
37 """_summary_
38 This function is in charge of adding a path to the list of paths_initialised.
39
40 Args:
41 path (str): _description_: The path to call for the endpoint to be triggered
42 endpoint (str): _description_: The function that represents the endpoint
43 method (str, list): _description_: The method used for the provided path (GET, PUT, POST, etc)
44
45 Returns:
46 int: _description_: success if it succeeded, error if there was an error in the data.
47 """
48 self.disp.log_debug(f"Adding path <{path}>", "add_path")
49
50 if isinstance(path, (str)) is False or isinstance(method, (str, list)) is False or callable(endpoint) is False:
51 self.disp.log_error(
52 f"Failed to insert {path} with method {method}", "add_path"
53 )
54 return self.error
55 if isinstance(method, str) is True and method.upper() not in ALLOWED_METHODS:
56 msg = f"Failed to insert {path}, method {method} not allowed"
57 self.disp.log_error(msg, "add_path")
58 return self.error
59 if isinstance(method, list) is True:
60 for i in method:
61 if isinstance(i, str) is False or i.upper() not in ALLOWED_METHODS:
62 msg = f"Failed to insert {path}, method {i} not allowed"
63 self.disp.log_error(msg, "add_path")
64 return self.error
65 if isinstance(method, str) is True:
66 method = [method]
67 self.routes.append(
68 {PATH_KEY: path, ENDPOINT_KEY: endpoint, METHOD_KEY: method}
69 )
70 return self.success
71
73 """_summary_
74 This function is in charge of adding the default paths_initialised to the list of paths_initialised.
75 """
76 self.disp.log_debug(
77 "Loading default paths_initialised",
78 "load_default_paths_initialised"
79 )
80 self.runtime_data_initialised.endpoints_initialised.inject_routes()
81
82 def inject_routes(self) -> None:
83 """_summary_
84 Function in charge of loading the routes into the api.
85 Args:
86 app (FastAPI): _description_
87 """
88 self.disp.log_info("injecting routes", "inject_routes")
89 for route in self.routes:
90 self.disp.log_debug(f"route = {route}", "inject_routes")
91 self.runtime_data_initialised.app.add_api_route(
92 route[PATH_KEY],
93 route[ENDPOINT_KEY],
94 methods=route[METHOD_KEY]
95 )
None __init__(self, RuntimeData runtime_data, int success=0, int error=84, bool debug=False)
Definition paths.py:14
int add_path(self, str path, object endpoint, Union[str, List[str]] method)
Definition paths.py:36
None load_default_paths_initialised(self)
Definition paths.py:72