-
Notifications
You must be signed in to change notification settings - Fork 9
/
api_keys.go
188 lines (144 loc) · 4.21 KB
/
api_keys.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
package rockset
import (
"context"
"net/http"
rockerr "github.com/rockset/rockset-go-client/errors"
"github.com/rockset/rockset-go-client/openapi"
"github.com/rockset/rockset-go-client/option"
)
// CreateAPIKey creates a new API key for the current user with the specified, with an optional role.
//
// REST API documentation https://docs.rockset.com/rest-api/#createapikey
func (rc *RockClient) CreateAPIKey(ctx context.Context, keyName string,
options ...option.APIKeyRoleOption) (openapi.ApiKey, error) {
var err error
var httpResp *http.Response
var resp *openapi.CreateApiKeyResponse
createReq := rc.APIKeysApi.CreateApiKey(ctx)
b := openapi.NewCreateApiKeyRequest(keyName)
var opts option.APIKeyRoleOptions
for _, o := range options {
o(&opts)
}
if opts.Role != nil {
b.Role = opts.Role
}
err = rc.Retry(ctx, func() error {
resp, httpResp, err = createReq.Body(*b).Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return openapi.ApiKey{}, err
}
return resp.GetData(), nil
}
const self = "self"
// GetAPIKey gets the named API key.
// An admin can get an api key for another user with option.ForUser().
//
// REST API documentation https://docs.rockset.com/rest-api/#getapikey
func (rc *RockClient) GetAPIKey(ctx context.Context, name string,
options ...option.APIKeyOption) (openapi.ApiKey, error) {
var err error
var httpResp *http.Response
var resp *openapi.GetApiKeyResponse
opts := option.APIKeyOptions{}
for _, o := range options {
o(&opts)
}
user := self
if opts.User != nil {
user = *opts.User
}
getReq := rc.APIKeysApi.GetApiKey(ctx, user, name)
err = rc.Retry(ctx, func() error {
resp, httpResp, err = getReq.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return openapi.ApiKey{}, err
}
return resp.GetData(), nil
}
// DeleteAPIKey deletes an API key.
// An admin can delete an api key for another user with option.ForUser().
//
// REST API documentation https://docs.rockset.com/rest-api/#deleteapikey
func (rc *RockClient) DeleteAPIKey(ctx context.Context, keyName string, options ...option.APIKeyOption) error {
var err error
var httpResp *http.Response
opts := option.APIKeyOptions{}
for _, o := range options {
o(&opts)
}
user := self
if opts.User != nil {
user = *opts.User
}
getReq := rc.APIKeysApi.DeleteApiKey(ctx, keyName, user)
err = rc.Retry(ctx, func() error {
_, httpResp, err = getReq.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return err
}
return nil
}
// ListAPIKeys list API keys.
// An admin can list api keys for another user with option.ForUser().
//
// REST API documentation https://docs.rockset.com/rest-api/#listapikey
func (rc *RockClient) ListAPIKeys(ctx context.Context, options ...option.APIKeyOption) ([]openapi.ApiKey, error) {
var err error
var httpResp *http.Response
var resp *openapi.ListApiKeysResponse
opts := option.APIKeyOptions{}
for _, o := range options {
o(&opts)
}
user := self
if opts.User != nil {
user = *opts.User
}
getReq := rc.APIKeysApi.ListApiKeys(ctx, user)
err = rc.Retry(ctx, func() error {
resp, httpResp, err = getReq.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return nil, err
}
return resp.GetData(), nil
}
// UpdateAPIKey updates the state of an API key.
// An admin can update an api key for another user with option.ForUser().
//
// REST API documentation https://rockset.com/docs/rest-api/#updateapikey
func (rc *RockClient) UpdateAPIKey(ctx context.Context, keyName string,
options ...option.APIKeyOption) (openapi.ApiKey, error) {
var err error
var httpResp *http.Response
var resp *openapi.UpdateApiKeyResponse
opts := option.APIKeyOptions{}
for _, o := range options {
o(&opts)
}
user := self
if opts.User != nil {
user = *opts.User
}
updateReq := rc.APIKeysApi.UpdateApiKey(ctx, keyName, user)
if opts.State != nil {
state := string(*opts.State)
updateReq = updateReq.Body(openapi.UpdateApiKeyRequest{State: &state})
}
err = rc.Retry(ctx, func() error {
resp, httpResp, err = updateReq.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return openapi.ApiKey{}, err
}
return resp.GetData(), nil
}