-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_snippet.go
111 lines (83 loc) · 2.51 KB
/
query_snippet.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
//go:generate go run tools/withoutctx.go
package redash
import (
"context"
"fmt"
"time"
"github.com/winebarrel/redash-go/v2/internal/util"
)
type QuerySnippet struct {
CreatedAt time.Time `json:"created_at"`
Description string `json:"description"`
ID int `json:"id"`
Snippet string `json:"snippet"`
Trigger string `json:"trigger"`
UpdatedAt time.Time `json:"updated_at"`
User User `json:"user"`
}
func (client *Client) ListQuerySnippets(ctx context.Context) ([]QuerySnippet, error) {
res, close, err := client.Get(ctx, "api/query_snippets", nil)
defer close()
if err != nil {
return nil, err
}
querySnippets := []QuerySnippet{}
if err := util.UnmarshalBody(res, &querySnippets); err != nil {
return nil, err
}
return querySnippets, nil
}
func (client *Client) GetQuerySnippet(ctx context.Context, id int) (*QuerySnippet, error) {
res, close, err := client.Get(ctx, fmt.Sprintf("api/query_snippets/%d", id), nil)
defer close()
if err != nil {
return nil, err
}
querySnippet := &QuerySnippet{}
if err := util.UnmarshalBody(res, &querySnippet); err != nil {
return nil, err
}
return querySnippet, nil
}
type CreateQuerySnippetInput struct {
Description string `json:"description"`
Snippet string `json:"snippet"`
Trigger string `json:"trigger"`
}
func (client *Client) CreateQuerySnippet(ctx context.Context, input *CreateQuerySnippetInput) (*QuerySnippet, error) {
res, close, err := client.Post(ctx, "api/query_snippets", input)
defer close()
if err != nil {
return nil, err
}
querySnippet := &QuerySnippet{}
if err := util.UnmarshalBody(res, &querySnippet); err != nil {
return nil, err
}
return querySnippet, nil
}
type UpdateQuerySnippetInput struct {
Description string `json:"description,omitempty"`
Snippet string `json:"snippet,omitempty"`
Trigger string `json:"trigger,omitempty"`
}
func (client *Client) UpdateQuerySnippet(ctx context.Context, id int, input *UpdateQuerySnippetInput) (*QuerySnippet, error) {
res, close, err := client.Post(ctx, fmt.Sprintf("api/query_snippets/%d", id), input)
defer close()
if err != nil {
return nil, err
}
querySnippet := &QuerySnippet{}
if err := util.UnmarshalBody(res, &querySnippet); err != nil {
return nil, err
}
return querySnippet, nil
}
func (client *Client) DeleteQuerySnippet(ctx context.Context, id int) error {
_, close, err := client.Delete(ctx, fmt.Sprintf("api/query_snippets/%d", id))
defer close()
if err != nil {
return err
}
return nil
}