-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
267 lines (216 loc) · 5.25 KB
/
client.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
package lolp
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path"
"runtime"
"strings"
cleanhttp "github.com/hashicorp/go-cleanhttp"
)
const (
// defaultEndpoint
defaultEndpoint = "https://api.mc.lolipop.jp/"
// EndpointEnvVar for endpoint
EndpointEnvVar = "LOLP_ENDPOINT"
// TLSNoVerifyEnvVar for TLS verify skip flag
TLSNoVerifyEnvVar = "LOLP_TLS_NOVERIFY"
// TokenEnvVar for authentication
TokenEnvVar = "LOLP_TOKEN"
)
// projectURL for this
var projectURL = "https://github.com/pepabo/golipop"
// userAgent for request
var userAgent = fmt.Sprintf("lolp/%s (+%s; %s)", Version, projectURL, runtime.Version())
// Client struct
type Client struct {
URL *url.URL
HTTPClient *http.Client
DefaultHeader http.Header
Token string
}
// New returns client struct pointer
func New() *Client {
endpoint := os.Getenv(EndpointEnvVar)
if endpoint == "" {
endpoint = defaultEndpoint
}
client, err := NewClient(endpoint)
if err != nil {
panic(err)
}
token := os.Getenv(TokenEnvVar)
if token != "" {
client.Token = token
}
return client
}
// NewClient returns clean client struct pointer
func NewClient(u string) (*Client, error) {
if len(u) == 0 {
return nil, fmt.Errorf("client: missing url")
}
parsedURL, err := url.Parse(u)
if err != nil {
return nil, err
}
c := &Client{
URL: parsedURL,
DefaultHeader: make(http.Header),
}
if err := c.init(); err != nil {
return nil, err
}
return c, nil
}
// init initializes for client
func (c *Client) init() error {
c.DefaultHeader.Set("User-Agent", userAgent)
c.DefaultHeader.Set("Content-Type", "application/json")
tlsConfig := &tls.Config{}
if os.Getenv(TLSNoVerifyEnvVar) != "" {
tlsConfig.InsecureSkipVerify = true
}
t := cleanhttp.DefaultTransport()
t.TLSClientConfig = tlsConfig
c.HTTPClient = &http.Client{Transport: t}
return nil
}
// RequestOptions struct
type RequestOptions struct {
Params map[string]string
Headers map[string]string
Body io.Reader
BodyLength int64
}
// HTTP returns http.Response with dispose
func (c *Client) HTTP(verb, spath string, ro *RequestOptions) (*http.Response, error) {
req, err := c.Request(verb, spath, ro)
if err != nil {
return nil, err
}
res, err := dispose(c.HTTPClient.Do(req))
if err != nil {
return nil, err
}
return res, nil
}
// Request returns http.Request pointer with error
func (c *Client) Request(verb, spath string, ro *RequestOptions) (*http.Request, error) {
log.Printf("[INFO] request: %s %s", verb, spath)
if ro == nil {
ro = new(RequestOptions)
}
u := *c.URL
u.Path = path.Join(c.URL.Path, spath)
if c.Token != "" {
if ro.Headers == nil {
ro.Headers = make(map[string]string)
}
ro.Headers["Authorization"] = fmt.Sprintf("Bearer %s", c.Token)
}
return c.rawRequest(verb, &u, ro)
}
// rawRequest returns http.Request pointer with error
func (c *Client) rawRequest(verb string, u *url.URL, ro *RequestOptions) (*http.Request, error) {
if verb == "" {
return nil, fmt.Errorf("client: missing verb")
}
if u == nil {
return nil, fmt.Errorf("client: missing URL.url")
}
if ro == nil {
return nil, fmt.Errorf("client: missing RequestOptions")
}
var params = make(url.Values)
for k, v := range ro.Params {
params.Add(k, v)
}
u.RawQuery = params.Encode()
request, err := http.NewRequest(verb, u.String(), ro.Body)
if err != nil {
return nil, err
}
for k, v := range c.DefaultHeader {
request.Header[k] = v
}
for k, v := range ro.Headers {
request.Header.Add(k, v)
}
if ro.BodyLength > 0 {
request.ContentLength = ro.BodyLength
}
log.Printf("[DEBUG] raw request: %#v", request)
return request, nil
}
// dispose returns http.Request pointer with error
func dispose(res *http.Response, err error) (*http.Response, error) {
if err != nil {
return res, err
}
log.Printf("[INFO] response: %d (%s)", res.StatusCode, res.Status)
var buf bytes.Buffer
if _, err := io.Copy(&buf, res.Body); err != nil {
log.Printf("[ERR] response: error copying response body")
} else {
log.Printf("[DEBUG] response: %s", buf.String())
res.Body.Close()
res.Body = &bytesReadCloser{&buf}
}
switch res.StatusCode {
case 200:
return res, nil
case 201:
return res, nil
case 202:
return res, nil
case 204:
return res, nil
case 400:
return nil, parseErr(res)
case 401:
return nil, fmt.Errorf("authentication failed")
case 404:
return nil, fmt.Errorf("resource not found")
case 422:
return nil, parseErr(res)
default:
return nil, fmt.Errorf("client: %s", res.Status)
}
}
// parseErr parses for error response
func parseErr(r *http.Response) error {
re := &AppError{}
if err := decodeJSON(r, &re); err != nil {
return fmt.Errorf("error decoding JSON body: %s", err)
}
return re
}
// decodeJSON decodes for response
func decodeJSON(res *http.Response, out interface{}) error {
defer res.Body.Close()
dec := json.NewDecoder(res.Body)
return dec.Decode(out)
}
// bytesReadCloser struct
type bytesReadCloser struct {
*bytes.Buffer
}
// Close returns nil
func (nrc *bytesReadCloser) Close() error {
return nil
}
// AppError struct
type AppError struct {
Errors []string `json:"errors"`
}
// Error returns error by string
func (re *AppError) Error() string {
return strings.Join(re.Errors, ", ")
}