forked from inancgumus/myhttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
66 lines (53 loc) · 1.33 KB
/
get.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
package myhttp
import (
"net"
"net/http"
"time"
"github.com/pkg/errors"
)
const (
defaultTimeout = time.Second * 3
)
// HTTPGetter does a get request to a http endpoint
type HTTPGetter interface {
Get(url string) (*http.Response, error)
WrapGet(url string, do func(r *http.Response) error) error
}
// Getter are for implementing HTTPGetter interface
// and reserved for the future work
type Getter struct {
timeout time.Duration
client *http.Client
}
// New creates a new Getter
func New(to time.Duration) *Getter {
if to == 0 {
to = defaultTimeout
}
// http.Client.Get reuses the transport. this should be created once.
tp := http.Transport{}
tp.DialContext = (&net.Dialer{
Timeout: to,
}).DialContext
tp.TLSHandshakeTimeout = to
tp.ResponseHeaderTimeout = to
tp.ExpectContinueTimeout = to
// this should be used everytime. no need to create new one for each Get.
c := http.Client{
Transport: &tp,
}
return &Getter{client: &c}
}
// Get fetches url with a timeout
func (g *Getter) Get(url string) (*http.Response, error) {
return g.client.Get(url)
}
// WrapGet gets from `url` and closes the body automatically after running in `do`
func (g *Getter) WrapGet(url string, do func(r *http.Response) error) error {
r, err := g.Get(url)
if err != nil {
return errors.WithStack(err)
}
defer r.Body.Close()
return do(r)
}