/
codewars.go
178 lines (149 loc) · 3.88 KB
/
codewars.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
// Package codewars implements the client for Codewars API.
//
// As a simple example:
//
// token := "some-api-token"
// cw := codewars.NewClient(token)
//
// slug := "printing-array-elements-with-comma-delimiters"
// kata, _, err := cw.Katas.GetKata(slug)
// if err != nil {
// fmt.Printf("Error: %v\n\n", err)
// } else {
// kataJSON, _ := json.MarshalIndent(kata, "", " ")
// fmt.Printf("Kata:\n%s\n\n", kataJSON)
// }
package codewars
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/google/go-querystring/query"
)
const (
libraryVersion = "0.0.0"
defaultBaseURL = "https://www.codewars.com/api/v1/"
userAgent = "go-codewars/" + libraryVersion
)
// Client represents a Codewars API client.
type Client struct {
// HTTP client used to communicate with the API.
client *http.Client
// Base URL for API requests.
baseURL *url.URL
// Token used to make authenticated API calls.
token string
// User agent used when communicating with the Codewars API.
UserAgent string
// Services used for talking to different parts of the Codewars API.
Users *UsersService
Katas *KatasService
Deferred *DeferredService
}
// NewClient returns a new Codewars API client.
func NewClient(token string) *Client {
u, err := url.Parse(defaultBaseURL)
if err != nil {
panic(err)
}
c := &Client{
client: http.DefaultClient,
baseURL: u,
token: token,
UserAgent: userAgent,
}
c.Users = &UsersService{client: c}
c.Katas = &KatasService{client: c}
c.Deferred = &DeferredService{client: c}
return c
}
// NewRequest returns an API request.
func (c *Client) NewRequest(method, path string, opts interface{}) (*http.Request, error) {
u := *c.baseURL
u.Opaque = c.baseURL.Path + path
req := &http.Request{
Method: method,
URL: &u,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: make(http.Header),
Host: u.Host,
}
if method == "POST" {
data, err := query.Values(opts)
if err != nil {
return nil, err
}
encodedData := data.Encode()
bodyReader := strings.NewReader(encodedData)
req.Body = ioutil.NopCloser(bodyReader)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Length", strconv.Itoa(len(encodedData)))
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", c.token)
req.Header.Set("User-Agent", c.UserAgent)
return req, nil
}
// Do executes the HTTP request and returns the response.
func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
err = CheckResponse(res)
if err != nil {
return res, err
}
if v != nil {
if w, ok := v.(io.Writer); ok {
_, err = io.Copy(w, res.Body)
} else {
err = json.NewDecoder(res.Body).Decode(v)
}
}
return res, err
}
// ErrorResponse represents a Codewars API error.
type ErrorResponse struct {
Response *http.Response
Success bool `json:"success"`
Reason string `json:"reason"`
}
func (er *ErrorResponse) Error() string {
path, _ := url.QueryUnescape(er.Response.Request.URL.Opaque)
requestURL := fmt.Sprintf(
"%s://%s%s",
er.Response.Request.URL.Scheme,
er.Response.Request.URL.Host,
path,
)
return fmt.Sprintf(
"%v %s : %d %s",
er.Response.Request.Method,
requestURL,
er.Response.StatusCode,
er.Reason,
)
}
// CheckResponse checks the Codewars API response for errors and returns them
// if present. A response is considered an error if it has a status code outside
// the 200 range.
func CheckResponse(res *http.Response) error {
if res.StatusCode >= 200 && res.StatusCode <= 299 {
return nil
}
errorResponse := &ErrorResponse{Response: res}
data, err := ioutil.ReadAll(res.Body)
if err == nil && data != nil {
json.Unmarshal(data, errorResponse)
}
return errorResponse
}