-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathviews.go
179 lines (135 loc) · 4.33 KB
/
views.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
package rockset
import (
"context"
"net/http"
"github.com/rs/zerolog"
rockerr "github.com/rockset/rockset-go-client/errors"
"github.com/rockset/rockset-go-client/openapi"
"github.com/rockset/rockset-go-client/option"
)
// CreateView creates a new view, with an optional description.
//
// REST API documentation https://docs.rockset.com/rest-api/#createview
func (rc *RockClient) CreateView(ctx context.Context, workspace, view, query string,
options ...option.ViewOption) (openapi.View, error) {
var err error
var httpResp *http.Response
var resp *openapi.CreateViewResponse
q := rc.ViewsApi.CreateView(ctx, workspace)
req := openapi.NewCreateViewRequest(view, query)
opts := option.ViewOptions{}
for _, o := range options {
o(&opts)
}
if opts.Description != nil {
req.Description = opts.Description
}
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Body(*req).Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return openapi.View{}, err
}
log := zerolog.Ctx(ctx)
log.Trace().Str("status", httpResp.Status).Str("state", resp.Data.GetState()).Msg("view created")
return resp.GetData(), nil
}
// UpdateView updates an existing view, with an optional description.
//
// REST API documentation https://docs.rockset.com/rest-api/#updateview
func (rc *RockClient) UpdateView(ctx context.Context, workspace, view, query string,
options ...option.ViewOption) (openapi.View, error) {
var err error
var httpResp *http.Response
var resp *openapi.UpdateViewResponse
q := rc.ViewsApi.UpdateView(ctx, workspace, view)
req := openapi.NewUpdateViewRequest(query)
opts := option.ViewOptions{}
for _, o := range options {
o(&opts)
}
if opts.Description != nil {
req.Description = opts.Description
}
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Body(*req).Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return openapi.View{}, err
}
log := zerolog.Ctx(ctx)
log.Trace().Str("status", httpResp.Status).Msg("view updated")
return resp.GetData(), nil
}
// DeleteView marks the view for deletion, which will take place in the background. Use the
// WaitUntilViewGone() call to block until the view has been deleted.
//
// REST API documentation https://docs.rockset.com/rest-api/#deleteview
func (rc *RockClient) DeleteView(ctx context.Context, workspace, view string) error {
var err error
var httpResp *http.Response
q := rc.ViewsApi.DeleteView(ctx, workspace, view)
err = rc.Retry(ctx, func() error {
_, httpResp, err = q.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return err
}
log := zerolog.Ctx(ctx)
log.Trace().Str("status", httpResp.Status).Msg("view deleted")
return nil
}
// ListViews list views. Use option.WithViewWorkspace() to limit the request to just one workspace.
//
// REST API documentation https://docs.rockset.com/rest-api/#listviews
func (rc *RockClient) ListViews(ctx context.Context, options ...option.ListViewOption) ([]openapi.View, error) {
var err error
var httpResp *http.Response
var resp *openapi.ListViewsResponse
opts := option.ListViewOptions{}
for _, o := range options {
o(&opts)
}
if opts.Workspace == "" {
q := rc.ViewsApi.ListViews(ctx)
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
} else {
q := rc.ViewsApi.WorkspaceViews(ctx, opts.Workspace)
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
}
if err != nil {
return nil, err
}
log := zerolog.Ctx(ctx)
log.Debug().Int("count", len(resp.GetData())).Msg("list views")
return resp.GetData(), nil
}
// GetView gets details about a view.
//
// REST API documentation https://docs.rockset.com/rest-api/#getview
func (rc *RockClient) GetView(ctx context.Context, workspace, name string) (openapi.View, error) {
var err error
var httpResp *http.Response
var resp *openapi.GetViewResponse
log := zerolog.Ctx(ctx)
q := rc.ViewsApi.GetView(ctx, workspace, name)
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return openapi.View{}, err
}
data := resp.GetData()
log.Debug().Str("name", data.GetName()).Msg("get view successful")
return data, nil
}