This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
playlist.go
79 lines (65 loc) · 1.91 KB
/
playlist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package gapi
import (
"encoding/json"
"fmt"
)
// PlaylistItem represents a Grafana playlist item.
type PlaylistItem struct {
Type string `json:"type"`
Value string `json:"value"`
Order int `json:"order"`
Title string `json:"title"`
}
// Playlist represents a Grafana playlist.
type Playlist struct {
ID int `json:"id,omitempty"` // Grafana < 9.0
UID string `json:"uid,omitempty"` // Grafana >= 9.0
Name string `json:"name"`
Interval string `json:"interval"`
Items []PlaylistItem `json:"items"`
}
// Grafana 9.0+ returns the ID and the UID but uses the UID in the API calls.
// Grafana <9 only returns the ID.
func (p *Playlist) QueryID() string {
if p.UID != "" {
return p.UID
}
return fmt.Sprintf("%d", p.ID)
}
// Playlist fetches and returns a Grafana playlist.
func (c *Client) Playlist(idOrUID string) (*Playlist, error) {
path := fmt.Sprintf("/api/playlists/%s", idOrUID)
playlist := &Playlist{}
err := c.request("GET", path, nil, nil, playlist)
if err != nil {
return nil, err
}
return playlist, nil
}
// NewPlaylist creates a new Grafana playlist.
func (c *Client) NewPlaylist(playlist Playlist) (string, error) {
data, err := json.Marshal(playlist)
if err != nil {
return "", err
}
var result Playlist
err = c.request("POST", "/api/playlists", nil, data, &result)
if err != nil {
return "", err
}
return result.QueryID(), nil
}
// UpdatePlaylist updates a Grafana playlist.
func (c *Client) UpdatePlaylist(playlist Playlist) error {
path := fmt.Sprintf("/api/playlists/%s", playlist.QueryID())
data, err := json.Marshal(playlist)
if err != nil {
return err
}
return c.request("PUT", path, nil, data, nil)
}
// DeletePlaylist deletes the Grafana playlist whose ID it's passed.
func (c *Client) DeletePlaylist(idOrUID string) error {
path := fmt.Sprintf("/api/playlists/%s", idOrUID)
return c.request("DELETE", path, nil, nil, nil)
}