-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source.go
212 lines (159 loc) · 5.33 KB
/
data_source.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
//go:generate go run tools/withoutctx.go
package redash
import (
"context"
"fmt"
"github.com/winebarrel/redash-go/v2/internal/util"
)
type DataSource struct {
Groups map[int]bool `json:"groups"`
ID int `json:"id"`
Name string `json:"name"`
Options map[string]any `json:"options"`
Paused int `json:"paused"`
PauseReason string `json:"pause_reason"`
QueueName string `json:"queue_name"`
ScheduledQueueName string `json:"scheduled_queue_name"`
Syntax string `json:"syntax"`
Type string `json:"type"`
ViewOnly bool `json:"view_only"`
}
func (client *Client) ListDataSources(ctx context.Context) ([]DataSource, error) {
res, close, err := client.Get(ctx, "api/data_sources", nil)
defer close()
if err != nil {
return nil, err
}
dataSources := []DataSource{}
if err := util.UnmarshalBody(res, &dataSources); err != nil {
return nil, err
}
return dataSources, nil
}
func (client *Client) GetDataSource(ctx context.Context, id int) (*DataSource, error) {
res, close, err := client.Get(ctx, fmt.Sprintf("api/data_sources/%d", id), nil)
defer close()
if err != nil {
return nil, err
}
dataSource := &DataSource{}
if err := util.UnmarshalBody(res, &dataSource); err != nil {
return nil, fmt.Errorf("Unmarshal response body failed: %w", err)
}
return dataSource, nil
}
type CreateDataSourceInput struct {
Name string `json:"name"`
Options map[string]any `json:"options"`
Type string `json:"type"`
}
func (client *Client) CreateDataSource(ctx context.Context, input *CreateDataSourceInput) (*DataSource, error) {
res, close, err := client.Post(ctx, "api/data_sources", input)
defer close()
if err != nil {
return nil, err
}
dataSource := &DataSource{}
if err := util.UnmarshalBody(res, &dataSource); err != nil {
return nil, err
}
return dataSource, nil
}
type UpdateDataSourceInput struct {
Name string `json:"name"`
Options map[string]any `json:"options"`
Type string `json:"type"`
}
func (client *Client) UpdateDataSource(ctx context.Context, id int, input *UpdateDataSourceInput) (*DataSource, error) {
res, close, err := client.Post(ctx, fmt.Sprintf("api/data_sources/%d", id), input)
defer close()
if err != nil {
return nil, err
}
dataSource := &DataSource{}
if err := util.UnmarshalBody(res, &dataSource); err != nil {
return nil, err
}
return dataSource, nil
}
func (client *Client) DeleteDataSource(ctx context.Context, id int) error {
_, close, err := client.Delete(ctx, fmt.Sprintf("api/data_sources/%d", id))
defer close()
if err != nil {
return err
}
return nil
}
type PauseDataSourceInput struct {
Reason string `json:"reason,omitempty"`
}
func (client *Client) PauseDataSource(ctx context.Context, id int, input *PauseDataSourceInput) (*DataSource, error) {
res, close, err := client.Post(ctx, fmt.Sprintf("api/data_sources/%d/pause", id), input)
defer close()
if err != nil {
return nil, err
}
dataSource := &DataSource{}
if err := util.UnmarshalBody(res, &dataSource); err != nil {
return nil, err
}
return dataSource, nil
}
func (client *Client) ResumeDataSource(ctx context.Context, id int) (*DataSource, error) {
res, close, err := client.Delete(ctx, fmt.Sprintf("api/data_sources/%d/pause", id))
defer close()
if err != nil {
return nil, err
}
dataSource := &DataSource{}
if err := util.UnmarshalBody(res, &dataSource); err != nil {
return nil, err
}
return dataSource, nil
}
type TestDataSourceOutput struct {
Message string `json:"message"`
Ok bool `json:"ok"`
}
func (client *Client) TestDataSource(ctx context.Context, id int) (*TestDataSourceOutput, error) {
res, close, err := client.Post(ctx, fmt.Sprintf("api/data_sources/%d/test", id), nil)
defer close()
if err != nil {
return nil, err
}
output := &TestDataSourceOutput{}
if err := util.UnmarshalBody(res, &output); err != nil {
return nil, err
}
return output, nil
}
type DataSourceType struct {
ConfigurationSchema DataSourceTypeConfigurationSchema `json:"configuration_schema"`
Name string `json:"name"`
Type string `json:"type"`
}
type DataSourceTypeConfigurationSchema struct {
ExtraOptions []string `json:"extra_options"`
Order []string `json:"order"`
Properties map[string]DataSourceTypeConfigurationSchemaProperty `json:"properties"`
Required []string `json:"required"`
Secret []string `json:"secret"`
Type string `json:"type"`
}
type DataSourceTypeConfigurationSchemaProperty struct {
Default any `json:"default"`
Title string `json:"title"`
Type string `json:"type"`
}
func (client *Client) GetDataSourceTypes(ctx context.Context) ([]DataSourceType, error) {
res, close, err := client.Get(ctx, "api/data_sources/types", nil)
defer close()
if err != nil {
return nil, err
}
types := []DataSourceType{}
if err := util.UnmarshalBody(res, &types); err != nil {
return nil, err
}
return types, nil
}