-
Notifications
You must be signed in to change notification settings - Fork 30
/
client.py
277 lines (226 loc) · 8.24 KB
/
client.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from json import JSONDecodeError
import niquests
import niquests.auth
from niquests import HTTPError, Timeout
DEFAULT_TIMEOUT: float = 5.0
DEFAULT_SESSION_POOL_SIZE: int = 10
class GrafanaException(Exception):
def __init__(self, status_code, response, message):
self.status_code = status_code
self.response = response
self.message = message
# Backwards compatible with implementations that rely on just the message.
super(GrafanaException, self).__init__(message)
class GrafanaTimeoutError(GrafanaException):
"""
A timeout occurred
"""
class GrafanaServerError(GrafanaException):
"""
5xx
"""
pass
class GrafanaClientError(GrafanaException):
"""
Invalid input (4xx errors)
"""
pass
class GrafanaBadInputError(GrafanaClientError):
"""
400
"""
def __init__(self, response):
super(GrafanaBadInputError, self).__init__(400, response, f"Bad Input: `{response}`")
class GrafanaUnauthorizedError(GrafanaClientError):
"""
401
"""
def __init__(self, response):
super(GrafanaUnauthorizedError, self).__init__(401, response, "Unauthorized")
class TokenAuth(niquests.auth.AuthBase):
def __init__(self, token):
self.token = token
def __call__(self, request):
request.headers.update({"Authorization": f"Bearer {self.token}"})
return request
class HeaderAuth(niquests.auth.AuthBase):
def __init__(self, name, value):
self.name = name
self.value = value
def __call__(self, request):
request.headers.update({self.name: self.value})
return request
class GrafanaClient:
def __init__(
self,
auth,
host="localhost",
port=None,
url_path_prefix="",
protocol="http",
verify=True,
timeout=DEFAULT_TIMEOUT,
user_agent: str = None,
organization_id: int = None,
session_pool_size=DEFAULT_SESSION_POOL_SIZE,
):
self.auth = auth
self.verify = verify
self.timeout = timeout
self.url_host = host
self.url_port = port
self.url_path_prefix = url_path_prefix
self.url_protocol = protocol
self.session_pool_size = session_pool_size
def construct_api_url():
params = {
"protocol": self.url_protocol,
"host": self.url_host,
"url_path_prefix": self.url_path_prefix,
}
if self.url_port is None:
url_pattern = "{protocol}://{host}/{url_path_prefix}api"
else:
params["port"] = self.url_port
url_pattern = "{protocol}://{host}:{port}/{url_path_prefix}api"
return url_pattern.format(**params)
self.url = construct_api_url()
from grafana_client import __appname__, __version__
self.user_agent = user_agent or f"{__appname__}/{__version__}"
self.s = niquests.Session(pool_maxsize=session_pool_size)
self.s.headers["User-Agent"] = self.user_agent
self.organization_id = organization_id
if self.organization_id:
# orgId is defined in the openapi3 spec as an int64, but headers need to be a str
self.s.headers["X-Grafana-Org-Id"] = str(self.organization_id)
if self.auth is not None:
if isinstance(self.auth, niquests.auth.AuthBase):
pass
elif isinstance(self.auth, tuple):
self.auth = niquests.auth.HTTPBasicAuth(*self.auth)
else:
self.auth = TokenAuth(self.auth)
def _make_url(self, url):
return f"{self.url}{url}"
@staticmethod
def _ensure_valid_json_arg(json):
if json is not None and not isinstance(json, (dict, list)):
raise TypeError(
f"JSON request payload has invalid shape. "
f"Accepted are dictionaries and lists. "
f"The type is: {type(json)}"
)
@staticmethod
def _extract_from_response(r, accept_empty_json):
if r.status_code >= 400:
try:
response = r.json()
except ValueError:
response = r.text
message = response["message"] if isinstance(response, dict) and "message" in response else r.text
if 500 <= r.status_code < 600:
raise GrafanaServerError(
r.status_code,
response,
f"Server Error {r.status_code}: {message}",
)
elif r.status_code == 400:
raise GrafanaBadInputError(response)
elif r.status_code == 401:
raise GrafanaUnauthorizedError(response)
elif 400 <= r.status_code < 500:
raise GrafanaClientError(
r.status_code,
response,
f"Client Error {r.status_code}: {message}",
)
# `204 No Content` responses have an empty response body,
# so it doesn't decode well from JSON.
if r.status_code == 204:
return None
# The "Tempo" data source responds with text/plain.
content_type = r.headers.get("Content-Type", "")
if content_type.startswith("text/"):
return r.text
try:
return r.json()
except JSONDecodeError:
if accept_empty_json and r.text == "":
return ""
else:
raise
def __getattr__(self, item):
def __request_runner(url, json=None, data=None, params=None, headers=None, accept_empty_json=False):
__url = self._make_url(url)
# Sanity checks.
self._ensure_valid_json_arg(json)
try:
r = self.s.request(
item.lower(),
__url,
json=json,
data=data,
params=params,
headers=headers,
auth=self.auth,
verify=self.verify,
timeout=self.timeout,
)
except Timeout as e:
raise GrafanaTimeoutError(0, None, str(e)) from e
except HTTPError as e:
# Make sure to not leak any exception types of the requests implementation.
raise GrafanaException(0, None, str(e)) from e
return self._extract_from_response(r, accept_empty_json)
return __request_runner
class AsyncGrafanaClient(GrafanaClient):
def __init__(
self,
auth,
host="localhost",
port=None,
url_path_prefix="",
protocol="http",
verify=True,
timeout=DEFAULT_TIMEOUT,
user_agent: str = None,
organization_id: int = None,
session_pool_size=DEFAULT_SESSION_POOL_SIZE,
):
super().__init__(
auth,
host=host,
port=port,
url_path_prefix=url_path_prefix,
protocol=protocol,
verify=verify,
timeout=timeout,
user_agent=user_agent,
organization_id=organization_id,
session_pool_size=session_pool_size,
)
self.s = niquests.AsyncSession(pool_maxsize=session_pool_size)
self.s.headers.setdefault("Connection", "keep-alive")
def __getattr__(self, item):
async def __request_runner(url, json=None, data=None, params=None, headers=None, accept_empty_json=False):
__url = self._make_url(url)
# Sanity checks.
self._ensure_valid_json_arg(json)
try:
r = await self.s.request(
item.lower(),
__url,
json=json,
data=data,
params=params,
headers=headers,
auth=self.auth,
verify=self.verify,
timeout=self.timeout,
)
except Timeout as e:
raise GrafanaTimeoutError(0, None, str(e)) from e
except HTTPError as e:
raise GrafanaException(0, None, str(e)) from e
return self._extract_from_response(r, accept_empty_json)
return __request_runner