package grafana import ( "context" "net/url" "strconv" "git.ar21.de/yolokube/grafana-backuper/pkg/grafana/schema" ) type ( SearchParam func(*url.Values) SearchType string ) const ( SearchTypeFolder SearchType = "dash-folder" SearchTypeDashboard SearchType = "dash-db" ) func WithType(searchType SearchType) SearchParam { return func(v *url.Values) { v.Set("type", string(searchType)) } } func WithQuery(query string) SearchParam { return func(v *url.Values) { if query != "" { v.Set("query", query) } } } func WithStarred() SearchParam { return func(v *url.Values) { v.Set("starred", strconv.FormatBool(true)) } } func WithTag(tag string) SearchParam { return func(v *url.Values) { if tag != "" { v.Add("tag", tag) } } } type SearchResult struct { ID uint UID string Title string URI string URL string Slug string Type string Tags []string IsStarred bool SortMeta uint FolderID int FolderUID string FolderTitle string FolderURL string } type SearchClient struct { client *Client } func (c *SearchClient) Search(ctx context.Context, params ...SearchParam) ([]*SearchResult, *Response, error) { searchURL, err := url.Parse("/search") if err != nil { return nil, nil, err } if len(params) > 0 { query := searchURL.Query() for _, param := range params { param(&query) } searchURL.RawQuery = query.Encode() } req, err := c.client.newRequest(ctx, "GET", searchURL.String(), nil) if err != nil { return nil, nil, err } var body schema.SearchResultListResponse resp, err := c.client.do(req, &body) if err != nil { return nil, resp, err } searchResults := make([]*SearchResult, 0, len(body)) for _, searchResult := range body { searchResults = append(searchResults, SearchResultFromSchema(searchResult)) } return searchResults, resp, nil }