/
builder_requests.go
213 lines (171 loc) · 4.9 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
package se2
import (
"bytes"
"encoding/json"
"io"
"net/http"
"path"
"github.com/pkg/errors"
)
// BuilderHealth is used to check that the builder is healthy and responding to requests.
func (c *Client) BuilderHealth() (bool, error) {
req, err := c.builderRequestBuilder(http.MethodGet, "/api/v1/health", nil)
if err != nil {
return false, err
}
res, err := c.do(req)
if err != nil {
return false, err
}
defer res.Body.Close()
return true, nil
}
// BuilderFeatures lists the features present on the builder, such as testing capabilities.
func (c *Client) BuilderFeatures() (*FeaturesResponse, error) {
req, err := c.builderRequestBuilder(http.MethodGet, "/api/v1/features", nil)
if err != nil {
return nil, err
}
res, err := c.do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
features := &FeaturesResponse{
Features: []string{},
}
dec := json.NewDecoder(res.Body)
err = dec.Decode(features)
if err != nil {
return nil, err
}
return features, nil
}
// BuilderTemplate gets the plugin template for the provided plugin and template name.
func (c *Client) BuilderTemplate(plugin *Plugin, template string) (*EditorStateResponse, error) {
if plugin == nil {
return nil, errors.New("Plugin cannot be nil")
}
req, err := c.builderRequestBuilder(http.MethodGet,
path.Join("/api/v2/template", template, plugin.Name), nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("namespace", plugin.Namespace)
req.URL.RawQuery = q.Encode()
res, err := c.do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
editorState := &EditorStateResponse{Tests: []TestPayload{}}
dec := json.NewDecoder(res.Body)
err = dec.Decode(editorState)
if err != nil {
return nil, err
}
return editorState, nil
}
// BuildPlugin triggers a remote build for the given plugin and source code. See also: Client.BuildPluginString()
//
// # Example
//
// This plugin is useful for reading from a filesystem or from an http.Response.Body
//
// plugin := se2.NewPlugin("com.suborbital", "acmeco", "default", "hello")
// file, _ := os.Open("hello.rs")
// result, err := client.BuildPlugin(plugin, file)
func (c *Client) BuildPlugin(plugin *Plugin, template string, source io.Reader) (*BuildResult, error) {
if plugin == nil {
return nil, errors.New("Plugin cannot be nil")
}
if source == nil {
return nil, errors.New("source cannot be nil")
}
// TODO: cache somewhere in Client?
token, err := c.EditorToken(plugin)
if err != nil {
return nil, errors.Wrap(err, "failed to EditorToken")
}
req, err := c.builderRequestBuilder(http.MethodPost,
path.Join("/api/v1/build", template, plugin.URI()), source)
req.Header.Add("Authorization", "Bearer "+token)
if err != nil {
return nil, err
}
res, err := c.do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
buildResult := &BuildResult{}
dec := json.NewDecoder(res.Body)
err = dec.Decode(buildResult)
if err != nil {
return nil, err
}
return buildResult, nil
}
// BuildPluginString triggers a remote build for the given plugin and source code. See also: Client.BuildPlugin()
func (c *Client) BuildPluginString(plugin *Plugin, template, source string) (*BuildResult, error) {
buf := bytes.NewBufferString(source)
return c.BuildPlugin(plugin, template, buf)
}
// GetDraft gets the most recently build source code for the provided plugin. Must have the .FQFMURI field set.
func (c *Client) GetDraft(plugin *Plugin) (*EditorStateResponse, error) {
if plugin == nil {
return nil, errors.New("Plugin cannot be nil")
}
token, err := c.EditorToken(plugin)
if err != nil {
return nil, errors.Wrap(err, "failed to EditorToken")
}
req, err := c.builderRequestBuilder(http.MethodGet,
path.Join("/api/v1/draft", plugin.URI()), nil)
req.Header.Add("Authorization", "Bearer "+token)
if err != nil {
return nil, err
}
res, err := c.do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
editorState := &EditorStateResponse{Tests: []TestPayload{}}
dec := json.NewDecoder(res.Body)
err = dec.Decode(editorState)
if err != nil {
return nil, err
}
return editorState, nil
}
// PromoteDraft takes the most recent build of the provided plugin and deploys it so it can be
// run.
func (c *Client) PromoteDraft(plugin *Plugin) (*PromoteDraftResponse, error) {
if plugin == nil {
return nil, errors.New("Plugin cannot be nil")
}
token, err := c.EditorToken(plugin)
if err != nil {
return nil, errors.Wrap(err, "failed to EditorToken")
}
req, err := c.builderRequestBuilder(http.MethodPost,
path.Join("/api/v1/draft", plugin.URI(), "promote"), nil)
req.Header.Add("Authorization", "Bearer "+token)
if err != nil {
return nil, err
}
res, err := c.do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
promoteResponse := &PromoteDraftResponse{}
dec := json.NewDecoder(res.Body)
err = dec.Decode(promoteResponse)
if err != nil {
return nil, err
}
return promoteResponse, nil
}