-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.go
253 lines (192 loc) · 6.09 KB
/
dashboard.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//go:generate go run tools/withoutctx.go
package redash
import (
"context"
"fmt"
"time"
"github.com/winebarrel/redash-go/v2/internal/util"
)
type DashboardPage struct {
Count int `json:"count"`
Page int `json:"page"`
PageSize int `json:"page_size"`
Results []Dashboard `json:"results"`
}
type Dashboard struct {
CanEdit bool `json:"can_edit"`
CreatedAt time.Time `json:"created_at"`
DashboardFiltersEnabled bool `json:"dashboard_filters_enabled"`
ID int `json:"id"`
IsArchived bool `json:"is_archived"`
IsDraft bool `json:"is_draft"`
IsFavorite bool `json:"is_favorite"`
Layout any `json:"layout"`
Name string `json:"name"`
Slug string `json:"slug"`
Tags []string `json:"tags"`
UpdatedAt time.Time `json:"updated_at"`
User User `json:"user"`
UserID int `json:"user_id"`
Version int `json:"version"`
Widgets []Widget `json:"widgets"`
}
type ListDashboardsInput struct {
OnlyFavorites bool `url:"only_favorites,omitempty"`
Page int `url:"page,omitempty"`
PageSize int `url:"page_size,omitempty"`
Q string `url:"q,omitempty"`
}
func (client *Client) ListDashboards(ctx context.Context, input *ListDashboardsInput) (*DashboardPage, error) {
res, close, err := client.Get(ctx, "api/dashboards", input)
defer close()
if err != nil {
return nil, err
}
page := &DashboardPage{}
if err := util.UnmarshalBody(res, &page); err != nil {
return nil, err
}
return page, nil
}
func (client *Client) GetDashboard(ctx context.Context, id int) (*Dashboard, error) {
res, close, err := client.Get(ctx, fmt.Sprintf("api/dashboards/%d", id), nil)
defer close()
if err != nil {
return nil, err
}
dashboard := &Dashboard{}
if err := util.UnmarshalBody(res, &dashboard); err != nil {
return nil, err
}
return dashboard, nil
}
func (client *Client) CreateFavoriteDashboard(ctx context.Context, id int) error {
_, close, err := client.Post(ctx, fmt.Sprintf("api/dashboards/%d/favorite", id), nil)
defer close()
if err != nil {
return err
}
return nil
}
type CreateDashboardInput struct {
Name string `json:"name"`
}
func (client *Client) CreateDashboard(ctx context.Context, input *CreateDashboardInput) (*Dashboard, error) {
res, close, err := client.Post(ctx, "api/dashboards", input)
defer close()
if err != nil {
return nil, err
}
dashboard := &Dashboard{}
if err := util.UnmarshalBody(res, &dashboard); err != nil {
return nil, err
}
return dashboard, nil
}
type UpdateDashboardInput struct {
DashboardFiltersEnabled bool `json:"dashboard_filters_enabled,omitempty"`
IsArchived bool `json:"is_archived,omitempty"`
IsDraft bool `json:"is_draft,omitempty"`
Layout []any `json:"layout,omitempty"`
Name string `json:"name,omitempty"`
Options any `json:"options,omitempty"`
Tags *[]string `json:"tags,omitempty"`
Version int `json:"version,omitempty"`
}
func (client *Client) UpdateDashboard(ctx context.Context, id int, input *UpdateDashboardInput) (*Dashboard, error) {
res, close, err := client.Post(ctx, fmt.Sprintf("api/dashboards/%d", id), input)
defer close()
if err != nil {
return nil, err
}
dashboard := &Dashboard{}
if err := util.UnmarshalBody(res, &dashboard); err != nil {
return nil, err
}
return dashboard, nil
}
func (client *Client) ArchiveDashboard(ctx context.Context, id int) error {
_, close, err := client.Delete(ctx, fmt.Sprintf("api/dashboards/%d", id))
defer close()
if err != nil {
return err
}
return nil
}
type DashboardTags struct {
Tags []DashboardTagsTag `json:"tags"`
}
type DashboardTagsTag struct {
Count int `json:"count"`
Name string `json:"name"`
}
func (client *Client) GetDashboardTags(ctx context.Context) (*DashboardTags, error) {
res, close, err := client.Get(ctx, "api/dashboards/tags", nil)
defer close()
if err != nil {
return nil, err
}
tags := &DashboardTags{}
if err := util.UnmarshalBody(res, &tags); err != nil {
return nil, err
}
return tags, nil
}
type ListMyDashboardsInput struct {
Page int `url:"page,omitempty"`
PageSize int `url:"page_size,omitempty"`
Q string `url:"q,omitempty"`
}
func (client *Client) ListMyDashboards(ctx context.Context, input *ListMyDashboardsInput) (*DashboardPage, error) {
res, close, err := client.Get(ctx, "api/dashboards/my", input)
defer close()
if err != nil {
return nil, err
}
page := &DashboardPage{}
if err := util.UnmarshalBody(res, &page); err != nil {
return nil, err
}
return page, nil
}
type ListFavoriteDashboardsInput struct {
Page int `url:"page,omitempty"`
PageSize int `url:"page_size,omitempty"`
Q string `url:"q,omitempty"`
}
func (client *Client) ListFavoriteDashboards(ctx context.Context, input *ListFavoriteDashboardsInput) (*DashboardPage, error) {
res, close, err := client.Get(ctx, "api/dashboards/favorites", input)
defer close()
if err != nil {
return nil, err
}
page := &DashboardPage{}
if err := util.UnmarshalBody(res, &page); err != nil {
return nil, err
}
return page, nil
}
type ShareDashboardOutput struct {
APIKey string `json:"api_key"`
PublicURL string `json:"public_url"`
}
func (client *Client) ShareDashboard(ctx context.Context, id int) (*ShareDashboardOutput, error) {
res, close, err := client.Post(ctx, fmt.Sprintf("api/dashboards/%d/share", id), nil)
defer close()
if err != nil {
return nil, err
}
output := &ShareDashboardOutput{}
if err := util.UnmarshalBody(res, &output); err != nil {
return nil, err
}
return output, nil
}
func (client *Client) UnshareDashboard(ctx context.Context, id int) error {
_, close, err := client.Delete(ctx, fmt.Sprintf("api/dashboards/%d/share", id))
defer close()
if err != nil {
return err
}
return nil
}