-
Notifications
You must be signed in to change notification settings - Fork 9
/
options.go
64 lines (56 loc) · 1.29 KB
/
options.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
package jsonrpc
import (
"net/http"
"time"
)
type Option interface {
apply(*client) error
}
func NewClientWithOptions(addr string, options ...Option) (Client, error) {
c := &client{
addr: addr,
}
for _, o := range options {
err := o.apply(c)
if err != nil {
return nil, err
}
}
return c, nil
}
type optionFunc func(*client) error
func (o optionFunc) apply(c *client) error {
return o(c)
}
// HTTPClient sets the http.Client on the Client for the connection.
// If RoundTripper was previously specified, this function will override
// the transport.
func HTTPClient(hc *http.Client) optionFunc {
return optionFunc(func(c *client) error {
c.http = hc
return nil
})
}
// RoundTripper sets the Transport on the http.Client if the http client has
// already been specified, this overrides the transport. If not, we create a
// new client.
func RoundTripper(rt http.RoundTripper) optionFunc {
return optionFunc(func(c *client) error {
if c.http == nil {
c.http = &http.Client{
Transport: rt,
Timeout: 10 * time.Minute,
}
} else {
c.http.Transport = rt
}
return nil
})
}
// UserAgent sets the User-Agent header on outgoing requests.
func UserAgent(userAgent string) optionFunc {
return optionFunc(func(c *client) error {
c.userAgent = userAgent
return nil
})
}