Skip to content

Commit 6aa9701

Browse files
authored
Miscellaneous Maintenance (#163)
1 parent c576e39 commit 6aa9701

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+385
-628
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Test
33
on:
44

55
pull_request:
6-
branches: [ main ]
6+
branches: ~
77
paths:
88
- '.github/workflows/test.yml'
99
- 'pyproject.toml'

CHANGELOG.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
## unreleased
44

5-
* Change the HTTP backend Requests for Niquests.
6-
In certain conditions, you will need to adjust your code, as it no longer propagates
7-
`requests.exceptions.Timeout` exceptions, but uses `GrafanaTimeoutError` instead.
8-
[Niquests](https://github.com/jawah/niquests) is a drop-in replacement of Requests and therefore remains compatible.
9-
* Add asynchronous interface via `AsyncGrafanaClient`.
10-
* Remove Python 3.6 support.
5+
* Add asynchronous interface via `AsyncGrafanaClient`. Thanks, @Ousret.
6+
Changed the HTTP backend from `requests` to `niquests`.
7+
BREAKING CHANGE: In certain conditions, you will need to adjust your code,
8+
as it no longer propagates `requests.exceptions.Timeout` exceptions, but
9+
uses `GrafanaTimeoutError` instead. Other than this, [Niquests] is a drop-
10+
in replacement for [Requests] and therefore is largely compatible.
11+
* Remove Python 3.6 support. Thanks, @Ousret.
12+
13+
[Niquests]: https://niquests.readthedocs.io/
14+
[Riquests]: https://requests.readthedocs.io/
1115

1216
## 3.11.2 (2024-03-07)
1317

examples/datasource-health-probe.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ def prometheus_demo(grafana: GrafanaApi):
7878
datasource = DatasourceModel(
7979
name="probe-prometheus", type="prometheus", url="http://host.docker.internal:9090", access="server"
8080
)
81-
health_info = health_probe(grafana, datasource)
82-
return health_info
81+
return health_probe(grafana, datasource)
8382

8483

8584
def run(grafana: GrafanaApi, grafana_version: Version = None):

examples/grafana-tokenauth-healthcheck.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def run_conversation(grafana: GrafanaApi):
3636
# Connect to custom Grafana instance.
3737
grafana = GrafanaApi.from_url(
3838
url="http://localhost:3000/",
39-
credential=TokenAuth(token="eyJrIjoiMGJIcDBZZVdGV3VNWHYyV1J2dU5lcnVBSW1qYzR2c1MiLCJuIjoiZm9vIiwiaWQiOjF9"),
39+
credential=TokenAuth(token="eyJrIjoiMGJIcDBZZVdGV3VNWHYyV1J2dU5lcnVBSW1qYzR2c1MiLCJuIjoiZm9vIiwiaWQiOjF9"), # noqa: S106
4040
)
4141

4242
# Connect to Grafana instance of Grafana Labs fame.

examples/grafanalib-upload-dashboard.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,11 @@ def mkdashboard(uid: str, title: str = None, message: str = None, overwrite: boo
5252
implementation would stuff more details into the dashboard beforehand.
5353
"""
5454
dashboard = Dashboard(uid=uid, title=title)
55-
data = {
55+
return {
5656
"dashboard": encode_dashboard(dashboard),
5757
"overwrite": overwrite,
5858
"message": message,
5959
}
60-
return data
6160

6261

6362
def encode_dashboard(entity) -> Dict:

grafana_client/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class GrafanaBadInputError(GrafanaClientError):
4444
"""
4545

4646
def __init__(self, response):
47-
super(GrafanaBadInputError, self).__init__(400, response, "Bad Input: `{0}`".format(response))
47+
super(GrafanaBadInputError, self).__init__(400, response, f"Bad Input: `{response}`")
4848

4949

5050
class GrafanaUnauthorizedError(GrafanaClientError):
@@ -61,7 +61,7 @@ def __init__(self, token):
6161
self.token = token
6262

6363
def __call__(self, request):
64-
request.headers.update({"Authorization": "Bearer {0}".format(self.token)})
64+
request.headers.update({"Authorization": f"Bearer {self.token}"})
6565
return request
6666

6767

@@ -158,7 +158,7 @@ def _extract_from_response(r, accept_empty_json):
158158
raise GrafanaServerError(
159159
r.status_code,
160160
response,
161-
"Server Error {0}: {1}".format(r.status_code, message),
161+
f"Server Error {r.status_code}: {message}",
162162
)
163163
elif r.status_code == 400:
164164
raise GrafanaBadInputError(response)
@@ -168,7 +168,7 @@ def _extract_from_response(r, accept_empty_json):
168168
raise GrafanaClientError(
169169
r.status_code,
170170
response,
171-
"Client Error {0}: {1}".format(r.status_code, message),
171+
f"Client Error {r.status_code}: {message}",
172172
)
173173

174174
# `204 No Content` responses have an empty response body,

grafana_client/elements/_async/admin.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ async def settings(self):
1212
:return:
1313
"""
1414
path = "/admin/settings"
15-
r = await self.client.GET(path)
16-
return r
15+
return await self.client.GET(path)
1716

1817
async def stats(self):
1918
"""
2019
2120
:return:
2221
"""
2322
path = "/admin/stats"
24-
r = await self.client.GET(path)
25-
return r
23+
return await self.client.GET(path)
2624

2725
async def create_user(self, user):
2826
"""
@@ -31,8 +29,7 @@ async def create_user(self, user):
3129
:return:
3230
"""
3331
create_user_path = "/admin/users"
34-
r = await self.client.POST(create_user_path, json=user)
35-
return r
32+
return await self.client.POST(create_user_path, json=user)
3633

3734
async def change_user_password(self, user_id, password):
3835
"""
@@ -42,8 +39,7 @@ async def change_user_password(self, user_id, password):
4239
:return:
4340
"""
4441
change_user_password_path = "/admin/users/%s/password" % user_id
45-
r = await self.client.PUT(change_user_password_path, json={"password": password})
46-
return r
42+
return await self.client.PUT(change_user_password_path, json={"password": password})
4743

4844
async def change_user_permissions(self, user_id, is_grafana_admin):
4945
"""
@@ -53,8 +49,7 @@ async def change_user_permissions(self, user_id, is_grafana_admin):
5349
:return:
5450
"""
5551
change_user_permissions = "/admin/users/%s/permissions" % user_id
56-
r = await self.client.PUT(change_user_permissions, json={"isGrafanaAdmin": is_grafana_admin})
57-
return r
52+
return await self.client.PUT(change_user_permissions, json={"isGrafanaAdmin": is_grafana_admin})
5853

5954
async def delete_user(self, user_id):
6055
"""
@@ -63,8 +58,7 @@ async def delete_user(self, user_id):
6358
:return:
6459
"""
6560
delete_user_path = "/admin/users/%s" % user_id
66-
r = await self.client.DELETE(delete_user_path)
67-
return r
61+
return await self.client.DELETE(delete_user_path)
6862

6963
async def pause_all_alerts(self, pause):
7064
"""
@@ -73,8 +67,7 @@ async def pause_all_alerts(self, pause):
7367
:return:
7468
"""
7569
change_user_permissions = "/admin/pause-all-alerts"
76-
r = await self.client.POST(change_user_permissions, json={"paused": pause})
77-
return r
70+
return await self.client.POST(change_user_permissions, json={"paused": pause})
7871

7972
async def set_user_enabled(self, user_id, enabled: bool):
8073
"""
@@ -85,5 +78,4 @@ async def set_user_enabled(self, user_id, enabled: bool):
8578
"""
8679
action = "enable" if enabled else "disable"
8780
set_user_enabled = "/admin/users/%s/%s" % (user_id, action)
88-
r = await self.client.POST(set_user_enabled)
89-
return r
81+
return await self.client.POST(set_user_enabled)

grafana_client/elements/_async/alerting.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ async def get_alertrule(self, folder_name, alertrule_name):
1313
:return:
1414
"""
1515
get_alertrule_path = "/ruler/grafana/api/v1/rules/%s/%s" % (folder_name, alertrule_name)
16-
r = await self.client.GET(get_alertrule_path)
17-
return r
16+
return await self.client.GET(get_alertrule_path)
1817

1918
async def create_alertrule(self, folder_name, alertrule):
2019
"""
@@ -23,8 +22,7 @@ async def create_alertrule(self, folder_name, alertrule):
2322
:return:
2423
"""
2524
create_alertrule_path = "/ruler/grafana/api/v1/rules/%s" % folder_name
26-
r = await self.client.POST(create_alertrule_path, json=alertrule)
27-
return r
25+
return await self.client.POST(create_alertrule_path, json=alertrule)
2826

2927
async def update_alertrule(self, folder_name, alertrule):
3028
"""
@@ -34,8 +32,7 @@ async def update_alertrule(self, folder_name, alertrule):
3432
"""
3533

3634
update_alertrule_path = "/ruler/grafana/api/v1/rules/%s" % folder_name
37-
r = await self.client.POST(update_alertrule_path, json=alertrule)
38-
return r
35+
return await self.client.POST(update_alertrule_path, json=alertrule)
3936

4037
async def delete_alertrule(self, folder_name, alertrule_name):
4138
"""
@@ -45,5 +42,4 @@ async def delete_alertrule(self, folder_name, alertrule_name):
4542
"""
4643

4744
delete_alertrule_path = "/ruler/grafana/api/v1/rules/%s/%s" % (folder_name, alertrule_name)
48-
r = await self.client.DELETE(delete_alertrule_path)
49-
return r
45+
return await self.client.DELETE(delete_alertrule_path)

grafana_client/elements/_async/alertingprovisioning.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ async def get_alertrules_all(self):
1212
@return:
1313
"""
1414
get_alertrules_all_path = "/v1/provisioning/alert-rules"
15-
r = await self.client.GET(get_alertrules_all_path)
16-
return r
15+
return await self.client.GET(get_alertrules_all_path)
1716

1817
async def get_alertrule(self, alertrule_uid):
1918
"""
@@ -22,8 +21,7 @@ async def get_alertrule(self, alertrule_uid):
2221
:return:
2322
"""
2423
get_alertrule_path = "/v1/provisioning/alert-rules/%s" % alertrule_uid
25-
r = await self.client.GET(get_alertrule_path)
26-
return r
24+
return await self.client.GET(get_alertrule_path)
2725

2826
async def create_alertrule(self, alertrule, disable_provenance=False):
2927
"""
@@ -35,8 +33,7 @@ async def create_alertrule(self, alertrule, disable_provenance=False):
3533
headers = {}
3634
if disable_provenance:
3735
headers["X-Disable-Provenance"] = "true"
38-
r = await self.client.POST(create_alertrule_path, json=alertrule, headers=headers)
39-
return r
36+
return await self.client.POST(create_alertrule_path, json=alertrule, headers=headers)
4037

4138
async def update_alertrule(self, alertrule_uid, alertrule, disable_provenance=False):
4239
"""
@@ -49,8 +46,7 @@ async def update_alertrule(self, alertrule_uid, alertrule, disable_provenance=Fa
4946
headers = {}
5047
if disable_provenance:
5148
headers["X-Disable-Provenance"] = "true"
52-
r = await self.client.PUT(update_alertrule_path, json=alertrule, headers=headers)
53-
return r
49+
return await self.client.PUT(update_alertrule_path, json=alertrule, headers=headers)
5450

5551
async def get_rule_group(self, folder_uid, group_uid):
5652
"""
@@ -60,8 +56,7 @@ async def get_rule_group(self, folder_uid, group_uid):
6056
:return:
6157
"""
6258
get_rule_group_path = "/v1/provisioning/folder/%s/rule-groups/%s" % (folder_uid, group_uid)
63-
r = await self.client.GET(get_rule_group_path)
64-
return r
59+
return await self.client.GET(get_rule_group_path)
6560

6661
async def update_rule_group(self, folder_uid, group_uid, alertrule_group, disable_provenance=False):
6762
"""
@@ -74,8 +69,7 @@ async def update_rule_group(self, folder_uid, group_uid, alertrule_group, disabl
7469
if disable_provenance:
7570
headers["X-Disable-Provenance"] = "true"
7671
update_rule_group_path = "/v1/provisioning/folder/%s/rule-groups/%s" % (folder_uid, group_uid)
77-
r = await self.client.PUT(update_rule_group_path, json=alertrule_group, headers=headers)
78-
return r
72+
return await self.client.PUT(update_rule_group_path, json=alertrule_group, headers=headers)
7973

8074
async def update_rule_group_interval(self, folder_uid, group_uid, alertrule_group, disable_provenance=False):
8175
"""
@@ -94,8 +88,7 @@ async def delete_alertrule(self, alertrule_uid):
9488
"""
9589

9690
delete_alertrule_path = "/v1/provisioning/alert-rules/%s" % alertrule_uid
97-
r = await self.client.DELETE(delete_alertrule_path)
98-
return r
91+
return await self.client.DELETE(delete_alertrule_path)
9992

10093
async def get_contactpoints(self):
10194
"""

grafana_client/elements/_async/annotations.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ async def get_annotation(
7171
list_annotations_path += "?"
7272
list_annotations_path += "&".join(params)
7373

74-
r = await self.client.GET(list_annotations_path)
75-
76-
return r
74+
return await self.client.GET(list_annotations_path)
7775

7876
async def add_annotation(
7977
self,
@@ -111,9 +109,7 @@ async def add_annotation(
111109
if dashboard_uid is not None:
112110
payload["dashboardUID"] = dashboard_uid
113111

114-
r = await self.client.POST(annotations_path, json=payload)
115-
116-
return r
112+
return await self.client.POST(annotations_path, json=payload)
117113

118114
async def add_annotation_graphite(
119115
self,
@@ -135,9 +131,7 @@ async def add_annotation_graphite(
135131
annotations_path = "/annotations/graphite"
136132
payload = {"what": what, "tags": tags, "when": when, "data": data}
137133

138-
r = await self.client.POST(annotations_path, json=payload)
139-
140-
return r
134+
return await self.client.POST(annotations_path, json=payload)
141135

142136
async def update_annotation(
143137
self,
@@ -156,12 +150,10 @@ async def update_annotation(
156150
:param text:
157151
:return:
158152
"""
159-
annotations_path = "/annotations/{}".format(annotations_id)
153+
annotations_path = f"/annotations/{annotations_id}"
160154
payload = {"time": time_from, "timeEnd": time_to, "tags": tags, "text": text}
161155

162-
r = await self.client.PUT(annotations_path, json=payload)
163-
164-
return r
156+
return await self.client.PUT(annotations_path, json=payload)
165157

166158
async def partial_update_annotation(
167159
self,
@@ -181,14 +173,12 @@ async def partial_update_annotation(
181173
:param text:
182174
:return:
183175
"""
184-
annotations_path = "/annotations/{}".format(annotations_id)
176+
annotations_path = f"/annotations/{annotations_id}"
185177
payload = {}
186178

187179
payload = {"time": time_from, "timeEnd": time_to, "tags": tags, "text": text}
188180

189-
r = await self.client.PATCH(annotations_path, json=payload)
190-
191-
return r
181+
return await self.client.PATCH(annotations_path, json=payload)
192182

193183
async def delete_annotations_by_id(self, annotations_id=None):
194184
"""
@@ -197,7 +187,5 @@ async def delete_annotations_by_id(self, annotations_id=None):
197187
:param annotations_id:
198188
:return:
199189
"""
200-
annotations_path = "/annotations/{}".format(annotations_id)
201-
r = await self.client.DELETE(annotations_path)
202-
203-
return r
190+
annotations_path = f"/annotations/{annotations_id}"
191+
return await self.client.DELETE(annotations_path)

0 commit comments

Comments
 (0)