-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathoptions.go
65 lines (56 loc) · 1.48 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
65
package codeship
import (
"net/http"
)
// Option is a functional option for configuring the API client
type Option func(*Client) error
// HTTPClient accepts a custom *http.Client for making API calls
func HTTPClient(client *http.Client) Option {
return func(c *Client) error {
c.httpClient = client
return nil
}
}
// Headers allows you to set custom HTTP headers when making API calls (e.g. for
// satisfying HTTP proxies, or for debugging)
func Headers(headers http.Header) Option {
return func(c *Client) error {
c.headers = headers
return nil
}
}
// BaseURL allows overriding of API client baseURL for testing
func BaseURL(baseURL string) Option {
return func(c *Client) error {
c.baseURL = baseURL
return nil
}
}
// Logger allows overriding the default STDOUT logger
func Logger(logger StdLogger) Option {
return func(c *Client) error {
c.logger = logger
return nil
}
}
// Verbose allows enabling/disabling internal logging
func Verbose(verbose bool) Option {
return func(c *Client) error {
c.verbose = verbose
return nil
}
}
// parseOptions parses the supplied options functions and returns a configured
// *Client instance
func (c *Client) parseOptions(opts ...Option) error {
// Range over each options function and apply it to our API type to
// configure it. Options functions are applied in order, with any
// conflicting options overriding earlier calls.
for _, option := range opts {
err := option(c)
if err != nil {
return err
}
}
return nil
}