-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
85 lines (75 loc) · 2.52 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package webhookrelay
import (
"net/http"
"time"
)
// Option is a functional option for configuring the API client.
type Option func(*API) error
// WithHTTPClient accepts a custom *http.Client for making API calls.
func WithHTTPClient(client *http.Client) Option {
return func(api *API) error {
api.httpClient = client
return nil
}
}
// WithAPIEndpointURL overrides default Webhook Relay API server address.
// Default: "https://my.webhookrelay.com/v1"
func WithAPIEndpointURL(apiBaseURL string) Option {
return func(api *API) error {
api.BaseURL = apiBaseURL
return nil
}
}
// WithHeaders allows you to set custom HTTP headers when making API calls (e.g. for
// satisfying HTTP proxies, or for debugging).
func WithHeaders(headers http.Header) Option {
return func(api *API) error {
api.headers = headers
return nil
}
}
// WithRetryPolicy applies a non-default number of retries and min/max retry delays
// This will be used when the client exponentially backs off after errored requests
func WithRetryPolicy(maxRetries int, minRetryDelaySecs int, maxRetryDelaySecs int) Option {
// seconds is very granular for a minimum delay - but this is only in case of failure
return func(api *API) error {
api.retryPolicy = RetryPolicy{
MaxRetries: maxRetries,
MinRetryDelay: time.Duration(minRetryDelaySecs) * time.Second,
MaxRetryDelay: time.Duration(maxRetryDelaySecs) * time.Second,
}
return nil
}
}
// WithUserAgent can be set if you want to send a software name and version for HTTP access logs.
// It is recommended to set it in order to help future Customer Support diagnostics
// and prevent collateral damage by sharing generic User-Agent string with abusive users.
// E.g. "my-software/1.2.3". By default generic Go User-Agent is used.
func WithUserAgent(userAgent string) Option {
return func(api *API) error {
api.UserAgent = userAgent
return nil
}
}
// WithLogger can be set if you want to get log output from this API instance
// By default no log output is emitted
func WithLogger(logger Logger) Option {
return func(api *API) error {
api.logger = logger
return nil
}
}
// parseOptions parses the supplied options functions and returns a configured
// *API instance.
func (api *API) 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(api)
if err != nil {
return err
}
}
return nil
}