Terarea  2
The automation project
Loading...
Searching...
No Matches
query_boilerplate.py
Go to the documentation of this file.
1from typing import Union, Mapping, Dict, Any
2import requests
3
4
6 """_summary_
7 This is the class in charge of containing the boilerplate endpoint functions.
8 """
9
10 def __init__(self, host: str = "http://127.0.0.1", port: int = 6000, delay: int = 2) -> None:
11 if host.startswith("http") is False:
12 self._host = f"http://{host}"
13 else:
14 self._host = host
15 self._port = port
16 self._delay = delay
17
18 def get_endpoint(self, path: str, content: Union[Dict[str, Any], None] = None, header: Union[Mapping[str, str], None] = None) -> requests.Response:
19 """_summary_
20 This function is in charge of sending a GET request to the server.
21 Args:
22 path (str): _description_: The path of the endpoint.
23 content (Union[Dict[str, Any], None], optional): _description_: The content to be sent to the server.
24 header (Union[Mapping[str, str], None], optional): _description_: The header to be sent to the server. Defaults to None.
25 Returns:
26 requests.Response: _description_: The response from the server.
27 """
28 if isinstance(path, str) is False:
29 raise ValueError(
30 f"Expected an input of type string but got {type(path)}"
31 )
32 if path[0] == "/":
33 path = path[1:]
34 if content is not None and header is None:
35 return requests.get(f"{self._host}:{self._port}/{path}", json=content, timeout=self._delay)
36 if content is None and header is not None:
37 return requests.get(f"{self._host}:{self._port}/{path}", headers=header, timeout=self._delay)
38 if content is not None and header is not None:
39 return requests.get(f"{self._host}:{self._port}/{path}", json=content, headers=header, timeout=self._delay)
40 return requests.get(f"{self._host}:{self._port}/{path}", timeout=self._delay)
41
42 def post_endpoint(self, path: str, content: Union[Dict[str, Any], None] = None, header: Union[Mapping[str, str], None] = None) -> requests.Response:
43 """_summary_
44 This function is in charge of sending a POST request to the server.
45 Args:
46 path (str): _description_: The path of the endpoint.
47 content (Union[Dict[str, Any], None], optional): _description_: The content to be sent to the server.
48 header (Union[Mapping[str, str], None], optional): _description_: The header to be sent to the server. Defaults to None.
49 Returns:
50 requests.Response: _description_: The response from the server.
51 """
52 if isinstance(path, str) is False:
53 raise ValueError(
54 f"Expected an input of type string but got {type(path)}"
55 )
56 if path[0] == "/":
57 path = path[1:]
58 if content is not None and header is None:
59 return requests.post(f"{self._host}:{self._port}/{path}", json=content, timeout=self._delay)
60 if content is None and header is not None:
61 return requests.post(f"{self._host}:{self._port}/{path}", headers=header, timeout=self._delay)
62 if content is not None and header is not None:
63 return requests.post(f"{self._host}:{self._port}/{path}", json=content, headers=header, timeout=self._delay)
64 return requests.post(f"{self._host}:{self._port}/{path}", timeout=self._delay)
65
66 def put_endpoint(self, path: str, content: Union[Dict[str, Any], None] = None, header: Union[Mapping[str, str], None] = None) -> requests.Response:
67 """_summary_
68 This function is in charge of sending a PUT request to the server.
69 Args:
70 path (str): _description_: The path of the endpoint.
71 content (Union[Dict[str, Any], None], optional): _description_: The content to be sent to the server.
72 header (Union[Mapping[str, str], None], optional): _description_: The header to be sent to the server. Defaults to None.
73 Returns:
74 requests.Response: _description_: The response from the server.
75 """
76 if isinstance(path, str) is False:
77 raise ValueError(
78 f"Expected an input of type string but got {type(path)}"
79 )
80 if path[0] == "/":
81 path = path[1:]
82 if content is not None and header is None:
83 return requests.put(f"{self._host}:{self._port}/{path}", json=content, timeout=self._delay)
84 if content is None and header is not None:
85 return requests.put(f"{self._host}:{self._port}/{path}", headers=header, timeout=self._delay)
86 if content is not None and header is not None:
87 return requests.put(f"{self._host}:{self._port}/{path}", json=content, headers=header, timeout=self._delay)
88 return requests.put(f"{self._host}:{self._port}/{path}", timeout=self._delay)
89
90 def patch_endpoint(self, path: str, content: Union[Dict[str, Any], None] = None, header: Union[Mapping[str, str], None] = None) -> requests.Response:
91 """_summary_
92 This function is in charge of sending a PATCH request to the server.
93 Args:
94 path (str): _description_: The path of the endpoint.
95 content (Union[Dict[str, Any], None], optional): _description_: The content to be sent to the server.
96 header (Union[Mapping[str, str], None], optional): _description_: The header to be sent to the server. Defaults to None.
97 Returns:
98 requests.Response: _description_: The response from the server.
99 """
100 if isinstance(path, str) is False:
101 raise ValueError(
102 f"Expected an input of type string but got {type(path)}"
103 )
104 if path[0] == "/":
105 path = path[1:]
106 if content is not None and header is None:
107 return requests.patch(f"{self._host}:{self._port}/{path}", json=content, timeout=self._delay)
108 if content is None and header is not None:
109 return requests.patch(f"{self._host}:{self._port}/{path}", headers=header, timeout=self._delay)
110 if content is not None and header is not None:
111 return requests.patch(f"{self._host}:{self._port}/{path}", json=content, headers=header, timeout=self._delay)
112 return requests.patch(f"{self._host}:{self._port}/{path}", timeout=self._delay)
113
114 def delete_endpoint(self, path: str, content: Union[Dict[str, Any], None] = None, header: Union[Mapping[str, str], None] = None) -> requests.Response:
115 """_summary_
116 This function is in charge of sending a DELETE request to the server.
117 Args:
118 path (str): _description_: The path of the endpoint.
119 content (Union[Dict[str, Any], None], optional): _description_: The content to be sent to the server.
120 header (Union[Mapping[str, str], None], optional): _description_: The header to be sent to the server. Defaults to None.
121 Returns:
122 requests.Response: _description_: The response from the server.
123 """
124 if isinstance(path, str) is False:
125 raise ValueError(
126 f"Expected an input of type string but got {type(path)}"
127 )
128 if path[0] == "/":
129 path = path[1:]
130 if content is not None and header is None:
131 return requests.delete(f"{self._host}:{self._port}/{path}", json=content, timeout=self._delay)
132 if content is None and header is not None:
133 return requests.delete(f"{self._host}:{self._port}/{path}", headers=header, timeout=self._delay)
134 if content is not None and header is not None:
135 return requests.delete(f"{self._host}:{self._port}/{path}", json=content, headers=header, timeout=self._delay)
136 return requests.delete(f"{self._host}:{self._port}/{path}", timeout=self._delay)
137
138 def get_status(self, response: requests.Response) -> int:
139 """_summary_
140 This function is in charge of getting the status code from the response.
141 Args:
142 response (requests.Response): _description_: The response from the server.
143 Returns:
144 int: _description_: The status code from the response.
145 """
146 return response.status_code
None __init__(self, str host="http://127.0.0.1", int port=6000, int delay=2)
requests.Response post_endpoint(self, str path, Union[Dict[str, Any], None] content=None, Union[Mapping[str, str], None] header=None)
requests.Response delete_endpoint(self, str path, Union[Dict[str, Any], None] content=None, Union[Mapping[str, str], None] header=None)
requests.Response patch_endpoint(self, str path, Union[Dict[str, Any], None] content=None, Union[Mapping[str, str], None] header=None)
requests.Response get_endpoint(self, str path, Union[Dict[str, Any], None] content=None, Union[Mapping[str, str], None] header=None)
requests.Response put_endpoint(self, str path, Union[Dict[str, Any], None] content=None, Union[Mapping[str, str], None] header=None)
int get_status(self, requests.Response response)