-
Notifications
You must be signed in to change notification settings - Fork 0
/
gohttp.go
225 lines (192 loc) · 7.38 KB
/
gohttp.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
package gohttp
import (
"context"
"net/http"
"time"
)
var (
defaultAgent = "Go-HTTP-Client"
defaultSession = newSession(http.DefaultClient)
)
// H represents the key-value pairs in an HTTP header.
type H map[string]string
func defaultHeaders() H {
return H{
"User-Agent": defaultAgent,
"Accept-Encoding": "gzip, deflate",
"Accept": "*/*",
"Connection": "keep-alive",
}
}
// SetAgent sets default user agent string.
func SetAgent(agent string) {
if agent != "" {
defaultAgent = agent
}
}
// SetProxy sets default client transport proxy.
func SetProxy(proxy string) error {
return defaultSession.SetProxy(proxy)
}
// SetNoProxy sets default client use no proxy.
func SetNoProxy() {
defaultSession.SetNoProxy()
}
// SetProxyFromEnvironment sets default client use environment proxy.
func SetProxyFromEnvironment() {
defaultSession.SetProxyFromEnvironment()
}
// SetTimeout sets default timeout. Zero means no timeout.
func SetTimeout(d time.Duration) {
defaultSession.SetTimeout(d)
}
// SetClient sets default client.
func SetClient(c *http.Client) {
defaultSession.SetClient(c)
}
// Do sends an HTTP request and returns a response.
func Do(req *http.Request) (*Response, error) {
return defaultSession.Do(req)
}
// Get issues a GET to the specified URL with headers.
func Get(url string, headers H) (*Response, error) {
return defaultSession.Get(url, headers)
}
// GetWithContext issues a GET to the specified URL with context and headers.
func GetWithContext(ctx context.Context, url string, headers H) (*Response, error) {
return defaultSession.GetWithContext(ctx, url, headers)
}
// Head issues a HEAD to the specified URL with headers.
func Head(url string, headers H) (*Response, error) {
return defaultSession.Head(url, headers)
}
// HeadWithContext issues a HEAD to the specified URL with context and headers.
func HeadWithContext(ctx context.Context, url string, headers H) (*Response, error) {
return defaultSession.HeadWithContext(ctx, url, headers)
}
// Post issues a POST to the specified URL with headers.
// Post data should be one of nil, io.Reader, url.Values, string map or struct.
func Post(url string, headers H, data any) (*Response, error) {
return defaultSession.Post(url, headers, data)
}
// PostWithContext issues a POST to the specified URL with context and headers.
// Post data should be one of nil, io.Reader, url.Values, string map or struct.
func PostWithContext(ctx context.Context, url string, headers H, data any) (*Response, error) {
return defaultSession.PostWithContext(ctx, url, headers, data)
}
// Upload issues a POST to the specified URL with a multipart document.
func Upload(url string, headers H, params map[string]string, files ...*File) (*Response, error) {
return defaultSession.Upload(url, headers, params, files...)
}
// UploadWithContext issues a POST to the specified URL with context and a multipart document.
func UploadWithContext(ctx context.Context, url string, headers H, params map[string]string, files ...*File) (*Response, error) {
return defaultSession.UploadWithContext(ctx, url, headers, params, files...)
}
// Do sends a session HTTP request and returns a response.
func (s *Session) Do(req *http.Request) (*Response, error) {
req = req.Clone(req.Context())
header := make(http.Header)
for k, v := range defaultHeaders() {
header.Set(k, v)
}
for k, v := range s.Header {
header[k] = v
}
for k, v := range req.Header {
header[k] = v
}
req.Header = header
resp, err := s.client.Do(req)
if err != nil {
return nil, err
}
return buildResponse(resp)
}
// Get issues a session GET to the specified URL with additional headers.
func (s *Session) Get(url string, headers H) (*Response, error) {
return s.GetWithContext(context.Background(), url, headers)
}
// GetWithContext issues a session GET to the specified URL with context and additional headers.
func (s *Session) GetWithContext(ctx context.Context, url string, headers H) (*Response, error) {
req, err := newRequest(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
for k, v := range headers {
req.Header.Set(k, v)
}
return s.Do(req)
}
// Head issues a session HEAD to the specified URL with additional headers.
func (s *Session) Head(url string, headers H) (*Response, error) {
return s.HeadWithContext(context.Background(), url, headers)
}
// HeadWithContext issues a session HEAD to the specified URL with context and additional headers.
func (s *Session) HeadWithContext(ctx context.Context, url string, headers H) (*Response, error) {
req, err := newRequest(ctx, "HEAD", url, nil)
if err != nil {
return nil, err
}
for k, v := range headers {
req.Header.Set(k, v)
}
return s.Do(req)
}
// Post issues a session POST to the specified URL with additional headers.
func (s *Session) Post(url string, headers H, data any) (*Response, error) {
return s.PostWithContext(context.Background(), url, headers, data)
}
// PostWithContext issues a session POST to the specified URL with context and additional headers.
func (s *Session) PostWithContext(ctx context.Context, url string, headers H, data any) (*Response, error) {
req, err := newRequest(ctx, "POST", url, data)
if err != nil {
return nil, err
}
for k, v := range headers {
req.Header.Set(k, v)
}
return s.Do(req)
}
// Upload issues a session POST to the specified URL with a multipart document and additional headers.
func (s *Session) Upload(url string, headers H, params map[string]string, files ...*File) (*Response, error) {
return s.UploadWithContext(context.Background(), url, headers, params, files...)
}
// UploadWithContext issues a session POST to the specified URL with context, a multipart document and additional headers.
func (s *Session) UploadWithContext(ctx context.Context, url string, headers H, params map[string]string, files ...*File) (*Response, error) {
data, contentType, err := buildMultipart(params, files...)
if err != nil {
return nil, err
}
req, err := newRequest(ctx, "POST", url, data)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
for k, v := range headers {
req.Header.Set(k, v)
}
return s.Do(req)
}
// KeepAlive repeatedly calls fn with a fixed interval delay between each call.
func (s *Session) KeepAlive(interval *time.Duration, fn func(*Session) error) (err error) {
for ; err == nil; <-time.After(*interval) {
err = fn(s)
}
return
}
// GetWithClient issues a GET to the specified URL with context, headers and client.
func GetWithClient(ctx context.Context, url string, headers H, client *http.Client) (*Response, error) {
return newSession(client).GetWithContext(ctx, url, headers)
}
// HeadWithClient issues a HEAD to the specified URL with context, headers and client.
func HeadWithClient(ctx context.Context, url string, headers H, client *http.Client) (*Response, error) {
return newSession(client).HeadWithContext(ctx, url, headers)
}
// PostWithClient issues a POST to the specified URL with context, headers and client.
func PostWithClient(ctx context.Context, url string, headers H, data any, client *http.Client) (*Response, error) {
return newSession(client).PostWithContext(ctx, url, headers, data)
}
// UploadWithClient issues a POST to the specified URL with context, a multipart document and client.
func UploadWithClient(ctx context.Context, url string, headers H, params map[string]string, files []*File, client *http.Client) (*Response, error) {
return newSession(client).UploadWithContext(ctx, url, headers, params, files...)
}