-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpcli.go
More file actions
125 lines (97 loc) · 2.5 KB
/
httpcli.go
File metadata and controls
125 lines (97 loc) · 2.5 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
package httpcli
import (
"context"
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"net/http/httputil"
"os"
"time"
)
var (
DefaultTransport http.RoundTripper
debug bool
)
func init() {
DefaultTransport = &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 500,
MaxIdleConnsPerHost: 250,
IdleConnTimeout: 90 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
debug = (os.Getenv("HTTPCLIDEBUG") != "")
}
type cli struct {
Timeout time.Duration
Insecure bool
}
type Option func(*cli)
func WithTimeout(d time.Duration) Option {
return func(c *cli) { c.Timeout = d }
}
func Do(ctx context.Context, req *http.Request, opts ...Option) (*http.Response, error) {
c := &cli{Timeout: 30 * time.Second, Insecure: true}
for _, opt := range opts {
opt(c)
}
client := http.Client{
Timeout: time.Duration(c.Timeout),
Transport: DefaultTransport,
}
rsp, err := client.Do(req.WithContext(ctx))
if debug && rsp != nil {
var b []byte
b, _ = httputil.DumpRequest(rsp.Request, true)
fmt.Printf("%s\n", b)
b, _ = httputil.DumpResponse(rsp, true)
fmt.Printf("%s\n", b)
}
return rsp, err
}
func Get(ctx context.Context, url string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
return Do(ctx, req)
}
func Post(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
return Do(ctx, req)
}
func Put(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPut, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
return Do(ctx, req)
}
func Patch(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPatch, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
return Do(ctx, req)
}
func Delete(ctx context.Context, url string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
return nil, err
}
return Do(ctx, req)
}