/
builder_requests.go
291 lines (227 loc) · 9.02 KB
/
builder_requests.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package se2
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/pkg/errors"
)
const (
pathBuilderFeatures = "/api/v1/features"
pathBuilderPrefix = "/builder/v1"
pathDraft = pathBuilderPrefix + "/draft"
pathBuild = pathDraft + "/build"
pathTest = pathDraft + "/test"
pathPromote = pathDraft + "/deploy"
)
// BuildPluginResponse captures the json body into a struct for a build attempt.
type BuildPluginResponse struct {
Succeeded bool `json:"succeeded"`
OutputLog string `json:"outputLog"`
}
// BuildPlugin will attempt to build a plugin supplied by the raw byte slice in the context of the current session. The
// language is set by the template, which you can control by calling the CreatePluginDraft method with the template
// name.
func (c *Client) BuildPlugin(ctx context.Context, pluginCode []byte, token CreateSessionResponse) (BuildPluginResponse, error) {
if len(pluginCode) == zeroLength {
return BuildPluginResponse{}, errors.New("client.BuildPlugin: can not build empty code")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.host+pathBuild, bytes.NewReader(pluginCode))
if err != nil {
return BuildPluginResponse{}, errors.Wrap(err, "client.BuildPlugin: http.NewRequest")
}
res, err := c.sessionDo(req, token)
if err != nil {
return BuildPluginResponse{}, errors.Wrap(err, "client.BuildPlugin: c.sessionDo")
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusCreated {
return BuildPluginResponse{}, fmt.Errorf(httpResponseCodeErrorFormat, "client.BuildPlugin", http.StatusCreated, res.StatusCode)
}
var t BuildPluginResponse
dec := json.NewDecoder(res.Body)
dec.DisallowUnknownFields()
err = dec.Decode(&t)
if err != nil {
return BuildPluginResponse{}, errors.Wrap(err, "client.BuildPlugin: dec.Decode")
}
return t, nil
}
// BuilderFeaturesResponse captures the json response from the features endpoint.
type BuilderFeaturesResponse struct {
Features []string `json:"features"`
Languages []Languages
}
// Languages captures the json representation of an individual supported language.
type Languages struct {
ID string `json:"identifier"`
ShortName string `json:"short"`
PrettyName string `json:"pretty"`
}
// GetBuilderFeatures will return the features that the builder can provide.
func (c *Client) GetBuilderFeatures(ctx context.Context) (BuilderFeaturesResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.host+pathBuilderFeatures, nil)
if err != nil {
return BuilderFeaturesResponse{}, errors.Wrap(err, "client.GetBuilderFeatures: http.NewRequest")
}
res, err := c.do(req)
if err != nil {
return BuilderFeaturesResponse{}, errors.Wrap(err, "client.GetBuilderFeatures: c.do")
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
return BuilderFeaturesResponse{}, fmt.Errorf(httpResponseCodeErrorFormat, "client.GetBuilderFeatures", http.StatusOK, res.StatusCode)
}
// Marshal response body into what we need to give back.
var t BuilderFeaturesResponse
dec := json.NewDecoder(res.Body)
dec.DisallowUnknownFields()
err = dec.Decode(&t)
if err != nil {
return BuilderFeaturesResponse{}, errors.Wrap(err, "client.GetBuilderFeatures: dec.Decode")
}
return t, nil
}
// runError captures the structure that the Error in the test draft response can take.
type runError struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
// TestPluginDraftResponse is the response of the test call with the given input data.
type TestPluginDraftResponse struct {
Result string `json:"result"`
Error runError `json:"error"`
}
// TestPluginDraft will send the testData byte slice to the plugin that's currently in the draft as input, and return
// the response that came back from the plugin.
func (c *Client) TestPluginDraft(ctx context.Context, testData []byte, token CreateSessionResponse) (TestPluginDraftResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.host+pathTest, bytes.NewReader(testData))
if err != nil {
return TestPluginDraftResponse{}, errors.Wrap(err, "client.TestPluginDraft: http.NewRequest")
}
res, err := c.sessionDo(req, token)
if err != nil {
return TestPluginDraftResponse{}, errors.Wrap(err, "client.TestPluginDraft: c.sessionDo")
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
return TestPluginDraftResponse{}, fmt.Errorf(httpResponseCodeErrorFormat, "client.TestPluginDraft", http.StatusOK, res.StatusCode)
}
var t TestPluginDraftResponse
dec := json.NewDecoder(res.Body)
dec.DisallowUnknownFields()
err = dec.Decode(&t)
if err != nil {
return TestPluginDraftResponse{}, errors.Wrap(err, "client.TestPluginDraft: dec.Decode")
}
return t, nil
}
// GetPluginDraft returns the currently set plugin draft for the given session token. To change the draft or the
// language you can use the CreatePluginDraft method instead with the name of a template.
func (c *Client) GetPluginDraft(ctx context.Context, token CreateSessionResponse) (DraftResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.host+pathDraft, nil)
if err != nil {
return DraftResponse{}, errors.Wrap(err, "client.GetPluginDraft: http.NewRequest")
}
res, err := c.sessionDo(req, token)
if err != nil {
return DraftResponse{}, errors.Wrap(err, "client.GetPluginDraft: c.sessionDo")
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
return DraftResponse{}, fmt.Errorf(httpResponseCodeErrorFormat, "client.GetPluginDraft", http.StatusOK, res.StatusCode)
}
var t DraftResponse
dec := json.NewDecoder(res.Body)
dec.DisallowUnknownFields()
err = dec.Decode(&t)
if err != nil {
return DraftResponse{}, errors.Wrap(err, "client.GetPluginDraft: dec.Decode")
}
return t, nil
}
// DraftResponse is a struct the captures the response from the CreatePluginDraft and GetDraft endpoints.
type DraftResponse struct {
Lang string `json:"lang"`
Contents string `json:"contents"`
}
// createDraftRequest is a helper struct to encode an incoming template name into a correct json structure we can send
// to the API. Users of this client library should not need to interact with this struct directly.
type createDraftRequest struct {
Template string `json:"template"`
}
// CreatePluginDraft takes in a template name and a session token, and will set the current session to use the named
// template for building and executing.
//
// To see available templates, use the ListTemplates method.
func (c *Client) CreatePluginDraft(ctx context.Context, templateName string, token CreateSessionResponse) (DraftResponse, error) {
if templateName == emptyString {
return DraftResponse{}, errors.New("client.CreatePluginDraft: template name cannot be blank")
}
var b bytes.Buffer
r := createDraftRequest{Template: templateName}
err := json.NewEncoder(&b).Encode(r)
if err != nil {
return DraftResponse{}, errors.Wrapf(err, "client.CreatePluginDraft: json.NewEncoder.Encode(createDraftRequest with template name '%s'", templateName)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.host+pathDraft, &b)
if err != nil {
return DraftResponse{}, errors.Wrap(err, "client.CreatePluginDraft: http.NewRequest")
}
res, err := c.sessionDo(req, token)
if err != nil {
return DraftResponse{}, errors.Wrap(err, "client.CreatePluginDraft: c.sessionDo")
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
return DraftResponse{}, fmt.Errorf(httpResponseCodeErrorFormat, "client.CreatePluginDraft", http.StatusOK, res.StatusCode)
}
var t DraftResponse
dec := json.NewDecoder(res.Body)
dec.DisallowUnknownFields()
err = dec.Decode(&t)
if err != nil {
return DraftResponse{}, errors.Wrap(err, "client.CreatePluginDraft: dec.Decode")
}
return t, nil
}
// PromotePluginDraftResponse captures the json returned by a successful call to the promote endpoint.
type PromotePluginDraftResponse struct {
Ref string `json:"ref"`
}
// PromotePluginDraft promotes the current version of the draft to the live version of the plugin.
func (c *Client) PromotePluginDraft(ctx context.Context, token CreateSessionResponse) (PromotePluginDraftResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.host+pathPromote, nil)
if err != nil {
return PromotePluginDraftResponse{}, errors.Wrap(err, "client.PromotePluginDraft: http.NewRequest")
}
res, err := c.sessionDo(req, token)
if err != nil {
return PromotePluginDraftResponse{}, errors.Wrap(err, "client.PromotePluginDraft: c.sessionDo")
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
return PromotePluginDraftResponse{}, fmt.Errorf(httpResponseCodeErrorFormat, "client.PromotePluginDraft", http.StatusOK, res.StatusCode)
}
var t PromotePluginDraftResponse
dec := json.NewDecoder(res.Body)
dec.DisallowUnknownFields()
err = dec.Decode(&t)
if err != nil {
return PromotePluginDraftResponse{}, errors.Wrap(err, "client.PromotePluginDraft: dec.Decode")
}
return t, nil
}