-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
197 lines (169 loc) · 4.62 KB
/
request.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
package request
import (
"bytes"
"compress/flate"
"compress/gzip"
"crypto/tls"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/pkg/errors"
)
// type RequestOptionsFunc func(*RequestOptions)
// type RequestOptions struct {
// retryTimes int
// userAgent string
// refer string
// }
// func WithRetryTimes(v int) RequestOptionsFunc {
// return func(o *RequestOptions) {
// o.retryTimes = v
// }
// }
// func WithUserAgent(v string) RequestOptionsFunc {
// return func(o *RequestOptions) {
// o.userAgent = v
// }
// }
// func WithRefer(v string) RequestOptionsFunc {
// return func(o *RequestOptions) {
// o.refer = v
// }
// }
func Request(method, url string, body io.Reader, headers map[string]string) (*http.Response, error) {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableCompression: true,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: transport,
Timeout: 15 * time.Minute,
}
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, errors.WithStack(err)
}
for k, v := range headers {
req.Header.Set(k, v)
}
if _, ok := headers["Referer"]; !ok {
req.Header.Set("Referer", url)
}
var (
res *http.Response
requestError error
)
for i := 0; ; i++ {
res, requestError = client.Do(req)
if requestError == nil && res.StatusCode < 400 {
break
} else if i+1 >= 3 {
var err error
if requestError != nil {
err = errors.Errorf("request error: %v", requestError)
} else {
err = errors.Errorf("%s request error: HTTP %d", url, res.StatusCode)
}
return nil, errors.WithStack(err)
}
time.Sleep(1 * time.Second)
}
return res, nil
}
func Get(rawURL string, params map[string]string, headers map[string]string) (string, error) {
urlValues := url.Values{}
Url, _ := url.Parse(rawURL)
for key, val := range params {
urlValues.Set(key, val)
}
Url.RawQuery = urlValues.Encode()
urlPath := Url.String()
body, err := GetByte(http.MethodGet, urlPath, nil, headers)
return string(body), err
}
func PostForm(url string, data map[string]string, headers map[string]string) (string, error) {
return Post(url, "application/x-www-form-urlencoded", data, headers)
}
func PostJson(url string, data map[string]string, headers map[string]string) (string, error) {
return Post(url, "application/json", data, headers)
}
// func PostFile(url string, data map[string]string) (string, error) {
// return Post(url, "multipart/form-data", data, nil)
// }
func Post(url, contentType string, data map[string]string, headers map[string]string) (string, error) {
body := GetReader(data, contentType)
headers["Content-Type"] = contentType
resp, err := GetByte(http.MethodPost, url, body, headers)
return string(resp), err
}
func GetReader(data map[string]string, contentType string) io.Reader {
if strings.Contains(contentType, "json") {
bytesData, _ := json.Marshal(data)
return bytes.NewReader(bytesData)
}
urlValues := url.Values{}
for key, val := range data {
urlValues.Set(key, val)
}
body := urlValues.Encode()
return strings.NewReader(body)
}
func GetByte(method, url string, body io.Reader, headers map[string]string) ([]byte, error) {
res, err := Request(method, url, body, headers)
if err != nil {
return nil, errors.WithStack(err)
}
defer res.Body.Close()
var reader io.ReadCloser
switch res.Header.Get("Content-Encoding") {
case "gzip":
reader, _ = gzip.NewReader(res.Body)
case "deflate":
reader = flate.NewReader(res.Body)
default:
reader = res.Body
}
defer reader.Close()
response, err := ioutil.ReadAll(reader)
if err != nil && err != io.EOF {
return nil, errors.WithStack(err)
}
return response, nil
}
// func Headers(url string, headers map[string]string) (http.Header, error) {
// res, err := Request(http.MethodGet, url, nil, headers)
// if err != nil {
// return nil, errors.WithStack(err)
// }
// defer res.Body.Close()
// return res.Header, nil
// }
// func Size(url string, headers map[string]string) (int64, error) {
// h, err := Headers(url, headers)
// if err != nil {
// return 0, err
// }
// s := h.Get("Content-Length")
// if s == "" {
// return 0, errors.New("Content-Length is not present")
// }
// size, err := strconv.ParseInt(s, 10, 64)
// if err != nil {
// return 0, err
// }
// return size, nil
// }
// func ContentType(url string, headers map[string]string) (string, error) {
// h, err := Headers(url, headers)
// if err != nil {
// return "", err
// }
// s := h.Get("Content-Type")
// return strings.Split(s, ";")[0], nil
// }