/
client.go
153 lines (127 loc) · 4.01 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
package goHttpProMaxPlus
import (
"errors"
"io"
"net/http"
"time"
)
// AspectModel 切片模组
type AspectModel func(*HttpRequest, *HttpResponse, ...interface{})
// HttpClient A client send request
type HttpClient struct {
c *http.Client
BeforeClientBuild AspectModel
AfterClientBuild AspectModel
BeforeRequestBuild AspectModel
AfterRequestBuild AspectModel
AfterResponseCreate AspectModel
AspectArgs []interface{} // Only the most recent assignment will be kept
}
func (client HttpClient) GetAspectArgs() []interface{} {
return client.AspectArgs
}
func (client HttpClient) Get(url string) (*HttpResponse, error) {
return client.GetWithCookieAndHeader(url, nil, nil)
}
func (client HttpClient) GetWithCookieAndHeader(url string, cookie, header map[string]string) (*HttpResponse, error) {
return client.Do(&HttpRequest{
Method: GET,
URL: url,
Headers: header,
Cookies: cookie,
})
}
func (client HttpClient) Post(url string) (*HttpResponse, error) {
return client.PostWithCookieHeaderAndForm(url, nil, nil, nil)
}
func (client HttpClient) PostWithForm(url string, form map[string]string) (*HttpResponse, error) {
return client.PostWithCookieHeaderAndForm(url, nil, nil, form)
}
func (client HttpClient) PostWithCookieHeaderAndForm(url string, cookie, header, form map[string]string) (*HttpResponse, error) {
return client.Do(&HttpRequest{
Method: POST,
URL: url,
Headers: header,
Cookies: cookie,
Forms: form,
})
}
func (client HttpClient) PostWithIoData(url string, data *io.Reader) (*HttpResponse, error) {
return client.PostWithCookieHeaderAndIoData(url, nil, nil, data)
}
func (client HttpClient) PostWithCookieHeaderAndIoData(url string, cookie, header map[string]string, data *io.Reader) (*HttpResponse, error) {
return client.Do(&HttpRequest{
URL: url,
Headers: header,
Cookies: cookie,
ReaderBody: data,
})
}
// Do run with Aspect
func (client HttpClient) Do(req *HttpRequest) (*HttpResponse, error) {
if client.BeforeRequestBuild != nil {
client.BeforeRequestBuild(req, nil, client.AspectArgs)
}
_req, err := req.BuildRequest()
if err != nil {
return nil, err
}
if client.AfterClientBuild != nil {
client.AfterRequestBuild(req, nil, client.AspectArgs)
}
_resp, err := client.c.Do(_req)
if err != nil {
return nil, errors.New("request err, msg : " + err.Error())
}
resp := CreateResponse(_resp)
if client.AfterResponseCreate != nil {
client.AfterResponseCreate(req, resp, client.AspectArgs)
}
return resp, nil
}
// defaultAspect Default aspect
func defaultAspect(*HttpRequest, *HttpResponse, ...interface{}) {}
var DefaultClient = &HttpClient{
&http.Client{Timeout: 5 * time.Second},
defaultAspect,
defaultAspect,
defaultAspect,
defaultAspect,
defaultAspect,
nil,
}
// GetDefaultClient
// !!! This method will not execute BeforeClientBuild and AfterClientBuild.
// 这个方法不会执行 BeforeClientBuild 和 AfterClientBuild。
func GetDefaultClient() *HttpClient {
return DefaultClient
}
// NewClient
// !!! This method will not execute BeforeClientBuild and AfterClientBuild.
// 这个方法不会执行 BeforeClientBuild 和 AfterClientBuild。
func NewClient(client *http.Client) *HttpClient {
return NewClientX(client, nil, nil, nil, nil, nil)
}
// NewClientX
// This method execute BeforeClientBuild and AfterClientBuild.
// 这个方法执行 BeforeClientBuild 和 AfterClientBuild。
func NewClientX(client *http.Client,
beforeClientBuild, afterClientBuild, beforeRequestBuild, afterRequestBuild, afterResponseCreate AspectModel,
args ...interface{}) *HttpClient {
if beforeRequestBuild != nil {
beforeRequestBuild(nil, nil, args)
}
hc := &HttpClient{
c: client,
BeforeClientBuild: beforeClientBuild,
AfterClientBuild: afterClientBuild,
BeforeRequestBuild: beforeRequestBuild,
AfterRequestBuild: afterRequestBuild,
AfterResponseCreate: afterResponseCreate,
AspectArgs: args,
}
if afterRequestBuild != nil {
afterRequestBuild(nil, nil, args)
}
return hc
}