-
Notifications
You must be signed in to change notification settings - Fork 2
/
http.go
195 lines (158 loc) · 3.64 KB
/
http.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
// Copyright 2016 The go-ego Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// https://github.com/go-ego/ego/blob/master/LICENSE
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
package http
import (
"bytes"
"io"
"os"
"time"
"mime/multipart"
"net/http"
"net/url"
)
// Map a [string]interface{} map
type Map map[string]interface{}
// Get http get
func Get(api string, args ...url.Values) ([]byte, error) {
var params url.Values
if len(args) > 0 {
params = args[0]
}
u, err := url.Parse(api)
if err != nil {
return nil, err
}
// URLEncode
u.RawQuery = params.Encode()
resp, err := http.Get(u.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
// Post http post, params is url.Values type
func Post(api string, args ...interface{}) ([]byte, error) {
var params url.Values
if len(args) > 0 {
params = args[0].(url.Values)
}
out := 1000
if len(args) > 1 {
out = args[1].(int)
}
timeOut := time.Duration(out) * time.Millisecond
c := &http.Client{
Timeout: timeOut,
}
resp, err := c.PostForm(api, params)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
// Api http api
func Api(api string, args ...interface{}) (rs []byte, err error) {
paramMap := Map{}
if len(args) > 0 {
paramMap = args[0].(Map)
}
param := url.Values{}
for k, v := range paramMap {
param.Set(k, v.(string))
}
apiMethod := "post"
if len(args) > 1 {
apiMethod = args[1].(string)
}
if apiMethod == "get" {
rs, err = Get(api, param)
return
}
rs, err = Post(api, param)
return
}
// Do http.Do
func Do(url, method string, out int, args ...[]string) (*http.Response, error) {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", GetRandomUserAgent(args...))
// client := http.DefaultClient
timeOut := time.Duration(out) * time.Millisecond
client := &http.Client{
Timeout: timeOut,
}
res, e := client.Do(req)
if e != nil {
return res, e
}
return res, nil
}
// DoPost http.Do post
func DoPost(url string, args ...interface{}) (*http.Response, error) {
var out int
if len(args) > 0 {
out = args[0].(int)
}
if len(args) > 1 {
res, err := Do(url, "POST", out, args[1].([]string))
return res, err
}
res, err := Do(url, "POST", out)
return res, err
}
// DoGet http.Do get
func DoGet(url string, args ...interface{}) (*http.Response, error) {
var out int
if len(args) > 0 {
out = args[0].(int)
}
if len(args) > 1 {
res, err := Do(url, "GET", out, args[1].([]string))
return res, err
}
res, err := Do(url, "GET", out)
return res, err
}
// PostFile post file
func PostFile(filename, targetUrl, upParam string) (string, error) {
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
// uploadfile
fileWriter, err := bodyWriter.CreateFormFile(upParam, filename)
if err != nil {
return "", err
}
// openfile
fh, err := os.Open(filename)
if err != nil {
return "", err
}
// iocopy
_, err = io.Copy(fileWriter, fh)
if err != nil {
return "", err
}
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
resp, err := http.Post(targetUrl, contentType, bodyBuf)
if err != nil {
return "", err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(respBody), nil
}