-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
wrapper_http_client.go
55 lines (45 loc) · 1.56 KB
/
wrapper_http_client.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
package fastshot
import (
"net/http"
"time"
)
// DefaultHttpClient implements HttpClientComponent interface and provides a default HTTP client.
var _ HttpClientComponent = (*DefaultHttpClient)(nil)
// DefaultHttpClient implements HttpClientComponent interface and provides a default HTTP client.
type DefaultHttpClient struct {
client *http.Client
}
// Do will execute the *http.Client Do method
func (c *DefaultHttpClient) Do(req *http.Request) (*http.Response, error) {
return c.client.Do(req)
}
// Transport will return the underlying transport type
func (c *DefaultHttpClient) Transport() http.RoundTripper {
return c.client.Transport
}
// SetTransport sets the Transport field on the underlying http.Client type
func (c *DefaultHttpClient) SetTransport(transport http.RoundTripper) {
c.client.Transport = transport
}
// Timeout will return the underlying timeout value
func (c *DefaultHttpClient) Timeout() time.Duration {
return c.client.Timeout
}
// SetTimeout sets the Timeout field on the underlying http.Client type
func (c *DefaultHttpClient) SetTimeout(duration time.Duration) {
c.client.Timeout = duration
}
// SetFollowRedirects sets the CheckRedirect field on the underlying http.Client type
func (c *DefaultHttpClient) SetFollowRedirects(follow bool) {
if !follow {
c.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
}
// newDefaultHttpClient initializes a new DefaultHttpClient.
func newDefaultHttpClient() *DefaultHttpClient {
return &DefaultHttpClient{
client: &http.Client{},
}
}