Skip to content

Commit

Permalink
Correct dashboard search endpoint, fixes #174. Incidentally add util …
Browse files Browse the repository at this point in the history
…to accept and format list-type params.
  • Loading branch information
chintal committed Apr 14, 2024
1 parent 443194e commit d341a2b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
33 changes: 21 additions & 12 deletions grafana_client/elements/_async/search.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from grafana_client.util import format_param_value

from ..base import Base


Expand All @@ -12,7 +14,9 @@ async def search_dashboards(
tag=None,
type_=None,
dashboard_ids=None,
dashboard_uids=None,
folder_ids=None,
folder_uids=None,
starred=None,
limit=None,
):
Expand All @@ -22,36 +26,41 @@ async def search_dashboards(
:param tag:
:param type_:
:param dashboard_ids:
:param dashboard_uids:
:param folder_ids:
:param folder_uids:
:param starred:
:param limit:
:return:
"""
list_dashboard_path = "/search"
params = []
params = {}

if query:
params.append("query=%s" % query)
params["query"] = query

if tag:
params.append("tag=%s" % tag)
params["tag"] = format_param_value(tag)

if type_:
params.append("type=%s" % type_)
params["type"] = type_

if dashboard_ids:
params.append("dashboardIds=%s" % dashboard_ids)
params["dashboardIds"] = format_param_value(dashboard_ids)

if dashboard_uids:
params["dashboardUIDs"] = format_param_value(dashboard_uids)

if folder_ids:
params.append("folderIds=%s" % folder_ids)
params["folderIds"] = format_param_value(folder_ids)

if folder_uids:
params["folderUIDs"] = format_param_value(folder_uids)

if starred:
params.append("starred=%s" % starred)
params["starred"] = starred

if limit:
params.append("limit=%s" % limit)

list_dashboard_path += "?"
list_dashboard_path += "&".join(params)
params["limit"] = limit

return await self.client.GET(list_dashboard_path)
return await self.client.GET(list_dashboard_path, params=params)
33 changes: 21 additions & 12 deletions grafana_client/elements/search.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from grafana_client.util import format_param_value

from .base import Base


Expand All @@ -12,7 +14,9 @@ def search_dashboards(
tag=None,
type_=None,
dashboard_ids=None,
dashboard_uids=None,
folder_ids=None,
folder_uids=None,
starred=None,
limit=None,
):
Expand All @@ -22,36 +26,41 @@ def search_dashboards(
:param tag:
:param type_:
:param dashboard_ids:
:param dashboard_uids:
:param folder_ids:
:param folder_uids:
:param starred:
:param limit:
:return:
"""
list_dashboard_path = "/search"
params = []
params = {}

if query:
params.append("query=%s" % query)
params["query"] = query

if tag:
params.append("tag=%s" % tag)
params["tag"] = format_param_value(tag)

if type_:
params.append("type=%s" % type_)
params["type"] = type_

if dashboard_ids:
params.append("dashboardIds=%s" % dashboard_ids)
params["dashboardIds"] = format_param_value(dashboard_ids)

if dashboard_uids:
params["dashboardUIDs"] = format_param_value(dashboard_uids)

Check warning on line 52 in grafana_client/elements/search.py

View check run for this annotation

Codecov / codecov/patch

grafana_client/elements/search.py#L52

Added line #L52 was not covered by tests

if folder_ids:
params.append("folderIds=%s" % folder_ids)
params["folderIds"] = format_param_value(folder_ids)

if folder_uids:
params["folderUIDs"] = format_param_value(folder_uids)

Check warning on line 58 in grafana_client/elements/search.py

View check run for this annotation

Codecov / codecov/patch

grafana_client/elements/search.py#L58

Added line #L58 was not covered by tests

if starred:
params.append("starred=%s" % starred)
params["starred"] = starred

if limit:
params.append("limit=%s" % limit)

list_dashboard_path += "?"
list_dashboard_path += "&".join(params)
params["limit"] = limit

return self.client.GET(list_dashboard_path)
return self.client.GET(list_dashboard_path, params=params)
7 changes: 7 additions & 0 deletions grafana_client/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,10 @@ def as_bool(value: str) -> bool:
return _STR_BOOLEAN_MAPPING[value.lower()]
except KeyError:
raise ValueError(f"invalid truth value {value}")


def format_param_value(maybe_list):
if isinstance(maybe_list, list):
return ",".join([str(x) for x in maybe_list])

Check warning on line 46 in grafana_client/util.py

View check run for this annotation

Codecov / codecov/patch

grafana_client/util.py#L46

Added line #L46 was not covered by tests
else:
return maybe_list

0 comments on commit d341a2b

Please sign in to comment.