-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.go
62 lines (49 loc) · 1.43 KB
/
widget.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
//go:generate go run tools/withoutctx.go
package redash
import (
"context"
"fmt"
"time"
"github.com/winebarrel/redash-go/v2/internal/util"
)
type Widget struct {
CreatedAt time.Time `json:"created_at"`
DashboardID int `json:"dashboard_id"`
ID int `json:"id"`
Options map[string]any `json:"options"`
Text string `json:"text"`
UpdatedAt time.Time `json:"updated_at"`
Visualization *Visualization `json:"visualization"`
Width int `json:"width"`
}
type CreateWidgetInput struct {
DashboardID int `json:"dashboard_id"`
Options map[string]any `json:"options"`
Text string `json:"text,omitempty"`
VisualizationID int `json:"visualization_id"`
Width int `json:"width"`
}
func (client *Client) CreateWidget(ctx context.Context, input *CreateWidgetInput) (*Widget, error) {
// workaround
if input.Width < 1 {
input.Width = 1
}
res, close, err := client.Post(ctx, "api/widgets", input)
defer close()
if err != nil {
return nil, err
}
widget := &Widget{}
if err := util.UnmarshalBody(res, &widget); err != nil {
return nil, err
}
return widget, nil
}
func (client *Client) DeleteWidget(ctx context.Context, id int) error {
_, close, err := client.Delete(ctx, fmt.Sprintf("api/widgets/%d", id))
defer close()
if err != nil {
return err
}
return nil
}