-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
98 lines (86 loc) · 2.89 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
86
87
88
89
90
91
92
93
94
95
96
97
98
package sense
import (
"net/http"
"github.com/google/uuid"
"golang.org/x/time/rate"
)
// Implement the Options pattern for New and Connect. Minimize the amount of
// state kept in this struct, since it will be re-used when needed to generate
// new clients.
type newOptions struct {
httpClient *http.Client
rateLimit *rate.Limit
apiUrl string
realtimeApiUrl string
realtimeOrigin string
internalClient internalClient
internalRealtimeClient internalRealtimeClient
deviceID string
}
// Option is a function that can be passed to New or Connect to configure the
// resulting Client.
type Option func(*newOptions)
// WithHttpClient sets the HTTP client used to make requests to the Sense API.
// If this option is not provided, http.DefaultClient will be used.
//
// This option can be useful if you need special handling for proxies or TLS
// certificates, or if you have your own approach to authenticating with Sense.
func WithHttpClient(httpClient *http.Client) Option {
return func(o *newOptions) {
o.httpClient = httpClient
}
}
// WithRateLimit applies a rate limit to requests made to the Sense API.
// Without this option, a default rate limit of 10 requests/second will be applied.
// You can disable rate limiting by providing a limit of 0.
func WithRateLimit(limit rate.Limit) Option {
return func(o *newOptions) {
o.rateLimit = &limit
}
}
// WithApiUrl sets the base URLs for the Sense API.
// If this option is not provided, the standard production API URLs will be used
// (https://api.sense.com/apiservice/api/v1/).
func WithApiUrl(apiUrl, realtimeApiUrl string) Option {
return func(o *newOptions) {
o.apiUrl = apiUrl
o.realtimeApiUrl = realtimeApiUrl
}
}
// WithInternalClient is for internal use. All other options will be ignored.
//
// Deprecated: For internal use.
func WithInternalClient(cl internalClient, acl internalRealtimeClient) Option {
return func(o *newOptions) {
o.internalClient = cl
o.internalRealtimeClient = acl
}
}
// WithDeviceID sets the X-Sense-Device-Id header on requests to the Sense API.
// This appears to be intended to be a unique identifier for the client installation.
// If this option is not provided, a random value will be generated and used for all clients for the life of this process.
// Set this value to "" explicitly to disable this header.
func WithDeviceID(id string) Option {
return func(o *newOptions) {
o.deviceID = id
}
}
func getOptions(build newOptions, opts ...Option) *newOptions {
for _, o := range opts {
o(&build)
}
// Be a little paranoid about nils
if build.httpClient == nil {
build.httpClient = defaultOptions.httpClient
}
if build.apiUrl == "" {
build.apiUrl = defaultOptions.apiUrl
}
return &build
}
var defaultOptions = newOptions{
httpClient: http.DefaultClient,
apiUrl: defaultApiRoot,
realtimeApiUrl: defaultRealtimeApiRoot,
deviceID: uuid.New().String(),
}