-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttp.go
More file actions
287 lines (226 loc) · 8.44 KB
/
http.go
File metadata and controls
287 lines (226 loc) · 8.44 KB
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
287
package utils
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"math"
"net"
"net/http"
"time"
)
func httpContentTypeAndAuthorizationHeaders(contentType string, authorization string) map[string]string {
headers := make(map[string]string)
if !IsEmpty(contentType) {
headers["Content-Type"] = contentType
}
if !IsEmpty(authorization) {
headers["Authorization"] = authorization
}
return headers
}
func HttpRequestGetHeader(client *http.Client, URL string) (header map[string][]string, err error) {
resp, err := client.Head(URL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return resp.Header, nil
}
func HttpRequestRawWithHeadersOutCode(client *http.Client, method, URL string, headers map[string]string, raw []byte) (body []byte, code int, err error) {
var reader io.Reader
if raw != nil {
reader = bytes.NewReader(raw)
}
req, err := http.NewRequest(method, URL, reader)
if err != nil {
return nil, 0, err
}
for k, v := range headers {
if IsEmpty(v) {
continue
}
req.Header.Set(k, v)
}
resp, err := client.Do(req)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
var b []byte
if !IsEmpty(resp.Body) {
b, err = io.ReadAll(resp.Body)
if err != nil {
return nil, resp.StatusCode, err
}
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return b, resp.StatusCode, fmt.Errorf(resp.Status)
}
return b, resp.StatusCode, nil
}
func HttpRequestRawWithHeadersOutCodeSilent(client *http.Client, method, URL string, headers map[string]string, raw []byte) (body []byte, code int, err error) {
var reader io.Reader
if raw != nil {
reader = bytes.NewReader(raw)
}
req, err := http.NewRequest(method, URL, reader)
if err != nil {
return nil, 0, err
}
for k, v := range headers {
if IsEmpty(v) {
continue
}
req.Header.Set(k, v)
}
resp, err := client.Do(req)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
var b []byte
if !IsEmpty(resp.Body) {
b, err = io.ReadAll(resp.Body)
if err != nil {
return nil, resp.StatusCode, err
}
}
if len(b) == 0 {
return []byte(fmt.Sprintf(`{"code":%d}`, resp.StatusCode)), resp.StatusCode, nil
}
return b, resp.StatusCode, nil
}
func HttpRequestRawWithRetry(client *http.Client, method, URL string, headers map[string]string, raw []byte, maxRetries int, retryHeader string) (body []byte, code int, err error) {
var reader io.Reader
if raw != nil {
reader = bytes.NewReader(raw)
}
var b []byte
req, err := http.NewRequest(method, URL, reader)
if err != nil {
return nil, 0, err
}
for k, v := range headers {
if IsEmpty(v) {
continue
}
req.Header.Set(k, v)
}
for attempt := 0; attempt < maxRetries; attempt++ {
resp, err := client.Do(req)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
if !IsEmpty(resp.Body) {
b, err = io.ReadAll(resp.Body)
if err != nil {
return nil, resp.StatusCode, err
}
}
if resp.StatusCode == http.StatusTooManyRequests {
retryAfter := resp.Header.Get(retryHeader)
if retryAfter != "" {
duration, err := time.ParseDuration(retryAfter)
if err == nil {
time.Sleep(duration)
continue
}
}
backoff := time.Duration(math.Pow(2, float64(attempt))) * time.Second
time.Sleep(backoff)
continue
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return b, resp.StatusCode, fmt.Errorf(resp.Status)
}
return b, resp.StatusCode, nil
}
return nil, 0, fmt.Errorf("max retries exceeded")
}
func HttpRequestRawWithHeaders(client *http.Client, method, URL string, headers map[string]string, raw []byte) ([]byte, error) {
b, _, err := HttpRequestRawWithHeadersOutCode(client, method, URL, headers, raw)
return b, err
}
func HttpRequestRawWithHeadersRetry(client *http.Client, method, URL string, headers map[string]string, raw []byte, maxRetries int, retryHeader string) ([]byte, error) {
b, _, err := HttpRequestRawWithRetry(client, method, URL, headers, raw, maxRetries, retryHeader)
return b, err
}
func HttpPostRawWithHeaders(client *http.Client, URL string, headers map[string]string, raw []byte) ([]byte, error) {
return HttpRequestRawWithHeaders(client, "POST", URL, headers, raw)
}
func HttpPostRawWithHeadersRetry(client *http.Client, URL string, headers map[string]string, raw []byte, maxRetries int, retryHeader string) ([]byte, error) {
return HttpRequestRawWithHeadersRetry(client, "POST", URL, headers, raw, maxRetries, retryHeader)
}
func HttpPostRaw(client *http.Client, URL, contentType string, authorization string, raw []byte) ([]byte, error) {
headers := httpContentTypeAndAuthorizationHeaders(contentType, authorization)
return HttpPostRawWithHeaders(client, URL, headers, raw)
}
func HttpPostRawRetry(client *http.Client, URL, contentType string, authorization string, raw []byte, maxRetries int, retryHeader string) ([]byte, error) {
headers := httpContentTypeAndAuthorizationHeaders(contentType, authorization)
return HttpPostRawWithHeadersRetry(client, URL, headers, raw, maxRetries, retryHeader)
}
func HttpPostRawWithHeadersOutCode(client *http.Client, URL string, headers map[string]string, raw []byte) (body []byte, code int, err error) {
return HttpRequestRawWithHeadersOutCode(client, "POST", URL, headers, raw)
}
func HttpPostRawOutCode(client *http.Client, URL, contentType string, authorization string, raw []byte) (body []byte, code int, err error) {
headers := httpContentTypeAndAuthorizationHeaders(contentType, authorization)
return HttpPostRawWithHeadersOutCode(client, URL, headers, raw)
}
func HttpPutRawWithHeaders(client *http.Client, URL string, headers map[string]string, raw []byte) ([]byte, error) {
return HttpRequestRawWithHeaders(client, "PUT", URL, headers, raw)
}
func HttpPutRaw(client *http.Client, URL, contentType string, authorization string, raw []byte) ([]byte, error) {
headers := httpContentTypeAndAuthorizationHeaders(contentType, authorization)
return HttpPutRawWithHeaders(client, URL, headers, raw)
}
func HttpPatchRawWithHeaders(client *http.Client, URL string, headers map[string]string, raw []byte) ([]byte, error) {
return HttpRequestRawWithHeaders(client, "PATCH", URL, headers, raw)
}
func HttpPatchRaw(client *http.Client, URL, contentType string, authorization string, raw []byte) ([]byte, error) {
headers := httpContentTypeAndAuthorizationHeaders(contentType, authorization)
return HttpPatchRawWithHeaders(client, URL, headers, raw)
}
func HttpDeleteRawWithHeaders(client *http.Client, URL string, headers map[string]string, raw []byte) ([]byte, error) {
return HttpRequestRawWithHeaders(client, "DELETE", URL, headers, raw)
}
func HttpDeleteRaw(client *http.Client, URL, contentType string, authorization string, raw []byte) ([]byte, error) {
headers := httpContentTypeAndAuthorizationHeaders(contentType, authorization)
return HttpDeleteRawWithHeaders(client, URL, headers, raw)
}
func HttpGetRawWithHeaders(client *http.Client, URL string, headers map[string]string) ([]byte, error) {
return HttpRequestRawWithHeaders(client, "GET", URL, headers, nil)
}
func HttpGetRawWithHeadersRetry(client *http.Client, URL string, headers map[string]string, maxRetries int, retryHeader string) ([]byte, error) {
return HttpRequestRawWithHeadersRetry(client, "GET", URL, headers, nil, maxRetries, retryHeader)
}
func HttpGetRaw(client *http.Client, URL, contentType string, authorization string) ([]byte, error) {
headers := httpContentTypeAndAuthorizationHeaders(contentType, authorization)
return HttpGetRawWithHeaders(client, URL, headers)
}
func HttpGetRawRetry(client *http.Client, URL, contentType string, authorization string, maxRetries int, retryHeader string) ([]byte, error) {
headers := httpContentTypeAndAuthorizationHeaders(contentType, authorization)
return HttpGetRawWithHeadersRetry(client, URL, headers, maxRetries, retryHeader)
}
func HttpGetHeader(client *http.Client, URL string) (map[string][]string, error) {
return HttpRequestGetHeader(client, URL)
}
func NewHttpClient(timeout int, insecure bool) *http.Client {
var transport = &http.Transport{
Dial: (&net.Dialer{Timeout: time.Duration(timeout) * time.Second}).Dial,
TLSHandshakeTimeout: time.Duration(timeout) * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
}
var client = &http.Client{
Timeout: time.Duration(timeout) * time.Second,
Transport: transport,
}
return client
}
func NewHttpInsecureClient(timeout int) *http.Client {
return NewHttpClient(timeout, true)
}
func NewHttpSecureClient(timeout int) *http.Client {
return NewHttpClient(timeout, false)
}