forked from sequelize/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
217 lines (179 loc) · 4.97 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
package api
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"github.com/exercism/cli/config"
)
var (
// UserAgent lets the API know where the call is being made from.
// It's set from main() so that we have access to the version.
UserAgent string
UnknownLanguageError = 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
}
// Fetch retrieves problems from the API.
// In most cases these problems consist of a test suite and a README
// from the x-api, but it is also used when restoring earlier iterations.
func Fetch(url string) ([]*Problem, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
payload := &PayloadProblems{}
dec := json.NewDecoder(res.Body)
if err := dec.Decode(payload); err != nil {
return nil, fmt.Errorf("error parsing API response - %s", 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
}
// Download fetches a solution by submission key and writes it to disk.
func Download(url string) (*Submission, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
payload := &PayloadSubmission{}
dec := json.NewDecoder(res.Body)
err = dec.Decode(payload)
if err != nil {
return nil, fmt.Errorf("error parsing API response - %s", 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 Demo(c *config.Config) ([]*Problem, error) {
url := fmt.Sprintf("%s/problems/demo?key=%s", c.XAPI, c.APIKey)
return Fetch(url)
}
// Submit posts code to the API
func Submit(url string, iter *Iteration) (*Submission, error) {
payload, err := json.Marshal(iter)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewReader(payload))
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", UserAgent)
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("unable to submit solution - %s", err)
}
defer res.Body.Close()
ps := &PayloadSubmission{}
dec := json.NewDecoder(res.Body)
if err := dec.Decode(ps); err != nil {
return nil, fmt.Errorf("error parsing API response - %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 List(language, host string) ([]string, error) {
url := fmt.Sprintf("%s/tracks/%s", host, language)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", UserAgent)
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, UnknownLanguageError
}
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 Unsubmit(url string) error {
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
req.Header.Set("User-Agent", UserAgent)
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode == http.StatusNoContent {
return nil
}
pe := &PayloadError{}
if err := json.NewDecoder(res.Body).Decode(pe); err != nil {
return fmt.Errorf("failed to unsubmit - %s", err)
}
return fmt.Errorf("failed to unsubmit - %s", pe.Error)
}
// Tracks gets the current list of active and inactive language tracks.
func Tracks(url string) ([]*Track, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return []*Track{}, err
}
req.Header.Set("User-Agent", UserAgent)
res, err := http.DefaultClient.Do(req)
if err != nil {
return []*Track{}, err
}
defer res.Body.Close()
var payload struct {
Tracks []*Track
}
dec := json.NewDecoder(res.Body)
err = dec.Decode(&payload)
if err != nil {
return []*Track{}, err
}
return payload.Tracks, nil
}