Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous Maintenance #163

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Test
on:

pull_request:
branches: [ main ]
branches: ~
paths:
- '.github/workflows/test.yml'
- 'pyproject.toml'
Expand Down
16 changes: 10 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

## unreleased

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

[Niquests]: https://niquests.readthedocs.io/
[Riquests]: https://requests.readthedocs.io/

## 3.11.2 (2024-03-07)

Expand Down
3 changes: 1 addition & 2 deletions examples/datasource-health-probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ def prometheus_demo(grafana: GrafanaApi):
datasource = DatasourceModel(
name="probe-prometheus", type="prometheus", url="http://host.docker.internal:9090", access="server"
)
health_info = health_probe(grafana, datasource)
return health_info
return health_probe(grafana, datasource)


def run(grafana: GrafanaApi, grafana_version: Version = None):
Expand Down
2 changes: 1 addition & 1 deletion examples/grafana-tokenauth-healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def run_conversation(grafana: GrafanaApi):
# Connect to custom Grafana instance.
grafana = GrafanaApi.from_url(
url="http://localhost:3000/",
credential=TokenAuth(token="eyJrIjoiMGJIcDBZZVdGV3VNWHYyV1J2dU5lcnVBSW1qYzR2c1MiLCJuIjoiZm9vIiwiaWQiOjF9"),
credential=TokenAuth(token="eyJrIjoiMGJIcDBZZVdGV3VNWHYyV1J2dU5lcnVBSW1qYzR2c1MiLCJuIjoiZm9vIiwiaWQiOjF9"), # noqa: S106
)

# Connect to Grafana instance of Grafana Labs fame.
Expand Down
3 changes: 1 addition & 2 deletions examples/grafanalib-upload-dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ def mkdashboard(uid: str, title: str = None, message: str = None, overwrite: boo
implementation would stuff more details into the dashboard beforehand.
"""
dashboard = Dashboard(uid=uid, title=title)
data = {
return {
"dashboard": encode_dashboard(dashboard),
"overwrite": overwrite,
"message": message,
}
return data


def encode_dashboard(entity) -> Dict:
Expand Down
8 changes: 4 additions & 4 deletions grafana_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class GrafanaBadInputError(GrafanaClientError):
"""

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


class GrafanaUnauthorizedError(GrafanaClientError):
Expand All @@ -61,7 +61,7 @@ def __init__(self, token):
self.token = token

def __call__(self, request):
request.headers.update({"Authorization": "Bearer {0}".format(self.token)})
request.headers.update({"Authorization": f"Bearer {self.token}"})
return request


Expand Down Expand Up @@ -158,7 +158,7 @@ def _extract_from_response(r, accept_empty_json):
raise GrafanaServerError(
r.status_code,
response,
"Server Error {0}: {1}".format(r.status_code, message),
f"Server Error {r.status_code}: {message}",
)
elif r.status_code == 400:
raise GrafanaBadInputError(response)
Expand All @@ -168,7 +168,7 @@ def _extract_from_response(r, accept_empty_json):
raise GrafanaClientError(
r.status_code,
response,
"Client Error {0}: {1}".format(r.status_code, message),
f"Client Error {r.status_code}: {message}",
)

# `204 No Content` responses have an empty response body,
Expand Down
24 changes: 8 additions & 16 deletions grafana_client/elements/_async/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ async def settings(self):
:return:
"""
path = "/admin/settings"
r = await self.client.GET(path)
return r
return await self.client.GET(path)

async def stats(self):
"""

:return:
"""
path = "/admin/stats"
r = await self.client.GET(path)
return r
return await self.client.GET(path)

async def create_user(self, user):
"""
Expand All @@ -31,8 +29,7 @@ async def create_user(self, user):
:return:
"""
create_user_path = "/admin/users"
r = await self.client.POST(create_user_path, json=user)
return r
return await self.client.POST(create_user_path, json=user)

async def change_user_password(self, user_id, password):
"""
Expand All @@ -42,8 +39,7 @@ async def change_user_password(self, user_id, password):
:return:
"""
change_user_password_path = "/admin/users/%s/password" % user_id
r = await self.client.PUT(change_user_password_path, json={"password": password})
return r
return await self.client.PUT(change_user_password_path, json={"password": password})

async def change_user_permissions(self, user_id, is_grafana_admin):
"""
Expand All @@ -53,8 +49,7 @@ async def change_user_permissions(self, user_id, is_grafana_admin):
:return:
"""
change_user_permissions = "/admin/users/%s/permissions" % user_id
r = await self.client.PUT(change_user_permissions, json={"isGrafanaAdmin": is_grafana_admin})
return r
return await self.client.PUT(change_user_permissions, json={"isGrafanaAdmin": is_grafana_admin})

async def delete_user(self, user_id):
"""
Expand All @@ -63,8 +58,7 @@ async def delete_user(self, user_id):
:return:
"""
delete_user_path = "/admin/users/%s" % user_id
r = await self.client.DELETE(delete_user_path)
return r
return await self.client.DELETE(delete_user_path)

async def pause_all_alerts(self, pause):
"""
Expand All @@ -73,8 +67,7 @@ async def pause_all_alerts(self, pause):
:return:
"""
change_user_permissions = "/admin/pause-all-alerts"
r = await self.client.POST(change_user_permissions, json={"paused": pause})
return r
return await self.client.POST(change_user_permissions, json={"paused": pause})

async def set_user_enabled(self, user_id, enabled: bool):
"""
Expand All @@ -85,5 +78,4 @@ async def set_user_enabled(self, user_id, enabled: bool):
"""
action = "enable" if enabled else "disable"
set_user_enabled = "/admin/users/%s/%s" % (user_id, action)
r = await self.client.POST(set_user_enabled)
return r
return await self.client.POST(set_user_enabled)
12 changes: 4 additions & 8 deletions grafana_client/elements/_async/alerting.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ async def get_alertrule(self, folder_name, alertrule_name):
:return:
"""
get_alertrule_path = "/ruler/grafana/api/v1/rules/%s/%s" % (folder_name, alertrule_name)
r = await self.client.GET(get_alertrule_path)
return r
return await self.client.GET(get_alertrule_path)

async def create_alertrule(self, folder_name, alertrule):
"""
Expand All @@ -23,8 +22,7 @@ async def create_alertrule(self, folder_name, alertrule):
:return:
"""
create_alertrule_path = "/ruler/grafana/api/v1/rules/%s" % folder_name
r = await self.client.POST(create_alertrule_path, json=alertrule)
return r
return await self.client.POST(create_alertrule_path, json=alertrule)

async def update_alertrule(self, folder_name, alertrule):
"""
Expand All @@ -34,8 +32,7 @@ async def update_alertrule(self, folder_name, alertrule):
"""

update_alertrule_path = "/ruler/grafana/api/v1/rules/%s" % folder_name
r = await self.client.POST(update_alertrule_path, json=alertrule)
return r
return await self.client.POST(update_alertrule_path, json=alertrule)

async def delete_alertrule(self, folder_name, alertrule_name):
"""
Expand All @@ -45,5 +42,4 @@ async def delete_alertrule(self, folder_name, alertrule_name):
"""

delete_alertrule_path = "/ruler/grafana/api/v1/rules/%s/%s" % (folder_name, alertrule_name)
r = await self.client.DELETE(delete_alertrule_path)
return r
return await self.client.DELETE(delete_alertrule_path)
21 changes: 7 additions & 14 deletions grafana_client/elements/_async/alertingprovisioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ async def get_alertrules_all(self):
@return:
"""
get_alertrules_all_path = "/v1/provisioning/alert-rules"
r = await self.client.GET(get_alertrules_all_path)
return r
return await self.client.GET(get_alertrules_all_path)

async def get_alertrule(self, alertrule_uid):
"""
Expand All @@ -22,8 +21,7 @@ async def get_alertrule(self, alertrule_uid):
:return:
"""
get_alertrule_path = "/v1/provisioning/alert-rules/%s" % alertrule_uid
r = await self.client.GET(get_alertrule_path)
return r
return await self.client.GET(get_alertrule_path)

async def create_alertrule(self, alertrule, disable_provenance=False):
"""
Expand All @@ -35,8 +33,7 @@ async def create_alertrule(self, alertrule, disable_provenance=False):
headers = {}
if disable_provenance:
headers["X-Disable-Provenance"] = "true"
r = await self.client.POST(create_alertrule_path, json=alertrule, headers=headers)
return r
return await self.client.POST(create_alertrule_path, json=alertrule, headers=headers)

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

async def get_rule_group(self, folder_uid, group_uid):
"""
Expand All @@ -60,8 +56,7 @@ async def get_rule_group(self, folder_uid, group_uid):
:return:
"""
get_rule_group_path = "/v1/provisioning/folder/%s/rule-groups/%s" % (folder_uid, group_uid)
r = await self.client.GET(get_rule_group_path)
return r
return await self.client.GET(get_rule_group_path)

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

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

delete_alertrule_path = "/v1/provisioning/alert-rules/%s" % alertrule_uid
r = await self.client.DELETE(delete_alertrule_path)
return r
return await self.client.DELETE(delete_alertrule_path)

async def get_contactpoints(self):
"""
Expand Down
30 changes: 9 additions & 21 deletions grafana_client/elements/_async/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ async def get_annotation(
list_annotations_path += "?"
list_annotations_path += "&".join(params)

r = await self.client.GET(list_annotations_path)

return r
return await self.client.GET(list_annotations_path)

async def add_annotation(
self,
Expand Down Expand Up @@ -111,9 +109,7 @@ async def add_annotation(
if dashboard_uid is not None:
payload["dashboardUID"] = dashboard_uid

r = await self.client.POST(annotations_path, json=payload)

return r
return await self.client.POST(annotations_path, json=payload)

async def add_annotation_graphite(
self,
Expand All @@ -135,9 +131,7 @@ async def add_annotation_graphite(
annotations_path = "/annotations/graphite"
payload = {"what": what, "tags": tags, "when": when, "data": data}

r = await self.client.POST(annotations_path, json=payload)

return r
return await self.client.POST(annotations_path, json=payload)

async def update_annotation(
self,
Expand All @@ -156,12 +150,10 @@ async def update_annotation(
:param text:
:return:
"""
annotations_path = "/annotations/{}".format(annotations_id)
annotations_path = f"/annotations/{annotations_id}"
payload = {"time": time_from, "timeEnd": time_to, "tags": tags, "text": text}

r = await self.client.PUT(annotations_path, json=payload)

return r
return await self.client.PUT(annotations_path, json=payload)

async def partial_update_annotation(
self,
Expand All @@ -181,14 +173,12 @@ async def partial_update_annotation(
:param text:
:return:
"""
annotations_path = "/annotations/{}".format(annotations_id)
annotations_path = f"/annotations/{annotations_id}"
payload = {}

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

r = await self.client.PATCH(annotations_path, json=payload)

return r
return await self.client.PATCH(annotations_path, json=payload)

async def delete_annotations_by_id(self, annotations_id=None):
"""
Expand All @@ -197,7 +187,5 @@ async def delete_annotations_by_id(self, annotations_id=None):
:param annotations_id:
:return:
"""
annotations_path = "/annotations/{}".format(annotations_id)
r = await self.client.DELETE(annotations_path)

return r
annotations_path = f"/annotations/{annotations_id}"
return await self.client.DELETE(annotations_path)
Loading
Loading