-
Notifications
You must be signed in to change notification settings - Fork 21
/
route_builder.go
348 lines (289 loc) · 6.93 KB
/
route_builder.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package fb
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"strings"
"time"
)
// RouteBuilder helps building facebook API request routes.
type RouteBuilder struct {
err error
version string
path string
v url.Values
}
// NewRoute starts building a new route.
func NewRoute(version, format string, a ...interface{}) *RouteBuilder {
return &RouteBuilder{
version: version,
v: url.Values{},
path: fmt.Sprintf(format, a...),
}
}
// Fields sets the fields query param.
func (rb *RouteBuilder) Fields(f ...string) *RouteBuilder {
if len(f) > 0 {
rb.v.Set("fields", strings.Join(f, ","))
} else {
rb.v.Del("fields")
}
return rb
}
// Limit sets the limit param.
func (rb *RouteBuilder) Limit(limit int) *RouteBuilder {
if limit > -1 {
rb.v.Set("limit", strconv.Itoa(limit))
} else {
rb.v.Del("limit")
}
return rb
}
// Type sets the type param.
func (rb *RouteBuilder) Type(s string) *RouteBuilder {
if s != "" {
rb.v.Set("type", s)
} else {
rb.v.Del("type")
}
return rb
}
// Class sets the type param.
func (rb *RouteBuilder) Class(s string) *RouteBuilder {
if s != "" {
rb.v.Set("class", s)
} else {
rb.v.Del("class")
}
return rb
}
// LocationTypes sets the location_types array param.
func (rb *RouteBuilder) LocationTypes(s ...string) *RouteBuilder {
if len(s) > 0 {
rb.v.Set("location_types", fmt.Sprintf("['%s']", strings.Join(s, "','")))
} else {
rb.v.Del("location_types")
}
return rb
}
// ActionBreakdowns sets the action_breakdowns param.
func (rb *RouteBuilder) ActionBreakdowns(s string) *RouteBuilder {
if s != "" {
rb.v.Set("action_breakdowns", s)
} else {
rb.v.Del("action_breakdowns")
}
return rb
}
// Breakdowns sets the breakdowns array param.
func (rb *RouteBuilder) Breakdowns(s ...string) *RouteBuilder {
if len(s) > 0 {
rb.v.Set("breakdowns", strings.Join(s, ","))
} else {
rb.v.Del("breakdowns")
}
return rb
}
// Level sets the location_types level param.
func (rb *RouteBuilder) Level(s string) *RouteBuilder {
if s != "" {
rb.v.Set("level", s)
} else {
rb.v.Del("level")
}
return rb
}
// DailyTimeIncrement sets whether time_increment should be 1.
func (rb *RouteBuilder) DailyTimeIncrement(b bool) *RouteBuilder {
if b {
rb.v.Set("time_increment", "1")
} else {
rb.v.Del("time_increment")
}
return rb
}
// ExportFormat sets the export_format level param.
func (rb *RouteBuilder) ExportFormat(s string) *RouteBuilder {
if s != "" {
rb.v.Set("export_format", s)
} else {
rb.v.Del("export_format")
}
return rb
}
// TimeRange sets time_range param and deletes the date_preset one.
func (rb *RouteBuilder) TimeRange(minDate, maxDate time.Time) *RouteBuilder {
if minDate.IsZero() {
rb.v.Del("time_range")
} else {
rb.v.Del("date_preset")
if maxDate.IsZero() {
maxDate = time.Now()
}
b, _ := json.Marshal(TimeRange{
Since: minDate.Format("2006-01-02"),
Until: maxDate.Format("2006-01-02"),
})
rb.v.Set("time_range", string(b))
}
return rb
}
// DatePreset sets date_preset param and deletes the time_range one.
func (rb *RouteBuilder) DatePreset(s string) *RouteBuilder {
if s != "" {
rb.v.Del("time_range")
if s == "lifetime" {
s = "maximum"
}
rb.v.Set("date_preset", s)
} else {
rb.v.Del("date_preset")
}
return rb
}
// DefaultSummary sets default_summary param or deletes it.
func (rb *RouteBuilder) DefaultSummary(t bool) *RouteBuilder {
if t {
rb.v.Set("default_summary", "true")
} else {
rb.v.Del("default_summary")
}
return rb
}
// UnifiedAttributionSettings sets unified_attribution_setting param or deletes it.
func (rb *RouteBuilder) UnifiedAttributionSettings(t bool) *RouteBuilder {
if t {
rb.v.Set("use_unified_attribution_setting", "true")
} else {
rb.v.Del("use_unified_attribution_setting")
}
return rb
}
// Filtering sets filtering param or deletes it.
func (rb *RouteBuilder) Filtering(f ...Filter) *RouteBuilder {
if len(f) > 0 {
b, err := json.Marshal(f)
if err != nil {
rb.err = err
}
rb.v.Set("filtering", string(b))
} else {
rb.v.Del("filtering")
}
return rb
}
// EffectiveStatus sets the effective_status param or deletes it.
func (rb *RouteBuilder) EffectiveStatus(s ...string) *RouteBuilder {
if len(s) > 0 {
rb.v.Set("effective_status", `["`+strings.Join(s, `","`)+`"]`)
} else {
rb.v.Del("effective_status")
}
return rb
}
// AdFormat sets the ad_format param or deletes it.
func (rb *RouteBuilder) AdFormat(s string) *RouteBuilder {
if len(s) > 0 {
rb.v.Set("ad_format", s)
} else {
rb.v.Del("ad_format")
}
return rb
}
// Metadata sets the ad_format param or deletes it.
func (rb *RouteBuilder) Metadata(t bool) *RouteBuilder {
if t {
rb.v.Set("metadata", "1")
} else {
rb.v.Del("metadata")
}
return rb
}
// Order sets the order param or deletes it.
func (rb *RouteBuilder) Order(s string) *RouteBuilder {
if s != "" {
rb.v.Set("order", s)
} else {
rb.v.Del("order")
}
return rb
}
// Filter sets the filter param or deletes it.
func (rb *RouteBuilder) Filter(s string) *RouteBuilder {
if s != "" {
rb.v.Set("filter", s)
} else {
rb.v.Del("filter")
}
return rb
}
// Summary sets the summary param or deletes it.
func (rb *RouteBuilder) Summary(s string) *RouteBuilder {
if s != "" {
rb.v.Set("summary", s)
} else {
rb.v.Del("summary")
}
return rb
}
// Q sets the q param or deletes it.
func (rb *RouteBuilder) Q(s string) *RouteBuilder {
if s != "" {
rb.v.Set("q", s)
} else {
rb.v.Del("q")
}
return rb
}
// Aggregation sets the aggregation param or deletes it.
func (rb *RouteBuilder) Aggregation(s string) *RouteBuilder {
if s != "" {
rb.v.Set("aggregation", s)
} else {
rb.v.Del("aggregation")
}
return rb
}
// ActionAttributionWindows sets the action_attribution_windows param or deletes it.
func (rb *RouteBuilder) ActionAttributionWindows(s ...string) *RouteBuilder {
if len(s) > 0 {
rb.v.Set("action_attribution_windows", strings.Join(s, ","))
} else {
rb.v.Del("action_attribution_windows")
}
return rb
}
// TargetingSpec sets the action_attribution_windows param or deletes it.
func (rb *RouteBuilder) TargetingSpec(ts interface{}) *RouteBuilder {
b, _ := json.Marshal(ts)
rb.v.Set("targeting_spec", string(b))
return rb
}
// TargetingOptionList sets the targeting_option_list param or deletes it.
func (rb *RouteBuilder) TargetingOptionList(s ...string) *RouteBuilder {
if len(s) > 0 {
rb.v.Set("targeting_option_list", `["`+strings.Join(s, `","`)+`"]`)
} else {
rb.v.Del("targeting_option_list")
}
return rb
}
// String implements fmt.Stringer and returns the finished url.
func (rb *RouteBuilder) String() string {
if rb.err != nil {
return "err: " + rb.err.Error()
}
return (&url.URL{
Scheme: "https",
Host: "graph.facebook.com",
Path: "/" + rb.version + rb.path,
RawQuery: (rb.v).Encode(),
}).String()
}
// Filter is used for filtering lists.
type Filter struct {
Field string `json:"field"`
Operator string `json:"operator"`
Value interface{} `json:"value"`
}