-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
312 lines (277 loc) · 9.78 KB
/
option.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package hubspot
import (
"context"
"net/url"
"strconv"
"strings"
"golang.org/x/time/rate"
"github.com/marcozac/hubspot-go/limiter"
)
type clientConfig struct {
ctx context.Context
limiter *rate.Limiter
}
type ClientOption func(*clientConfig)
// WithContext sets the context for the client.
//
// The context is used to initialize the oauth2 client, which is used to
// make requests to the HubSpot API. As stated in the [oauth2 documentation],
// the client is not valid beyond the lifetime of the context.
//
// [oauth2 documentation]: https://pkg.go.dev/golang.org/x/oauth2#NewClient
func WithContext(ctx context.Context) ClientOption {
return func(c *clientConfig) {
c.ctx = ctx
}
}
// WithLimiter sets the rate limiter for the client.
//
// The rate limiter is used to control the rate of requests made to the
// HubSpot API. If the limiter is nil, no rate limiting is applied.
//
// A small set of predefined limiters is available in the limiter package.
func WithLimiter(limiter *rate.Limiter) ClientOption {
return func(c *clientConfig) {
c.limiter = limiter
}
}
// applyClientOptions applies the given options to the given client config,
// returning the same reference if the given config is not nil. Otherwise, a
// new config is created and returned with the options applied.
func applyClientOptions(cfg *clientConfig, opts ...ClientOption) *clientConfig {
if cfg == nil {
cfg = &clientConfig{
ctx: context.Background(),
limiter: limiter.NewInf(),
}
}
for _, opt := range opts {
opt(cfg)
}
return cfg
}
// RequestConfig is a struct that contains options for making requests to the
// HubSpot API.
//
// All fields are exported only for documentation purposes. This struct is
// used exclusively as a target for the RequestOption type. Refer to the
// methods' documentation for a list of available options.
type RequestConfig struct {
// Archived is a boolean that determines whether to return only archived
// objects in the response.
Archived bool
// Properties is a list of the properties to be returned in the response.
//
// According to the HubSpot API documentation, if any of the specified
// properties are not present on the requested object(s), they will be
// ignored.
Properties []string
// PropertiesWithHistory is a list of the properties to be returned in the
// response, along with their history.
//
// According to the HubSpot API documentation, if any of the specified
// properties are not present on the requested object(s), they will be
// ignored.
PropertiesWithHistory []string
// Associations is a list of the associations to be returned in the
// response.
//
// According to the HubSpot API documentation, if any of the specified
// associations are not present on the requested object(s), they will be
// ignored.
Associations []string
// Limit is the maximum number of objects to return in the response.
//
// According to the HubSpot API documentation, the maximum value for this
// field is 100.
Limit int
// After is the paging cursor token used to get the next page of results.
After string
// IDProperty is the name of a property whose values are unique for the
// object type.
IDProperty string
}
type RequestOption func(*RequestConfig)
// WithArchived sets the archived option for the request.
//
// See [RequestConfig.Archived] for more information.
func WithArchived(archived bool) RequestOption {
return func(c *RequestConfig) {
c.Archived = archived
}
}
// WithProperties sets the properties option for the request.
//
// See [RequestConfig.Properties] for more information.
func WithProperties(properties ...string) RequestOption {
return func(c *RequestConfig) {
c.Properties = properties
}
}
// WithPropertiesWithHistory sets the properties with history option for the
// request.
//
// See [RequestConfig.PropertiesWithHistory] for more information.
func WithPropertiesWithHistory(properties ...string) RequestOption {
return func(c *RequestConfig) {
c.PropertiesWithHistory = properties
}
}
// WithAssociations sets the associations option for the request.
//
// See [RequestConfig.Associations] for more information.
func WithAssociations(associations ...string) RequestOption {
return func(c *RequestConfig) {
c.Associations = associations
}
}
// WithLimit sets the limit option for the request.
//
// See [RequestConfig.Limit] for more information.
func WithLimit(limit int) RequestOption {
return func(c *RequestConfig) {
c.Limit = limit
}
}
// WithAfter sets the after option for the request.
//
// See [RequestConfig.After] for more information.
func WithAfter(after string) RequestOption {
return func(c *RequestConfig) {
c.After = after
}
}
// WithIDProperty sets the ID property option for the request.
//
// See [RequestConfig.IDProperty] for more information.
func WithIDProperty(name string) RequestOption {
return func(c *RequestConfig) {
c.IDProperty = name
}
}
// applyRequestOptions applies the given options to the given request config,
// returning the same reference if the given config is not nil. Otherwise, a
// new config is created and returned with the options applied.
func applyRequestOptions(cfg *RequestConfig, opts ...RequestOption) *RequestConfig {
if cfg == nil {
cfg = &RequestConfig{}
}
for _, opt := range opts {
opt(cfg)
}
return cfg
}
const (
QueryKeyArchived = "archived"
QueryKeyProperties = "properties"
QueryKeyPropertiesWithHistory = "propertiesWithHistory"
QueryKeyAssociations = "associations"
QueryKeyLimit = "limit"
QueryKeyAfter = "after"
QueryKeyIDProperty = "idProperty"
)
// queryOption is a function that applies a query parameter to the given
// url.Values.
type queryOption func(cfg *RequestConfig, query url.Values)
// applyQueryOptions applies the given options to the given request config,
// setting also (overwriting) the url.RawQuery field with the resulting
// query string.
//
// NOTE: this function performs a write operation on the given url.Values
// (map), so it is not safe to call concurrently.
func applyQueryOptions(cfg *RequestConfig, url *url.URL, opts ...queryOption) {
q := url.Query()
for _, opt := range opts {
opt(cfg, q)
}
url.RawQuery = getRAWQuery(q)
}
func getRAWQuery(q url.Values) string {
var sb strings.Builder
for k, v := range q {
sb.WriteString(k)
sb.WriteString("=")
sb.WriteString(strings.Join(v, ","))
sb.WriteString("&")
}
return strings.TrimSuffix(sb.String(), "&")
}
// applyArchived checks the archived field of the given request config and
// adds the appropriate query parameter to the given url.Values. It always
// adds the "archived" query parameter.
//
// NOTE: this function performs a write operation on the given url.Values
// (map), so it is not safe to call concurrently.
func applyArchivedQuery(cfg *RequestConfig, query url.Values) {
if cfg.Archived {
query.Set(QueryKeyArchived, "true")
} else {
query.Set(QueryKeyArchived, "false")
}
}
// applyPropertiesOption checks the properties field of the given request
// config and adds the appropriate query parameter to the given url.Values.
// If the cfg.Properties field is empty, this function does nothing.
//
// NOTE: this function performs a write operation on the given url.Values
// (map), so it is not safe to call concurrently.
func applyPropertiesQuery(cfg *RequestConfig, query url.Values) {
for _, p := range cfg.Properties {
query.Add(QueryKeyProperties, p)
}
}
// applyPropertiesWithHistoryOption checks the properties with history field of the given request
// config and adds the appropriate query parameter to the given url.Values.
// If the cfg.PropertiesWithHistory field is empty, this function does nothing.
//
// NOTE: this function performs a write operation on the given url.Values
// (map), so it is not safe to call concurrently.
func applyPropertiesWithHistoryQuery(cfg *RequestConfig, query url.Values) {
for _, p := range cfg.PropertiesWithHistory {
query.Add(QueryKeyPropertiesWithHistory, p)
}
}
// applyAssociationsOption checks the associations field of the given request
// config and adds the appropriate query parameter to the given url.Values.
// If the cfg.Associations field is empty, this function does nothing.
//
// NOTE: this function performs a write operation on the given url.Values
// (map), so it is not safe to call concurrently.
func applyAssociationsQuery(cfg *RequestConfig, query url.Values) {
for _, a := range cfg.Associations {
query.Add(QueryKeyAssociations, a)
}
}
// applyLimitOption checks the limit field of the given request
// config and adds the appropriate query parameter to the given url.Values.
// If the cfg.Limit field is 0, this function does nothing.
//
// NOTE: this function performs a write operation on the given url.Values
// (map), so it is not safe to call concurrently.
func applyLimitQuery(cfg *RequestConfig, query url.Values) {
if cfg.Limit > 0 {
query.Set(QueryKeyLimit, strconv.Itoa(cfg.Limit))
}
}
// applyAfterOption checks the after field of the given request
// config and adds the appropriate query parameter to the given url.Values.
// If the cfg.After field is empty, this function does nothing.
//
// NOTE: this function performs a write operation on the given url.Values
// (map), so it is not safe to call concurrently.
func applyAfterQuery(cfg *RequestConfig, query url.Values) {
if cfg.After != "" {
query.Set(QueryKeyAfter, cfg.After)
}
}
// applyIDPropertyQuery checks the IDProperty field of the given request
// config and adds the appropriate query parameter to the given url.Values.
// If the cfg.IDProperty field is empty, this function does nothing.
//
// NOTE: this function performs a write operation on the given url.Values
// (map), so it is not safe to call concurrently.
func applyIDPropertyQuery(cfg *RequestConfig, query url.Values) {
if cfg.IDProperty != "" {
query.Set(QueryKeyIDProperty, cfg.IDProperty)
}
}