forked from AliyunContainerService/pouch
/
httpcli.go
276 lines (235 loc) · 6.19 KB
/
httpcli.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
package client
import (
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"net/url"
"os"
"reflect"
"github.com/alibaba/pouch/pkg/serializer"
"github.com/go-resty/resty"
)
// HTTPClient represents a client connect to http server.
type HTTPClient struct {
cli *resty.Client
req *resty.Request
err error
resp *resty.Response
method string
urls string
code int
}
var restyClient = resty.New().SetCloseConnection(true)
func isPtr(i interface{}) bool {
return reflect.ValueOf(i).Kind() == reflect.Ptr
}
func parseTLSConfig(ca, cert, key []byte) (*tls.Config, error) {
tlsConfig := &tls.Config{
// Prefer TLS1.2 as the client minimum
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: os.Getenv("DOCKER_TLS_VERIFY") == "",
}
//parse root ca
cas, err := certpool(ca)
if err != nil {
return nil, err
}
tlsConfig.RootCAs = cas
//parse cert and key pem
tlsCert, err := tls.X509KeyPair(cert, key)
if err != nil {
return nil, err
}
tlsConfig.Certificates = []tls.Certificate{tlsCert}
return tlsConfig, nil
}
func certpool(pem []byte) (*x509.CertPool, error) {
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(pem) {
return nil, fmt.Errorf("failed to append certificates from PEM")
}
return certPool, nil
}
// HTTPClientNew returns HTTPClient class object.
func HTTPClientNew() *HTTPClient {
return &HTTPClient{
cli: restyClient,
req: restyClient.R(),
resp: &resty.Response{},
}
}
func (c *HTTPClient) verb(method string) *HTTPClient {
c.method = method
return c
}
// TLSConfig is used to set tls config with specified config.
func (c *HTTPClient) TLSConfig(tlsc *tls.Config) *HTTPClient {
c.cli.SetTLSClientConfig(tlsc)
return c
}
// TLS is used to set tls config with ca, cert and key.
func (c *HTTPClient) TLS(ca, cert, key []byte) (*HTTPClient, error) {
tlsc, err := parseTLSConfig(ca, cert, key)
if err != nil {
return nil, err
}
c.cli.SetTLSClientConfig(tlsc)
return c, nil
}
// PUT used to implement http PUT method.
func PUT() *HTTPClient {
c := HTTPClientNew()
return c.verb("PUT")
}
// GET used to implement HTTPClient GET method.
func GET() *HTTPClient {
c := HTTPClientNew()
return c.verb("GET")
}
// POST used to implement HTTPClient POST method.
func POST() *HTTPClient {
c := HTTPClientNew()
return c.verb("POST")
}
// DELETE used to implement HTTPClient DELETE method.
func DELETE() *HTTPClient {
c := HTTPClientNew()
return c.verb("DELETE")
}
// PUT used to implement HTTPClient PUT method and then returns HTTPClient
func (c *HTTPClient) PUT() *HTTPClient {
c.verb("PUT")
return c
}
// GET used to implement HTTPClient GET method and then returns HTTPClient
func (c *HTTPClient) GET() *HTTPClient {
c.verb("GET")
return c
}
// POST used to implement HTTPClient POST method and then returns HTTPClient
func (c *HTTPClient) POST() *HTTPClient {
c.verb("POST")
return c
}
// DELETE used to implement HTTPClient DELETE method and then returns HTTPClient
func (c *HTTPClient) DELETE() *HTTPClient {
c.verb("DELETE")
return c
}
// Err returns HTTPClient's error.
func (c *HTTPClient) Err() error {
return c.err
}
// Method used to implement HTTPClient method that specified by caller,
// and then returns HTTPClient object.
func (c *HTTPClient) Method(method string) *HTTPClient {
c.verb(method)
return c
}
// URL is used to set url and then returns HTTPClient object.
func (c *HTTPClient) URL(rawurl string) *HTTPClient {
if _, err := url.Parse(rawurl); err != nil {
c.err = err
return c
}
c.urls = rawurl
return c
}
// SetHeader is used to set header and then returns HTTPClient object.
func (c *HTTPClient) SetHeader(key, value string) *HTTPClient {
c.req.SetHeader(key, value)
return c
}
// Body is used to set body with map[string]string type,
// and then returns HTTPClient object.
func (c *HTTPClient) Body(obj serializer.Object) *HTTPClient {
if c.err != nil {
return c
}
switch t := obj.(type) {
case map[string]string:
c.req.SetFormData(t)
default:
c.err = fmt.Errorf("post body need a map[string]string, get: %v", t)
}
return c
}
// JSONBody is used to set body with json format and then returns HTTPClient object.
func (c *HTTPClient) JSONBody(obj serializer.Object) *HTTPClient {
c.req.SetHeader("Content-Type", "application/json")
c.req.SetBody(obj)
return c
}
// Query is used to set query and then returns HTTPClient object.
func (c *HTTPClient) Query(obj serializer.Object) *HTTPClient {
if c.err != nil {
return c
}
switch t := obj.(type) {
case string:
c.req.SetQueryString(t)
case map[string][]string:
query := url.Values(t)
c.req.SetQueryString(query.Encode())
case map[string]string:
c.req.SetQueryParams(t)
case url.Values:
c.req.SetQueryString(t.Encode())
default:
c.err = fmt.Errorf("invalid query %#v", obj)
}
return c
}
// Do used to deal http request and then returns HTTPClient object.
func (c *HTTPClient) Do() *HTTPClient {
if c.err != nil {
return c
}
switch c.method {
case "POST":
c.resp, c.err = c.req.Post(c.urls)
case "GET":
c.resp, c.err = c.req.Get(c.urls)
case "DELETE":
c.resp, c.err = c.req.Delete(c.urls)
case "PUT":
c.resp, c.err = c.req.Put(c.urls)
default:
c.err = fmt.Errorf("unsupport method: %s", c.method)
return c
}
c.code = c.resp.StatusCode()
if c.code >= http.StatusBadRequest {
c.err = fmt.Errorf("Bad response code: %d, %s", c.code, string(c.resp.Body()))
}
return c
}
// RespCodeEqual check HTTPClient's code is equal with "code" or not.
func (c *HTTPClient) RespCodeEqual(code int) bool {
return c.code == code
}
// StatusCode returns HTTPClient code.
func (c *HTTPClient) StatusCode() int {
return c.code
}
// Into used to set obj into body.
func (c *HTTPClient) Into(obj serializer.Object) error {
if c.err != nil {
return c.err
}
if !isPtr(obj) {
return fmt.Errorf("expect ptr, get: %v", obj)
}
body := c.resp.Body()
if reflect.TypeOf(obj).Elem().Kind() == reflect.String {
reflect.Indirect(reflect.ValueOf(obj)).Set(reflect.Indirect(reflect.ValueOf(string(body))))
return nil
}
newObj := reflect.New(reflect.TypeOf(obj).Elem()).Interface()
if err := serializer.Codec.Decode(body, newObj); err != nil {
return err
}
reflect.Indirect(reflect.ValueOf(obj)).Set(reflect.Indirect(reflect.ValueOf(newObj)))
return nil
}