-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
286 lines (246 loc) · 7.83 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package easy_http
import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"io"
"net/http"
"net/url"
"strings"
"time"
)
type Client struct {
//超时时间
timeOut time.Duration
//HTTP请求中的header信息
header map[string]string
//HTTP请求中,携带的cookies
cookies []*http.Cookie
//发起请求的client(go 自带的client)
client *http.Client
//临时header,每次请求后会重置
_header map[string]string
//临时cookie,每次请求后会重置
_cookies []*http.Cookie
//处理HTTP返回的response
buildResponse BuildResponse
}
//为这次请求设置header,只有这次会生效
func (c *Client) Header(header map[string]string) *Client {
c._header = header
return c
}
//为这次请求设置cookie,只有这次会生效
func (c *Client) Cookies(cookies []*http.Cookie) *Client {
c._cookies = cookies
return c
}
//为client添加header,原来的会保留,整个生命周期有效
func (c *Client) AddHeader(header map[string]string) {
for k, v := range header {
c.header[k] = v
}
}
//为client设置新的header,原来的会删除,整个生命周期有效
func (c *Client) SetHeader(header map[string]string) {
c.header = header
}
//为client添加cookie,原来的会保留,整个生命周期有效
func (c *Client) AddCookies(cookies []*http.Cookie) {
for _, cookie := range cookies {
c.cookies = append(c.cookies, cookie)
}
}
//为client设置新的cookie,原来的会删除,整个生命周期有效
func (c *Client) SetCookies(cookies []*http.Cookie) {
c.cookies = cookies
}
//发起HTTP请求
func (c *Client) DoRequest(r *http.Request) (*http.Response, error) {
return c.client.Do(r)
}
//指定请求的方法,发送请求
//`req` 参数 可以处理这次请求的request
func (c *Client) SendWithMethod(url, method string, body io.Reader, req func(request *http.Request)) IResponse {
request, err := c.getRequest(method, url, body)
if err != nil {
return c.buildResponse(nil, err)
}
if req != nil {
req(request)
}
return c.buildResponse(c.DoRequest(request))
}
//使用异步回调的方式,指定请求的方法,发送请求
//`req` 参数 可以处理这次请求的request
//`call` 参数,请求成功后的回调函数
func (c *Client) SendWithMethodCallBack(url, method string, body io.Reader, req func(request *http.Request), call func(response IResponse)) error {
request, err := c.getRequest(method, url, body)
if err != nil {
return err
}
if req != nil {
req(request)
}
go func() {
call(c.buildResponse(c.DoRequest(request)))
}()
return nil
}
//发起GET 请求
func (c *Client) Get(url string) IResponse {
return c.SendWithMethod(url, http.MethodGet, nil, nil)
}
//GET 异步请求,使用回调函数
func (c *Client) GetAsyn(url string, call func(response IResponse)) error {
return c.SendWithMethodCallBack(url, http.MethodGet, nil, nil, call)
}
//GET 异步请求,使用接口回调
func (c *Client) GetAsynWithCallback(url string, call ICallBack) error {
return c.GetAsyn(url, call.EasyResponseCallback)
}
//post 的form请求
func (c *Client) PostForm(url string, values url.Values) IResponse {
var reader io.Reader
if values != nil {
reader = strings.NewReader(values.Encode())
}
return c.SendWithMethod(url, http.MethodPost, reader, EasyPostFromRequest)
}
//Post form 异步请求,使用回调函数
func (c *Client) PostFormAsyn(url string, values url.Values, call func(response IResponse)) error {
if call == nil {
return errors.New("callback function is nil")
}
if values == nil {
return errors.New("values is nil")
}
reader := strings.NewReader(values.Encode())
return c.SendWithMethodCallBack(url, http.MethodPost, reader, EasyPostFromRequest, call)
}
//Post form 异步请求,使用接口回调
func (c *Client) PostFormAsynWithCallback(url string, values url.Values, call ICallBack) error {
return c.PostFormAsyn(url, values, call.EasyResponseCallback)
}
//post 的bytes请求
func (c *Client) PostBytes(url string, value []byte, req func(request *http.Request)) IResponse {
if value == nil {
return c.buildResponse(nil, errors.New("PostBytes value is nil"))
}
reader := bytes.NewReader(value)
return c.SendWithMethod(url, http.MethodPost, reader, req)
}
//post 的bytes请求
func (c *Client) PostBytesAsyn(url string, value []byte, req func(request *http.Request), call func(response IResponse)) error {
if call == nil {
return errors.New("callback function is nil")
}
if value == nil {
return errors.New("value is nil")
}
reader := bytes.NewReader(value)
return c.SendWithMethodCallBack(url, http.MethodPost, reader, req, call)
}
//post 的json请求
func (c *Client) PostJson(url string, value interface{}) IResponse {
if value == nil {
return c.buildResponse(nil, errors.New("PostJson value is nil"))
}
by, err := json.Marshal(value)
if err != nil {
return c.buildResponse(nil, err)
}
return c.PostBytes(url, by, EasyPostJsonRequest)
}
//Post json 异步请求,使用回调函数
func (c *Client) PostJsonAsyn(url string, value interface{}, call func(response IResponse)) error {
if call == nil {
return errors.New("callback function is nil")
}
if value == nil {
return errors.New("value is nil")
}
by, err := json.Marshal(value)
if err != nil {
return errors.New("value json encode error: " + err.Error())
}
return c.PostBytesAsyn(url, by, EasyPostJsonRequest, call)
}
//Post json 异步请求,使用接口回调
func (c *Client) PostJsonAsynWithCallback(url string, values interface{}, call ICallBack) error {
return c.PostJsonAsyn(url, values, call.EasyResponseCallback)
}
//post 的xml请求
func (c *Client) PostXml(url string, value interface{}) IResponse {
if value == nil {
return c.buildResponse(nil, errors.New("PostJson value is nil"))
}
by, err := xml.Marshal(value)
if err != nil {
return c.buildResponse(nil, err)
}
return c.PostBytes(url, by, EasyPostXmlRequest)
}
//Post xml 异步请求,使用回调函数
func (c *Client) PostXmlAsyn(url string, value interface{}, call func(response IResponse)) error {
if call == nil {
return errors.New("callback function is nil")
}
if value == nil {
return errors.New("value is nil")
}
by, err := json.Marshal(value)
if err != nil {
return errors.New("value json encode error: " + err.Error())
}
return c.PostBytesAsyn(url, by, EasyPostXmlRequest, call)
}
//Post xml 异步请求,使用接口回调
func (c *Client) PostXmlAsynWithCallback(url string, values interface{}, call ICallBack) error {
return c.PostXmlAsyn(url, values, call.EasyResponseCallback)
}
//post 的multipart请求
func (c *Client) PostMultipart(url string, body IMultipart) IResponse {
return c.SendWithMethod(url, http.MethodPost, body, func(request *http.Request) {
request.Header.Set("Content-Type", body.ContentType())
})
}
//post 的multipart请求,使用回调函数
func (c *Client) PostMultipartAsyn(url string, body IMultipart, call func(response IResponse)) error {
if call == nil {
return errors.New("callback function is nil")
}
return c.SendWithMethodCallBack(url, http.MethodPost, body, func(request *http.Request) {
request.Header.Set("Content-Type", body.ContentType())
}, call)
}
//post 的multipart请求,使用接口回调
func (c *Client) PostMultipartAsynWithCallback(url string, body IMultipart, call ICallBack) error {
return c.PostMultipartAsyn(url, body, call.EasyResponseCallback)
}
//初始化一个 http.Request, 并填充属性
func (c *Client) getRequest(method, url string, body io.Reader) (*http.Request, error) {
request, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
for k, v := range c.header {
request.Header.Set(k, v)
}
for k, v := range c._header {
request.Header.Set(k, v)
}
c._header = nil
if _, e := request.Header["User-Agent"]; !e {
request.Header.Set("User-Agent", HTTP_USER_AGENT_CHROME_PC)
}
for _, v := range c.cookies {
request.AddCookie(v)
}
for _, v := range c._cookies {
request.AddCookie(v)
}
c._cookies = nil
return request, nil
}