-
Notifications
You must be signed in to change notification settings - Fork 1
/
web_client.go
257 lines (228 loc) · 6.46 KB
/
web_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
package webkit
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"path/filepath"
"strings"
)
// Form 表单类型
type Form map[string]interface{}
type responseHandle = func(resp http.Response) error
const (
ContentType_Form = "application/x-www-form-urlencoded"
ContentType_JSON = "application/json"
)
// WebClient web请求辅助工具
type WebClient struct {
host string
headers map[string]string
cli *http.Client
}
// NewWebClient WebClient构造函数
func NewWebClient(host string) *WebClient {
jar, _ := cookiejar.New(nil)
return &WebClient{host, make(map[string]string), &http.Client{Jar: jar}}
}
// Set_Token 设置token
func (me *WebClient) SetHeader(key string, value string) {
me.headers[key] = value
}
// GetCookies 根据域名获取cookies
func (me *WebClient) GetCookies(rawURL string) ([]*http.Cookie, error) {
u, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
return me.cli.Jar.Cookies(u), nil
}
// SetCookie 根据域名设置单条cookie
func (me *WebClient) SetCookie(rawURL string, name string, value string) error {
u, err := url.Parse(rawURL)
if err != nil {
return err
}
me.cli.Jar.SetCookies(u,
[]*http.Cookie{&http.Cookie{Name: name, Value: value, HttpOnly: true}})
return nil
}
// SetCookies 根据域名设置cookies
func (me *WebClient) SetCookies(rawURL string, cookies []*http.Cookie) error {
u, err := url.Parse(rawURL)
if err != nil {
return err
}
me.cli.Jar.SetCookies(u, cookies)
return nil
}
// HTTP_request http请求
func (me *WebClient) HTTP_request(method Method, relativePath string,
contentType string, params io.Reader,
header map[string]interface{}) (*http.Response, error) {
req, err := http.NewRequest(method.String(),
me.host+relativePath, params)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
for k, v := range me.headers {
req.Header.Set(k, v)
}
for k, v := range header {
req.Header.Set(k, fmt.Sprint(v))
}
return me.cli.Do(req)
}
func (me *WebClient) processRequest(method Method, relativePath string,
contentType string, params io.Reader, handles ...responseHandle) ([]byte, error) {
resp, err := me.HTTP_request(method, relativePath, contentType, params, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
for _, v := range handles {
err = v(*resp)
if err != nil {
return nil, err
}
}
return ioutil.ReadAll(resp.Body)
}
// Form_request 表单请求
func (me *WebClient) Form_request(method Method, relativePath string, params Form,
handles ...responseHandle) ([]byte, error) {
forms := url.Values{}
for k, v := range params {
forms.Add(k, fmt.Sprint(v))
}
return me.processRequest(method, relativePath, ContentType_Form,
strings.NewReader(forms.Encode()), handles...)
}
// Form_GET 表单get
func (me *WebClient) Form_GET(relativePath string, params Form,
handles ...responseHandle) ([]byte, error) {
return me.Form_request(GET, relativePath, params, handles...)
}
// Form_POST 表单post
func (me *WebClient) Form_POST(relativePath string, params Form,
handles ...responseHandle) ([]byte, error) {
return me.Form_request(POST, relativePath, params, handles...)
}
// Form_PUT 表单put
func (me *WebClient) Form_PUT(relativePath string, params Form,
handles ...responseHandle) ([]byte, error) {
return me.Form_request(PUT, relativePath, params, handles...)
}
// Form_DELETE 表单delete
func (me *WebClient) Form_DELETE(relativePath string, params Form,
handles ...responseHandle) ([]byte, error) {
return me.Form_request(DELETE, relativePath, params, handles...)
}
// JSON_request JSON请求
func (me *WebClient) JSON_request(method Method, relativePath string,
params interface{}, response interface{},
handles ...responseHandle) error {
bt, err := json.Marshal(params)
if err != nil {
return err
}
body, err := me.processRequest(method, relativePath, ContentType_JSON, bytes.NewReader(bt), handles...)
if err != nil {
return err
}
return json.Unmarshal(body, response)
}
// JSON_GET JSON get
func (me *WebClient) JSON_GET(relativePath string,
params interface{}, response interface{},
handles ...responseHandle) error {
return me.JSON_request(GET, relativePath, params, response, handles...)
}
// JSON_POST JSON post
func (me *WebClient) JSON_POST(relativePath string,
params interface{}, response interface{},
handles ...responseHandle) error {
return me.JSON_request(POST, relativePath, params, response, handles...)
}
// JSON_PUT JSON put
func (me *WebClient) JSON_PUT(relativePath string,
params interface{}, response interface{},
handles ...responseHandle) error {
return me.JSON_request(PUT, relativePath, params, response, handles...)
}
// JSON_DELETE JSON delete
func (me *WebClient) JSON_DELETE(relativePath string,
params interface{}, response interface{},
handles ...responseHandle) error {
return me.JSON_request(DELETE, relativePath, params, response, handles...)
}
// File_Info 发送文件类型
type File_Info struct {
Field string
Path string
Params map[string]string
}
// UploadFile 文件上传方法
func (me *WebClient) UploadFile(relativePath string, field string,
path string, params Form,
handles ...responseHandle) ([]byte, error) {
fileBuffer := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(fileBuffer)
fileWriter, err := bodyWriter.CreateFormFile(field, filepath.Base(path))
if err != nil {
return nil, err
}
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
_, err = io.Copy(fileWriter, f)
if err != nil {
return nil, err
}
for k, v := range params {
err = bodyWriter.WriteField(k, fmt.Sprint(v))
if err != nil {
return nil, err
}
}
err = bodyWriter.Close()
if err != nil {
return nil, err
}
return me.processRequest(POST, relativePath, bodyWriter.FormDataContentType(),
fileBuffer, handles...)
}
// DownloadFile 文件下载方法
func (me *WebClient) DownloadFile(relativePath string, savePath string,
params Form, handles ...responseHandle) error {
body, err := me.Form_GET(relativePath, params)
if err != nil {
return err
}
// 创建文件夹
if err := os.MkdirAll(filepath.Dir(savePath), os.ModePerm); err != nil {
return err
}
// 写入临时文件
f, err := ioutil.TempFile(filepath.Dir(savePath), filepath.Base(savePath)+"_temp")
if err != nil {
return err
}
if _, err := f.Write(body); err != nil {
f.Close()
os.Remove(f.Name())
return err
}
f.Close()
// 将临时文件重命名为目标文件
return os.Rename(f.Name(), savePath)
}