89 lines
1.9 KiB
Go
89 lines
1.9 KiB
Go
package grafana
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/url"
|
|
"strconv"
|
|
)
|
|
|
|
const (
|
|
SearchTypeFolder SearchParamType = "dash-folder"
|
|
SearchTypeDashboard SearchParamType = "dash-db"
|
|
)
|
|
|
|
type FoundBoard struct {
|
|
ID uint `json:"id"`
|
|
UID string `json:"uid"`
|
|
Title string `json:"title"`
|
|
URI string `json:"uri"`
|
|
URL string `json:"url"`
|
|
Slug string `json:"slug"`
|
|
Type string `json:"type"`
|
|
Tags []string `json:"tags"`
|
|
IsStarred bool `json:"isStarred"`
|
|
FolderID int `json:"folderId"`
|
|
FolderUID string `json:"folderUid"`
|
|
FolderTitle string `json:"folderTitle"`
|
|
FolderURL string `json:"folderUrl"`
|
|
}
|
|
|
|
type (
|
|
// SearchParam is a type for specifying Search params.
|
|
SearchParam func(*url.Values)
|
|
// SearchParamType is a type accepted by SearchType func.
|
|
SearchParamType string
|
|
// QueryParam is a type for specifying arbitrary API parameters
|
|
QueryParam func(*url.Values)
|
|
)
|
|
|
|
func (c *Client) Search(ctx context.Context, params ...SearchParam) ([]FoundBoard, error) {
|
|
var (
|
|
raw []byte
|
|
boards []FoundBoard
|
|
code int
|
|
err error
|
|
)
|
|
u := url.URL{}
|
|
q := u.Query()
|
|
for _, p := range params {
|
|
p(&q)
|
|
}
|
|
if raw, code, err = c.get(ctx, "api/search", q); err != nil {
|
|
return nil, err
|
|
}
|
|
if code != 200 {
|
|
return nil, fmt.Errorf("HTTP error %d: returns %s", code, raw)
|
|
}
|
|
err = json.Unmarshal(raw, &boards)
|
|
return boards, err
|
|
}
|
|
|
|
func SearchQuery(query string) SearchParam {
|
|
return func(v *url.Values) {
|
|
if query != "" {
|
|
v.Set("query", query)
|
|
}
|
|
}
|
|
}
|
|
|
|
func SearchStarred(starred bool) SearchParam {
|
|
return func(v *url.Values) {
|
|
v.Set("starred", strconv.FormatBool(starred))
|
|
}
|
|
}
|
|
|
|
func SearchTag(tag string) SearchParam {
|
|
return func(v *url.Values) {
|
|
if tag != "" {
|
|
v.Add("tag", tag)
|
|
}
|
|
}
|
|
}
|
|
|
|
func SearchType(searchType SearchParamType) SearchParam {
|
|
return func(v *url.Values) {
|
|
v.Set("type", string(searchType))
|
|
}
|
|
}
|