Skip to content

Commit 828235b

Browse files
sleneasta.xie
authored and
asta.xie
committed
httplib support set transport and proxy
1 parent 430a0a9 commit 828235b

File tree

1 file changed

+50
-8
lines changed

1 file changed

+50
-8
lines changed

httplib/httplib.go

+50-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func Get(url string) *BeegoHttpRequest {
2424
req.Method = "GET"
2525
req.Header = http.Header{}
2626
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}
2828
}
2929

3030
// Post returns *BeegoHttpRequest with POST method.
@@ -33,7 +33,7 @@ func Post(url string) *BeegoHttpRequest {
3333
req.Method = "POST"
3434
req.Header = http.Header{}
3535
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}
3737
}
3838

3939
// Put returns *BeegoHttpRequest with PUT method.
@@ -42,7 +42,7 @@ func Put(url string) *BeegoHttpRequest {
4242
req.Method = "PUT"
4343
req.Header = http.Header{}
4444
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}
4646
}
4747

4848
// Delete returns *BeegoHttpRequest DELETE GET method.
@@ -51,7 +51,7 @@ func Delete(url string) *BeegoHttpRequest {
5151
req.Method = "DELETE"
5252
req.Header = http.Header{}
5353
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}
5555
}
5656

5757
// Head returns *BeegoHttpRequest with HEAD method.
@@ -60,7 +60,7 @@ func Head(url string) *BeegoHttpRequest {
6060
req.Method = "HEAD"
6161
req.Header = http.Header{}
6262
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}
6464
}
6565

6666
// BeegoHttpRequest provides more useful methods for requesting one url than http.Request.
@@ -72,6 +72,8 @@ type BeegoHttpRequest struct {
7272
connectTimeout time.Duration
7373
readWriteTimeout time.Duration
7474
tlsClientConfig *tls.Config
75+
proxy func(*http.Request) (*url.URL, error)
76+
transport http.RoundTripper
7577
}
7678

7779
// Debug sets show debug or not when executing request.
@@ -105,6 +107,24 @@ func (b *BeegoHttpRequest) SetCookie(cookie *http.Cookie) *BeegoHttpRequest {
105107
return b
106108
}
107109

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+
108128
// Param adds query param in to request.
109129
// params build query string as ?key1=value1&key2=value2...
110130
func (b *BeegoHttpRequest) Param(key, value string) *BeegoHttpRequest {
@@ -171,12 +191,34 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
171191
println(string(dump))
172192
}
173193

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{
176199
TLSClientConfig: b.tlsClientConfig,
200+
Proxy: b.proxy,
177201
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+
}
179216
}
217+
218+
client := &http.Client{
219+
Transport: trans,
220+
}
221+
180222
resp, err := client.Do(b.req)
181223
if err != nil {
182224
return nil, err

0 commit comments

Comments
 (0)