forked from tencentyun/tsf-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
145 lines (130 loc) · 3.08 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
package http
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net"
"net/http"
"time"
"github.com/go-kratos/kratos/v2/errors"
)
type Client struct {
cli *http.Client
}
type options struct {
timeout time.Duration
maxConnsPerHost int
}
// Option configures how we set up the client.
type Option interface {
apply(*options)
}
// funcOption wraps a function that modifies Options into an
// implementation of the Option interface.
type funcOption struct {
f func(*options)
}
func (fdo *funcOption) apply(do *options) {
fdo.f(do)
}
func newFuncOption(f func(*options)) *funcOption {
return &funcOption{
f: f,
}
}
// WithTimeout returns a Option that configures a timeout for dialing a ClientConn initially.
func WithTimeout(timeout time.Duration) Option {
return newFuncOption(func(o *options) {
o.timeout = timeout
})
}
// WithMaxConnPerHost returns a Option that configures a maxConnsPerHost for dialing a ClientConn initially.
func WithMaxConnPerHost(max int) Option {
return newFuncOption(func(o *options) {
o.maxConnsPerHost = max
})
}
func NewClient(optFunc ...Option) *Client {
opts := &options{}
for _, f := range optFunc {
f.apply(opts)
}
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 20,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 6 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: 6,
MaxConnsPerHost: opts.maxConnsPerHost,
}
return &Client{cli: &http.Client{
Timeout: opts.timeout,
Transport: transport,
}}
}
// Get http get
func (c *Client) Get(url string, respBody interface{}) (header http.Header, err error) {
header, err = c.Do("GET", url, nil, respBody)
return
}
// Put http put
func (c *Client) Put(url string, reqBody interface{}, respBody interface{}) (err error) {
_, err = c.Do("PUT", url, reqBody, respBody)
return
}
// Post http post
func (c *Client) Post(url string, reqBody interface{}, respBody interface{}) (err error) {
_, err = c.Do("POST", url, reqBody, respBody)
return
}
// Do http do
func (c *Client) Do(method string, url string, reqBody interface{}, respBody interface{}) (header http.Header, err error) {
var (
resp *http.Response
content []byte
body io.Reader
req *http.Request
)
if reqBody != nil {
content, err = json.Marshal(reqBody)
if err != nil {
return
}
body = bytes.NewReader(content)
}
req, err = http.NewRequest(method, url, body)
if err != nil {
return
}
if body != nil {
req.Header.Add("Content-Type", "application/json")
}
resp, err = c.cli.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
header = resp.Header
if resp.StatusCode != http.StatusOK {
content, _ = ioutil.ReadAll(resp.Body)
err = errors.Newf(resp.StatusCode, errors.UnknownReason, string(content))
return
}
content, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
if respBody != nil {
err = json.Unmarshal(content, respBody)
if err != nil {
return
}
}
return
}