-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxhttp.go
323 lines (269 loc) · 7.84 KB
/
xhttp.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Package xhttp provides a custom http client based on net/http.
package xhttp
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"reflect"
"strings"
"time"
)
// The defaults.
const (
DefaultClientTimeout = 30 * time.Second
DefaultDialTimeout = 10 * time.Second
DefaultKeepAlive = 30 * time.Second
DefaultIdleConnTimeout = 90 * time.Second
DefaultTLSHandshakeTimeout = 10 * time.Second
DefaultExpectContinueTimeout = 1 * time.Second
DefaultMaxIdleConns = 100
)
var (
// DefaultClient is used if no custom HTTP client is defined.
DefaultClient *Client = NewClient()
// ErrInvalidValue indicates a wrong type of the value passed to Unmarshal*To methods.
ErrInvalidValue = errors.New("xhttp: receiving value must be a pointer")
)
// Do makes a request based on http.Request using DefaultClient.
func Do(req *http.Request) (*http.Response, error) {
return DefaultClient.Do(req)
}
// Get makes a Get request using DefaultClient.
func Get(url string) (*http.Response, error) {
return DefaultClient.Get(url)
}
// Head makes a Head request using DefaultClient.
func Head(url string) (*http.Response, error) {
return DefaultClient.Head(url)
}
// Post makes a Post request using DefaultClient.
func Post(url, contentType string, body io.Reader) (*http.Response, error) {
return DefaultClient.Post(url, contentType, body)
}
// PostForm makes a PostForm request using DefaultClient.
func PostForm(url string, data url.Values) (*http.Response, error) {
return DefaultClient.PostForm(url, data)
}
// GET makes a GET request using DefaultClient.
func GET(url string) (*Response, error) {
return DefaultClient.GET(url)
}
// POST makes a POST request using DefaultClient.
func POST(url, contentType string, body []byte) (*Response, error) {
return DefaultClient.POST(url, contentType, body)
}
// Send makes a request based on Request using DefaultClient.
func Send(req *Request) (*Response, error) {
return DefaultClient.Send(req)
}
// DownloadFile downloads the file located at the url.
// It stores the result in a file with the filename.
func DownloadFile(url, filename string) error {
return DefaultClient.DownloadFile(url, filename)
}
// ClientConfig holds the configuration for a Client.
type ClientConfig struct {
Timeout time.Duration
DialTimeout time.Duration
KeepAlive time.Duration
IdleConnTimeout time.Duration
TLSHanshakeTimeout time.Duration
ExpectContinueTimeout time.Duration
MaxIdleConns int
SkipTLSVerify bool
IncludeRootCA bool
}
// Client represents a custom http client wrapper around net/http.Client.
type Client struct {
*http.Client
}
// NewClient returns a Client with customized default settings.
//
// More details here https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/
func NewClient() *Client {
return createClient(DefaultClientConfig())
}
// NewClientWithConfig creates a new Client with the settings specified in cfg.
func NewClientWithConfig(cfg *ClientConfig) *Client {
return createClient(cfg)
}
// createClient creates a new Client using custom tls.Config and http.Transport.
func createClient(cfg *ClientConfig) *Client {
// Create a custom tls config.
tlsConfig := &tls.Config{
InsecureSkipVerify: cfg.SkipTLSVerify,
}
// TODO: Create a pool with root CA.
// if customCACerts {}
// Create a custom transport.
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: cfg.DialTimeout,
KeepAlive: cfg.KeepAlive,
}).DialContext,
IdleConnTimeout: cfg.IdleConnTimeout,
TLSHandshakeTimeout: cfg.TLSHanshakeTimeout,
ExpectContinueTimeout: cfg.ExpectContinueTimeout,
MaxIdleConns: cfg.MaxIdleConns,
TLSClientConfig: tlsConfig,
}
// Finally, create a custom http client.
client := &Client{
Client: &http.Client{
Timeout: cfg.Timeout,
Transport: transport,
},
}
return client
}
// DefaultClientConfig returns ClientConfig with default settings.
func DefaultClientConfig() *ClientConfig {
return &ClientConfig{
Timeout: DefaultClientTimeout,
DialTimeout: DefaultDialTimeout,
KeepAlive: DefaultKeepAlive,
IdleConnTimeout: DefaultIdleConnTimeout,
TLSHanshakeTimeout: DefaultTLSHandshakeTimeout,
ExpectContinueTimeout: DefaultExpectContinueTimeout,
MaxIdleConns: DefaultMaxIdleConns,
SkipTLSVerify: false,
IncludeRootCA: false,
}
}
// GET makes a Get request.
func (c *Client) GET(url string) (*Response, error) {
req := NewRequest(http.MethodGet, url, nil)
return c.Send(req)
}
// POST makes a Post request.
func (c *Client) POST(url, contentType string, body []byte) (*Response, error) {
req := NewRequest(http.MethodPost, url, body)
req.Header.Set("Content-Type", contentType)
return c.Send(req)
}
// Send makes a request.
func (c *Client) Send(request *Request) (*Response, error) {
// Build the HTTP request object.
req, err := c.buildRequest(request)
if err != nil {
return nil, err
}
// Build the HTTP client and make the request.
resp, err := c.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
return c.buildResponse(resp)
}
// DownloadFile downloads the file located at an url and stores it in the given path.
func (c *Client) DownloadFile(url, filename string) error {
resp, err := c.GET(url)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("download failed: %s", resp.Status)
}
return ioutil.WriteFile(filename, resp.Body, 0644)
}
// buildRequest creates a http.Request from Request.
func (c *Client) buildRequest(req *Request) (*http.Request, error) {
url := req.BaseURL
if len(req.Param) != 0 {
url = strings.Join([]string{req.BaseURL, "?", req.Param.Encode()}, "")
}
r, err := http.NewRequest(req.Method, url, bytes.NewBuffer(req.Body))
if err != nil {
return nil, err
}
if len(req.Header) != 0 {
r.Header = req.Header
}
return r, nil
}
// buildResponse builds Response from http.Response.
//
// It takes care of closing the body.
func (c *Client) buildResponse(resp *http.Response) (*Response, error) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
response := &Response{
StatusCode: resp.StatusCode,
Status: resp.Status,
Body: body,
Headers: resp.Header,
}
return response, nil
}
// Request defines a request.
type Request struct {
Method string
BaseURL string
Body []byte
Header http.Header
Param url.Values
}
// NewRequest returns a Request ready for use.
func NewRequest(m string, u string, b []byte) *Request {
return &Request{
Method: m,
BaseURL: u,
Body: b,
Header: make(http.Header),
Param: make(url.Values),
}
}
// SetContentTypeJSON sets the Content-Type header to "application/json".
func (r *Request) SetContentTypeJSON() {
if r.Header == nil {
r.Header = make(http.Header)
}
r.Header.Set("Content-Type", "application/json")
}
// SetContentType sets the Content-Type header to a given value.
func (r *Request) SetContentType(value string) {
if r.Header == nil {
r.Header = make(http.Header)
}
r.Header.Set("Content-Type", value)
}
// SetAuthorization sets the Authorization header to a given value.
func (r *Request) SetAuthorization(value string) {
if r.Header == nil {
r.Header = make(http.Header)
}
r.Header.Set("Authorization", value)
}
// Response holds data from a response.
type Response struct {
StatusCode int
Status string
Body []byte
Headers http.Header
}
func (r *Response) UnmarshalJSONTo(val interface{}) error {
if err := ensurePointer(val); err != nil {
return err
}
return json.Unmarshal(r.Body, val)
}
func ensurePointer(v interface{}) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr {
return ErrInvalidValue
}
return nil
}