httplib support set transport and proxy
Showing
1 changed file
with
50 additions
and
8 deletions
| ... | @@ -24,7 +24,7 @@ func Get(url string) *BeegoHttpRequest { | ... | @@ -24,7 +24,7 @@ func Get(url string) *BeegoHttpRequest { |
| 24 | req.Method = "GET" | 24 | req.Method = "GET" |
| 25 | req.Header = http.Header{} | 25 | req.Header = http.Header{} |
| 26 | req.Header.Set("User-Agent", defaultUserAgent) | 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 | // Post returns *BeegoHttpRequest with POST method. | 30 | // Post returns *BeegoHttpRequest with POST method. |
| ... | @@ -33,7 +33,7 @@ func Post(url string) *BeegoHttpRequest { | ... | @@ -33,7 +33,7 @@ func Post(url string) *BeegoHttpRequest { |
| 33 | req.Method = "POST" | 33 | req.Method = "POST" |
| 34 | req.Header = http.Header{} | 34 | req.Header = http.Header{} |
| 35 | req.Header.Set("User-Agent", defaultUserAgent) | 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 | // Put returns *BeegoHttpRequest with PUT method. | 39 | // Put returns *BeegoHttpRequest with PUT method. |
| ... | @@ -42,7 +42,7 @@ func Put(url string) *BeegoHttpRequest { | ... | @@ -42,7 +42,7 @@ func Put(url string) *BeegoHttpRequest { |
| 42 | req.Method = "PUT" | 42 | req.Method = "PUT" |
| 43 | req.Header = http.Header{} | 43 | req.Header = http.Header{} |
| 44 | req.Header.Set("User-Agent", defaultUserAgent) | 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 | // Delete returns *BeegoHttpRequest DELETE GET method. | 48 | // Delete returns *BeegoHttpRequest DELETE GET method. |
| ... | @@ -51,7 +51,7 @@ func Delete(url string) *BeegoHttpRequest { | ... | @@ -51,7 +51,7 @@ func Delete(url string) *BeegoHttpRequest { |
| 51 | req.Method = "DELETE" | 51 | req.Method = "DELETE" |
| 52 | req.Header = http.Header{} | 52 | req.Header = http.Header{} |
| 53 | req.Header.Set("User-Agent", defaultUserAgent) | 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 | // Head returns *BeegoHttpRequest with HEAD method. | 57 | // Head returns *BeegoHttpRequest with HEAD method. |
| ... | @@ -60,7 +60,7 @@ func Head(url string) *BeegoHttpRequest { | ... | @@ -60,7 +60,7 @@ func Head(url string) *BeegoHttpRequest { |
| 60 | req.Method = "HEAD" | 60 | req.Method = "HEAD" |
| 61 | req.Header = http.Header{} | 61 | req.Header = http.Header{} |
| 62 | req.Header.Set("User-Agent", defaultUserAgent) | 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 | // BeegoHttpRequest provides more useful methods for requesting one url than http.Request. | 66 | // BeegoHttpRequest provides more useful methods for requesting one url than http.Request. |
| ... | @@ -72,6 +72,8 @@ type BeegoHttpRequest struct { | ... | @@ -72,6 +72,8 @@ type BeegoHttpRequest struct { |
| 72 | connectTimeout time.Duration | 72 | connectTimeout time.Duration |
| 73 | readWriteTimeout time.Duration | 73 | readWriteTimeout time.Duration |
| 74 | tlsClientConfig *tls.Config | 74 | tlsClientConfig *tls.Config |
| 75 | proxy func(*http.Request) (*url.URL, error) | ||
| 76 | transport http.RoundTripper | ||
| 75 | } | 77 | } |
| 76 | 78 | ||
| 77 | // Debug sets show debug or not when executing request. | 79 | // Debug sets show debug or not when executing request. |
| ... | @@ -105,6 +107,24 @@ func (b *BeegoHttpRequest) SetCookie(cookie *http.Cookie) *BeegoHttpRequest { | ... | @@ -105,6 +107,24 @@ func (b *BeegoHttpRequest) SetCookie(cookie *http.Cookie) *BeegoHttpRequest { |
| 105 | return b | 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 | // Param adds query param in to request. | 128 | // Param adds query param in to request. |
| 109 | // params build query string as ?key1=value1&key2=value2... | 129 | // params build query string as ?key1=value1&key2=value2... |
| 110 | func (b *BeegoHttpRequest) Param(key, value string) *BeegoHttpRequest { | 130 | func (b *BeegoHttpRequest) Param(key, value string) *BeegoHttpRequest { |
| ... | @@ -171,12 +191,34 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { | ... | @@ -171,12 +191,34 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { |
| 171 | println(string(dump)) | 191 | println(string(dump)) |
| 172 | } | 192 | } |
| 173 | 193 | ||
| 174 | client := &http.Client{ | 194 | trans := b.transport |
| 175 | Transport: &http.Transport{ | 195 | |
| 196 | if trans == nil { | ||
| 197 | // create default transport | ||
| 198 | trans = &http.Transport{ | ||
| 176 | TLSClientConfig: b.tlsClientConfig, | 199 | TLSClientConfig: b.tlsClientConfig, |
| 200 | Proxy: b.proxy, | ||
| 177 | Dial: TimeoutDialer(b.connectTimeout, b.readWriteTimeout), | 201 | Dial: TimeoutDialer(b.connectTimeout, b.readWriteTimeout), |
| 178 | }, | ||
| 179 | } | 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 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | client := &http.Client{ | ||
| 219 | Transport: trans, | ||
| 220 | } | ||
| 221 | |||
| 180 | resp, err := client.Do(b.req) | 222 | resp, err := client.Do(b.req) |
| 181 | if err != nil { | 223 | if err != nil { |
| 182 | return nil, err | 224 | return nil, err | ... | ... |
-
Please register or sign in to post a comment