1from typing
import Union, Mapping, Dict, Any
7 This is the class in charge of containing the boilerplate endpoint functions.
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:
18 def get_endpoint(self, path: str, content: Union[Dict[str, Any],
None] =
None, header: Union[Mapping[str, str],
None] =
None) -> requests.Response:
20 This function is in charge of sending a GET request to the server.
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.
26 requests.Response: _description_: The response from the server.
28 if isinstance(path, str)
is False:
30 f
"Expected an input of type string but got {type(path)}"
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)
42 def post_endpoint(self, path: str, content: Union[Dict[str, Any],
None] =
None, header: Union[Mapping[str, str],
None] =
None) -> requests.Response:
44 This function is in charge of sending a POST request to the server.
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.
50 requests.Response: _description_: The response from the server.
52 if isinstance(path, str)
is False:
54 f
"Expected an input of type string but got {type(path)}"
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)
66 def put_endpoint(self, path: str, content: Union[Dict[str, Any],
None] =
None, header: Union[Mapping[str, str],
None] =
None) -> requests.Response:
68 This function is in charge of sending a PUT request to the server.
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.
74 requests.Response: _description_: The response from the server.
76 if isinstance(path, str)
is False:
78 f
"Expected an input of type string but got {type(path)}"
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)
90 def patch_endpoint(self, path: str, content: Union[Dict[str, Any],
None] =
None, header: Union[Mapping[str, str],
None] =
None) -> requests.Response:
92 This function is in charge of sending a PATCH request to the server.
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.
98 requests.Response: _description_: The response from the server.
100 if isinstance(path, str)
is False:
102 f
"Expected an input of type string but got {type(path)}"
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)
114 def delete_endpoint(self, path: str, content: Union[Dict[str, Any],
None] =
None, header: Union[Mapping[str, str],
None] =
None) -> requests.Response:
116 This function is in charge of sending a DELETE request to the server.
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.
122 requests.Response: _description_: The response from the server.
124 if isinstance(path, str)
is False:
126 f
"Expected an input of type string but got {type(path)}"
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)
140 This function is in charge of getting the status code from the response.
142 response (requests.Response): _description_: The response from the server.
144 int: _description_: The status code from the response.
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)