-
Notifications
You must be signed in to change notification settings - Fork 7
/
other_http.py
193 lines (146 loc) · 6.35 KB
/
other_http.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import logging
from typing import Union
import asyncio
from httpx import AsyncClient, Client, BasicAuth, Response
import json
from .model import APIModel, APIEndpoints
from .api import Api
class OtherHTTP:
"""The class includes all necessary methods to access other Grafana API endpoints
Args:
grafana_api_model (APIModel): Inject a Grafana API model object that includes all necessary values and information
Attributes:
grafana_api_model (APIModel): This is where we store the grafana_api_model
"""
def __init__(self, grafana_api_model: APIModel):
self.grafana_api_model = grafana_api_model
def get_frontend_settings(self) -> dict:
"""The method includes a functionality to get the frontend settings
Raises:
Exception: Unspecified error by executing the API call
Returns:
api_call (dict): Returns the corresponding frontend settings
"""
api_call: dict = Api(self.grafana_api_model).call_the_api(
f"{APIEndpoints.FRONTEND.value}/settings"
)
if api_call == dict():
logging.error(f"Check the error: {api_call}.")
raise Exception
else:
return api_call
def renew_login_session_based_on_remember_cookie(self):
"""The method includes a functionality to renew the login session based on the remember cookie
Raises:
Exception: Unspecified error by executing the API call
Returns:
None
"""
api_call: dict = Api(self.grafana_api_model).call_the_api(
f"{APIEndpoints.LOGIN.value}/ping"
)
if api_call.get("message") != "Logged in":
logging.error(f"Check the error: {api_call}.")
raise Exception
else:
logging.info(
"You successfully renewed the login session based on the cookie."
)
def get_health_status(self) -> dict:
"""The method includes a functionality to get the health information
Raises:
Exception: Unspecified error by executing the API call
Returns:
api_call (dict): Returns the health information
"""
http = Api(self.grafana_api_model).create_the_http_api_client()
http_result = self._basic_get_call_without_token_auth(
http, f"{self.grafana_api_model.host}/api/health"
)
api_call: dict = json.loads(http_result.text)
if api_call == dict() or api_call.get("commit") is None:
logging.error(f"Check the error: {api_call}.")
raise Exception
else:
return api_call
def get_metrics(
self, basic_auth_username: str = None, basic_auth_password: str = None
) -> str:
"""The method includes a functionality to get the Grafana metrics information
Args:
basic_auth_username (str): Specify the optional basic auth username
basic_auth_password (str): Specify the optional basic auth password
Raises:
Exception: Unspecified error by executing the API call
Returns:
api_call (str): Returns the metrics information
"""
http = Api(self.grafana_api_model).create_the_http_api_client()
basic_auth = None
if basic_auth_username is not None and basic_auth_password is not None:
basic_auth = BasicAuth(basic_auth_username, basic_auth_password)
api_call: str = self._basic_get_call_without_token_auth(
http, f"{self.grafana_api_model.host}/metrics", basic_auth
).text
if len(api_call) == 0 or api_call is None:
logging.error(f"Check the error: {api_call}.")
raise Exception
else:
return api_call
def get_plugin_metrics(
self,
plugin_id: str,
basic_auth_username: str = None,
basic_auth_password: str = None,
) -> str:
"""The method includes a functionality to get the Grafana plugin metrics information
Args:
plugin_id (str): Specify the plugin id
basic_auth_username (str): Specify the optional basic auth username
basic_auth_password (str): Specify the optional basic auth password
Raises:
ValueError: Missed specifying a necessary value
Exception: Unspecified error by executing the API call
Returns:
api_call (str): Returns the metrics information
"""
http = Api(self.grafana_api_model).create_the_http_api_client()
basic_auth = None
if basic_auth_username is not None and basic_auth_password is not None:
basic_auth = BasicAuth(basic_auth_username, basic_auth_password)
if len(plugin_id) != 0:
url: str = f"{self.grafana_api_model.host}/metrics/plugins/{plugin_id}"
api_call: str = self._basic_get_call_without_token_auth(
http, url, basic_auth
).text
if len(api_call) == 0 or api_call is None:
logging.error(f"Check the error: {api_call}.")
raise Exception
else:
return api_call
else:
logging.error("There is no plugin_id defined.")
raise ValueError
def _basic_get_call_without_token_auth(
self, http: Union[Client, AsyncClient], url: str, basic_auth: BasicAuth = None
) -> Response:
"""The method includes a functionality to perform a basic GET call to an endpoint with optional BasicAuth
Args:
http (Union[Client, AsyncClient]): Specify the used client
url (str): Specify the url of the performed api call
basic_auth (BasicAuth): Specify the optional basic auth credentials (Default None)
Raises:
Exception: Unspecified error by executing the API call
Returns:
api_call (Response): Returns the corresponding result of the api call
"""
try:
if self.grafana_api_model.http2_support:
async def _execute_async_http_requests():
async with http:
return await http.request("GET", url, auth=basic_auth)
return asyncio.run(_execute_async_http_requests())
else:
return http.request("GET", url, auth=basic_auth)
except Exception as e:
raise e