forked from exercism/cli
/
api.go
294 lines (240 loc) · 7.06 KB
/
api.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
292
293
294
package api
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
)
var (
// ErrUnknownLanguage represents an error returned when the language requested does not exist
ErrUnknownLanguage = errors.New("the language is unknown")
)
// PayloadError represents an error message from the API.
type PayloadError struct {
Error string `json:"error"`
}
// PayloadProblems represents a response containing problems.
type PayloadProblems struct {
Problems []*Problem
PayloadError
}
// PayloadSubmission represents metadata about a successful submission.
type PayloadSubmission struct {
*Submission
PayloadError
}
// SubmissionInfo contains state information about a submission.
type SubmissionInfo struct {
Slug string `json:"slug"`
State string `json:"state"`
}
// Fetch retrieves problems from the API.
// Most problems consist of a README, some sort of test suite, and
// any supporting files (header files, test data, boilerplate, skeleton
// files, etc).
func (c *Client) Fetch(args []string) ([]*Problem, error) {
var url string
switch len(args) {
case 0:
url = fmt.Sprintf("%s/v2/exercises?key=%s", c.XAPIHost, c.APIKey)
case 1:
language := args[0]
url = fmt.Sprintf("%s/v2/exercises/%s?key=%s", c.XAPIHost, language, c.APIKey)
case 2:
language := args[0]
problem := args[1]
url = fmt.Sprintf("%s/v2/exercises/%s/%s", c.XAPIHost, language, problem)
default:
return nil, fmt.Errorf("Usage: exercism fetch\n or: exercism fetch LANGUAGE\n or: exercism fetch LANGUAGE PROBLEM")
}
req, err := c.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
payload := &PayloadProblems{}
res, err := c.Do(req, payload)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unable to fetch problems (HTTP: %d) - %s", res.StatusCode, payload.Error)
}
return payload.Problems, nil
}
// Restore fetches the latest revision of a solution and writes it to disk.
func (c *Client) Restore() ([]*Problem, error) {
url := fmt.Sprintf("%s/v2/exercises/restore?key=%s", c.XAPIHost, c.APIKey)
req, err := c.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
payload := &PayloadProblems{}
res, err := c.Do(req, payload)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unable to fetch problems (HTTP: %d) - %s", res.StatusCode, payload.Error)
}
return payload.Problems, nil
}
// Submissions gets a list of submitted exercises and their current acceptance state.
func (c *Client) Submissions() (map[string][]SubmissionInfo, error) {
url := fmt.Sprintf("%s/api/v1/exercises?key=%s", c.APIHost, c.APIKey)
req, err := c.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
var payload map[string][]SubmissionInfo
if _, err := c.Do(req, &payload); err != nil {
return nil, err
}
return payload, nil
}
// Submission get the latest submitted exercise for the given language and exercise
func (c *Client) Submission(language, excercise string) (*Submission, error) {
url := fmt.Sprintf("%s/api/v1/submissions/%s/%s?key=%s", c.APIHost, language, excercise, c.APIKey)
req, err := c.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
var payload Submission
if _, err := c.Do(req, &payload); err != nil {
return nil, err
}
return &payload, nil
}
// Download fetches a solution by submission key and writes it to disk.
func (c *Client) Download(submissionID string) (*Submission, error) {
url := fmt.Sprintf("%s/api/v1/submissions/%s", c.APIHost, submissionID)
req, err := c.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
payload := &PayloadSubmission{}
res, err := c.Do(req, payload)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unable to fetch Submission (HTTP: %d) - %s", res.StatusCode, payload.Error)
}
return payload.Submission, err
}
// Demo fetches the first problem in each language track.
func (c *Client) Demo() ([]*Problem, error) {
url := fmt.Sprintf("%s/problems/demo?key=%s", c.XAPIHost, c.APIKey)
req, err := c.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
payload := &PayloadProblems{}
res, err := c.Do(req, payload)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unable to fetch problems (HTTP: %d) - %s", res.StatusCode, payload.Error)
}
return payload.Problems, nil
}
// Submit posts code to the API
func (c *Client) Submit(iter *Iteration) (*Submission, error) {
url := fmt.Sprintf("%s/api/v1/user/assignments", c.APIHost)
payload, err := json.Marshal(iter)
if err != nil {
return nil, err
}
req, err := c.NewRequest("POST", url, bytes.NewReader(payload))
if err != nil {
return nil, err
}
ps := &PayloadSubmission{}
res, err := c.Do(req, ps)
if err != nil {
return nil, fmt.Errorf("unable to submit solution - %s", err)
}
if res.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("unable to submit (HTTP: %d) - %s", res.StatusCode, ps.Error)
}
return ps.Submission, nil
}
// List available problems for a language
func (c *Client) List(language string) ([]string, error) {
url := fmt.Sprintf("%s/tracks/%s", c.XAPIHost, language)
req, err := c.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
res, err := c.Do(req, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, ErrUnknownLanguage
}
var payload struct {
Track Track
}
if err := json.NewDecoder(res.Body).Decode(&payload); err != nil {
return nil, err
}
problems := make([]string, len(payload.Track.Problems))
prefix := language + "/"
for n, p := range payload.Track.Problems {
problems[n] = strings.TrimPrefix(p, prefix)
}
return problems, nil
}
// Unsubmit deletes a submission.
func (c *Client) Unsubmit() error {
url := fmt.Sprintf("%s/api/v1/user/assignments?key=%s", c.APIHost, c.APIKey)
req, err := c.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
pe := &PayloadError{}
if _, err := c.Do(req, pe); err != nil {
return fmt.Errorf("failed to unsubmit - %s", pe.Error)
}
return nil
}
// Tracks gets the current list of active and inactive language tracks.
func (c *Client) Tracks() ([]*Track, error) {
url := fmt.Sprintf("%s/tracks", c.XAPIHost)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return []*Track{}, err
}
var payload struct {
Tracks []*Track
}
if _, err := c.Do(req, &payload); err != nil {
return []*Track{}, err
}
return payload.Tracks, nil
}
// Skip sends a request to exercism to skip the exercise given language and slug
func (c *Client) Skip(language, slug string) error {
url := fmt.Sprintf("%s/api/v1/iterations/%s/%s/skip?key=%s", c.APIHost, language, slug, c.APIKey)
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
res, err := c.Do(req, nil)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode == http.StatusNoContent {
return nil
}
var pe PayloadError
if err := json.NewDecoder(res.Body).Decode(&pe); err != nil {
return err
}
return errors.New(pe.Error)
}