-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathapi.go
More file actions
163 lines (129 loc) · 3.43 KB
/
api.go
File metadata and controls
163 lines (129 loc) · 3.43 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/exercism/cli/configuration"
"io/ioutil"
"net/http"
)
const VERSION = "1.3.2"
var FetchEndpoints = map[string]string{
"current": "/api/v1/user/assignments/current",
"next": "/api/v1/user/assignments/next",
"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
}
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("Error fetching assignments. HTTP Status Code: %d", resp.StatusCode)
return
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
err = fmt.Errorf("Error fetching assignments: [%v]", err)
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", fmt.Sprintf("github.com/kytrinyx/exercism CLI v%s", VERSION))
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", fmt.Sprintf("github.com/exercism/cli v%s", VERSION))
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
}