/
adapter_impl.go
118 lines (95 loc) · 2.7 KB
/
adapter_impl.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package http
import (
"bytes"
"errors"
"net/http"
"time"
bvhttp "github.com/Updater/http"
)
// Errors that can be thrown.
var (
ErrURLNotSpecified = errors.New("The URL of the endpoint has not been specified.")
)
// client is an implementation of the ClientAdapter interface that relies
// on the core http package.
type client struct {
// The timeout specifies a time limit for requests made by a client.
// If greater than zero, the maximum amount of time to wait for a response.
// A Timeout of zero means no timeout.
timeout time.Duration
// The HTTP client pool to be used by this implementation of the
// HTTPClientAdapter interface.
pool *bvhttp.ClientPool
username string
password string
}
// Do sends a request.
func (c *client) Do(req *Request) (*Response, error) {
if req.URL == "" {
return nil, ErrURLNotSpecified
}
httpReq, err := http.NewRequest(req.Method, req.URL, bytes.NewBuffer(req.Body))
if err != nil {
return nil, err
}
httpReq.Header = req.Header
if len(c.username) > 0 {
httpReq.SetBasicAuth(c.username, c.password)
}
req.SentAt = time.Now()
pool := c.pool
if pool == nil {
pool = bvhttp.DefaultClientPool
}
httpRes, err := pool.GetClient(c.timeout).Do(httpReq)
if err != nil {
return nil, err
}
defer httpRes.Body.Close()
var buf bytes.Buffer
if _, err = buf.ReadFrom(httpRes.Body); err != nil {
return nil, err
}
resp := Response{
Body: buf.Bytes(),
Request: req,
ReceivedAt: time.Now(),
StatusCode: httpRes.StatusCode,
}
return &resp, nil
}
// Option represents a configuration function for a HTTP client.
// An Option will configure or set up internal details of a HTTP client.
type Option func(*client)
// Timeout returns a configuration function to configure the timeout of a client.
// The timeout parameter specifies a time limit for requests made by a client.
// A Timeout of zero means no timeout.
func Timeout(timeout time.Duration) Option {
return func(c *client) {
c.timeout = timeout
}
}
// Timeout returns a configuration function to configure the timeout of a client.
// The timeout parameter specifies a time limit for requests made by a client.
// A Timeout of zero means no timeout.
func BasicAuth(username string, password string) Option {
return func(c *client) {
c.username = username
c.password = password
}
}
// ClientPool returns a configuration function to configure the http client pool
// to be used by a client.
func ClientPool(pool *bvhttp.ClientPool) Option {
return func(c *client) {
c.pool = pool
}
}
// NewClientAdapter creates a new HTTP client adapter and set its initial state.
func NewClientAdapter(opts ...Option) ClientAdapter {
c := &client{}
for _, opt := range opts {
opt(c)
}
return c
}