@@ -24,7 +24,7 @@ func Get(url string) *BeegoHttpRequest {
24
24
req .Method = "GET"
25
25
req .Header = http.Header {}
26
26
req .Header .Set ("User-Agent" , defaultUserAgent )
27
- return & BeegoHttpRequest {url , & req , map [string ]string {}, false , 60 * time .Second , 60 * time .Second , nil }
27
+ return & BeegoHttpRequest {url , & req , map [string ]string {}, false , 60 * time .Second , 60 * time .Second , nil , nil , nil }
28
28
}
29
29
30
30
// Post returns *BeegoHttpRequest with POST method.
@@ -33,7 +33,7 @@ func Post(url string) *BeegoHttpRequest {
33
33
req .Method = "POST"
34
34
req .Header = http.Header {}
35
35
req .Header .Set ("User-Agent" , defaultUserAgent )
36
- return & BeegoHttpRequest {url , & req , map [string ]string {}, false , 60 * time .Second , 60 * time .Second , nil }
36
+ return & BeegoHttpRequest {url , & req , map [string ]string {}, false , 60 * time .Second , 60 * time .Second , nil , nil , nil }
37
37
}
38
38
39
39
// Put returns *BeegoHttpRequest with PUT method.
@@ -42,7 +42,7 @@ func Put(url string) *BeegoHttpRequest {
42
42
req .Method = "PUT"
43
43
req .Header = http.Header {}
44
44
req .Header .Set ("User-Agent" , defaultUserAgent )
45
- return & BeegoHttpRequest {url , & req , map [string ]string {}, false , 60 * time .Second , 60 * time .Second , nil }
45
+ return & BeegoHttpRequest {url , & req , map [string ]string {}, false , 60 * time .Second , 60 * time .Second , nil , nil , nil }
46
46
}
47
47
48
48
// Delete returns *BeegoHttpRequest DELETE GET method.
@@ -51,7 +51,7 @@ func Delete(url string) *BeegoHttpRequest {
51
51
req .Method = "DELETE"
52
52
req .Header = http.Header {}
53
53
req .Header .Set ("User-Agent" , defaultUserAgent )
54
- return & BeegoHttpRequest {url , & req , map [string ]string {}, false , 60 * time .Second , 60 * time .Second , nil }
54
+ return & BeegoHttpRequest {url , & req , map [string ]string {}, false , 60 * time .Second , 60 * time .Second , nil , nil , nil }
55
55
}
56
56
57
57
// Head returns *BeegoHttpRequest with HEAD method.
@@ -60,7 +60,7 @@ func Head(url string) *BeegoHttpRequest {
60
60
req .Method = "HEAD"
61
61
req .Header = http.Header {}
62
62
req .Header .Set ("User-Agent" , defaultUserAgent )
63
- return & BeegoHttpRequest {url , & req , map [string ]string {}, false , 60 * time .Second , 60 * time .Second , nil }
63
+ return & BeegoHttpRequest {url , & req , map [string ]string {}, false , 60 * time .Second , 60 * time .Second , nil , nil , nil }
64
64
}
65
65
66
66
// BeegoHttpRequest provides more useful methods for requesting one url than http.Request.
@@ -72,6 +72,8 @@ type BeegoHttpRequest struct {
72
72
connectTimeout time.Duration
73
73
readWriteTimeout time.Duration
74
74
tlsClientConfig * tls.Config
75
+ proxy func (* http.Request ) (* url.URL , error )
76
+ transport http.RoundTripper
75
77
}
76
78
77
79
// Debug sets show debug or not when executing request.
@@ -105,6 +107,24 @@ func (b *BeegoHttpRequest) SetCookie(cookie *http.Cookie) *BeegoHttpRequest {
105
107
return b
106
108
}
107
109
110
+ // Set transport to
111
+ func (b * BeegoHttpRequest ) SetTransport (transport http.RoundTripper ) * BeegoHttpRequest {
112
+ b .transport = transport
113
+ return b
114
+ }
115
+
116
+ // Set http proxy
117
+ // example:
118
+ //
119
+ // func(req *http.Request) (*url.URL, error) {
120
+ // u, _ := url.ParseRequestURI("http://127.0.0.1:8118")
121
+ // return u, nil
122
+ // }
123
+ func (b * BeegoHttpRequest ) SetProxy (proxy func (* http.Request ) (* url.URL , error )) * BeegoHttpRequest {
124
+ b .proxy = proxy
125
+ return b
126
+ }
127
+
108
128
// Param adds query param in to request.
109
129
// params build query string as ?key1=value1&key2=value2...
110
130
func (b * BeegoHttpRequest ) Param (key , value string ) * BeegoHttpRequest {
@@ -171,12 +191,34 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
171
191
println (string (dump ))
172
192
}
173
193
174
- client := & http.Client {
175
- Transport : & http.Transport {
194
+ trans := b .transport
195
+
196
+ if trans == nil {
197
+ // create default transport
198
+ trans = & http.Transport {
176
199
TLSClientConfig : b .tlsClientConfig ,
200
+ Proxy : b .proxy ,
177
201
Dial : TimeoutDialer (b .connectTimeout , b .readWriteTimeout ),
178
- },
202
+ }
203
+ } else {
204
+ // if b.transport is *http.Transport then set the settings.
205
+ if t , ok := trans .(* http.Transport ); ok {
206
+ if t .TLSClientConfig == nil {
207
+ t .TLSClientConfig = b .tlsClientConfig
208
+ }
209
+ if t .Proxy == nil {
210
+ t .Proxy = b .proxy
211
+ }
212
+ if t .Dial == nil {
213
+ t .Dial = TimeoutDialer (b .connectTimeout , b .readWriteTimeout )
214
+ }
215
+ }
179
216
}
217
+
218
+ client := & http.Client {
219
+ Transport : trans ,
220
+ }
221
+
180
222
resp , err := client .Do (b .req )
181
223
if err != nil {
182
224
return nil , err
0 commit comments