forked from exercism/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
171 lines (137 loc) · 3.65 KB
/
api.go
File metadata and controls
171 lines (137 loc) · 3.65 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/exercism/cli/configuration"
"io/ioutil"
"net/http"
)
const VERSION = "1.4.0"
const USER_AGENT = "github.com/exercism/cli v" + VERSION
var FetchEndpoints = map[string]string{
"current": "/api/v1/user/assignments/current",
"next": "/api/v1/user/assignments/next",
"restore": "/api/v1/user/assignments/restore",
"demo": "/api/v1/assignments/demo",
"exercise": "/api/v1/assignments",
}
type submitResponse struct {
Id string
Status string
Language string
Exercise string
SubmissionPath string `json:"submission_path"`
Error string
}
type submitRequest struct {
Key string `json:"key"`
Code string `json:"code"`
Path string `json:"path"`
}
func FetchAssignments(config configuration.Config, path string) (as []Assignment, err error) {
url := fmt.Sprintf("%s%s?key=%s", config.Hostname, path, config.ApiKey)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
err = fmt.Errorf("Error fetching assignments: [%v]", err)
return
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
err = fmt.Errorf("Error fetching assignments: [%v]", err)
return
}
if resp.StatusCode != http.StatusOK {
var apiError struct { Error string `json:"error"` }
err = json.Unmarshal(body, &apiError)
if err != nil {
err = fmt.Errorf("Error parsing API response: [%v]", err)
return
}
err = fmt.Errorf("Error fetching assignments. HTTP Status Code: %d\n%s", resp.StatusCode, apiError.Error)
return
}
var fr struct {
Assignments []Assignment
}
err = json.Unmarshal(body, &fr)
if err != nil {
err = fmt.Errorf("Error parsing API response: [%v]", err)
return
}
return fr.Assignments, err
}
func UnsubmitAssignment(config configuration.Config) (r string, err error) {
path := "api/v1/user/assignments"
url := fmt.Sprintf("%s/%s?key=%s", config.Hostname, path, config.ApiKey)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return
}
req.Header.Set("User-Agent", USER_AGENT)
resp, err := http.DefaultClient.Do(req)
if err != nil {
err = fmt.Errorf("Error destroying submission: [%v]", err)
return
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return
}
if resp.StatusCode != http.StatusNoContent {
var ur struct {
Error string
}
err = json.Unmarshal(body, &ur)
if err != nil {
return
}
err = fmt.Errorf("Status: %d, Error: %v", resp.StatusCode, ur.Error)
return ur.Error, err
}
return
}
func SubmitAssignment(config configuration.Config, filePath string, code []byte) (r submitResponse, err error) {
path := "api/v1/user/assignments"
url := fmt.Sprintf("%s/%s", config.Hostname, path)
submission := submitRequest{Key: config.ApiKey, Code: string(code), Path: filePath}
submissionJson, err := json.Marshal(submission)
if err != nil {
return
}
req, err := http.NewRequest("POST", url, bytes.NewReader(submissionJson))
if err != nil {
return
}
req.Header.Set("User-Agent", USER_AGENT)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
err = fmt.Errorf("Error posting assignment: [%v]", err)
return
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return
}
if resp.StatusCode != http.StatusCreated {
err = json.Unmarshal(body, &r)
if err != nil {
return
}
err = fmt.Errorf("Status: %d, Error: %v", resp.StatusCode, r)
return
}
err = json.Unmarshal(body, &r)
if err != nil {
err = fmt.Errorf("Error parsing API response: [%v]", err)
}
return
}