-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathoptions.go
More file actions
173 lines (149 loc) · 3.57 KB
/
options.go
File metadata and controls
173 lines (149 loc) · 3.57 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package customerio
import (
"fmt"
"net/url"
"time"
)
// Option configures Customer.io API and Track clients.
type Option interface {
applyAPI(*APIClient)
applyTrack(*CustomerIO)
}
type option struct {
api func(*APIClient)
track func(*CustomerIO)
}
func (o option) applyAPI(a *APIClient) {
if o.api != nil {
o.api(a)
}
}
func (o option) applyTrack(c *CustomerIO) {
if o.track != nil {
o.track(c)
}
}
// Region configures the Customer.io API endpoints for a workspace region.
type Region string
const (
// RegionUS configures clients for Customer.io US endpoints.
RegionUS Region = "us"
// RegionEU configures clients for Customer.io EU endpoints.
RegionEU Region = "eu"
)
func (r Region) APIURL() string {
if r == RegionEU {
return "https://api-eu.customer.io"
}
return "https://api.customer.io"
}
func (r Region) TrackURL() string {
if r == RegionEU {
return "https://track-eu.customer.io"
}
return "https://track.customer.io"
}
func WithRegion(r Region) Option {
switch r {
case RegionUS, RegionEU:
default:
panic(fmt.Sprintf("customerio: unknown region %q", r))
}
return option{
api: func(a *APIClient) {
a.URL = r.APIURL()
},
track: func(c *CustomerIO) {
c.URL = r.TrackURL()
},
}
}
func WithHTTPClient(client HTTPClient) Option {
if client == nil {
panic("customerio: WithHTTPClient called with nil HTTPClient")
}
return option{
api: func(a *APIClient) {
a.Client = client
},
track: func(c *CustomerIO) {
c.Client = client
},
}
}
// WithURL overrides the base URL for API requests. Most callers should use
// WithRegion instead; this is intended for tests or on-premise deployments.
func WithURL(url string) Option {
if url == "" {
panic("customerio: WithURL called with empty string")
}
return option{
api: func(a *APIClient) {
a.URL = url
},
track: func(c *CustomerIO) {
c.URL = url
},
}
}
func WithUserAgent(ua string) Option {
if ua == "" {
panic("customerio: WithUserAgent called with empty string")
}
return option{
api: func(a *APIClient) {
a.UserAgent = ua
},
track: func(c *CustomerIO) {
c.UserAgent = ua
},
}
}
// TrackOption sets optional top-level fields on tracked events.
type TrackOption func(map[string]any)
// TrackType is the type of event being tracked.
type TrackType string
const (
TrackTypeEvent TrackType = "event"
TrackTypePage TrackType = "page"
TrackTypeScreen TrackType = "screen"
)
// WithEventID sets the id field on tracked events.
func WithEventID(id string) TrackOption {
return func(payload map[string]any) {
payload["id"] = id
}
}
// WithEventTimestamp sets the timestamp field on tracked events.
func WithEventTimestamp(timestamp time.Time) TrackOption {
return func(payload map[string]any) {
payload["timestamp"] = timestamp.Unix()
}
}
// WithEventType sets the type field on tracked events.
func WithEventType(typ TrackType) TrackOption {
return func(payload map[string]any) {
payload["type"] = typ
}
}
func trackPayload(eventName string, data map[string]any, opts ...TrackOption) map[string]any {
payload := map[string]any{
"name": eventName,
"data": data,
}
for _, opt := range opts {
if opt != nil {
opt(payload)
}
}
return payload
}
// SegmentOption configures optional query parameters on segment membership requests.
type SegmentOption func(url.Values)
// WithSegmentIDType sets the id_type query parameter, identifying which kind of
// identifier the supplied ids represent. Defaults server-side to IdentifierTypeID.
func WithSegmentIDType(t IdentifierType) SegmentOption {
return func(v url.Values) {
v.Set("id_type", string(t))
}
}