/
api.go
304 lines (248 loc) · 7.19 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
295
296
297
298
299
300
301
302
303
304
package api
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
)
var (
// ErrUnknownTrack represents an error returned when the track requested does not exist.
ErrUnknownTrack = errors.New("no such track")
)
// 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:
url = fmt.Sprintf("%s/v2/exercises/%s?key=%s", c.XAPIHost, args[0], c.APIKey)
case 2:
url = fmt.Sprintf("%s/v2/exercises/%s/%s", c.XAPIHost, args[0], args[1])
if c.APIKey != "" {
url = fmt.Sprintf("%s?key=%s", url, c.APIKey)
}
default:
return nil, fmt.Errorf("Usage: exercism fetch\n or: exercism fetch TRACK_ID\n or: exercism fetch TRACK_ID 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
}
// FetchAll retrieves all problems for a given language track from the API
func (c *Client) FetchAll(trackID string) ([]*Problem, error) {
list, err := c.List(trackID)
if err != nil {
return nil, err
}
problems := make([]*Problem, len(list))
for i, prob := range list {
p, err := c.Fetch([]string{trackID, prob})
if err != nil {
return nil, err
}
problems[i] = p[0]
}
return 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 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
}
// SubmissionURL gets the url of the latest iteration on the given language track id and problem slug.
func (c *Client) SubmissionURL(trackID, slug string) (*Submission, error) {
url := fmt.Sprintf("%s/api/v1/submissions/%s/%s?key=%s", c.APIHost, trackID, slug, 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
}
// Submit posts an iteration 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 track.
func (c *Client) List(trackID string) ([]string, error) {
url := fmt.Sprintf("%s/tracks/%s", c.XAPIHost, trackID)
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, ErrUnknownTrack
}
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 := trackID + "/"
for n, p := range payload.Track.Problems {
problems[n] = strings.TrimPrefix(p, prefix)
}
return problems, 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 marks the exercise in the given language track as skipped.
func (c *Client) Skip(trackID, slug string) error {
url := fmt.Sprintf("%s/api/v1/iterations/%s/%s/skip?key=%s", c.APIHost, trackID, 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)
}
// Status sends a request to exercism to fetch the user's
// completion status for the given language track.
func (c *Client) Status(trackID string) (*StatusInfo, error) {
url := fmt.Sprintf("%s/api/v1/tracks/%s/status?key=%s", c.APIHost, trackID, c.APIKey)
req, err := http.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.StatusNotFound {
return nil, ErrUnknownTrack
}
var si StatusInfo
if err := json.NewDecoder(res.Body).Decode(&si); err != nil {
return nil, err
}
return &si, nil
}