forked from twoscott/patreon-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
67 lines (56 loc) · 1.43 KB
/
options.go
File metadata and controls
67 lines (56 loc) · 1.43 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
package patreon
import (
"strings"
)
type options struct {
fields map[string]string
filters map[string]string
include string
size int
cursor *string
maxResults int
}
type RequestOpt func(*options)
// WithFields specifies the resource attributes you want to be returned by API.
func WithFields(resource string, fields ...string) RequestOpt {
return func(o *options) {
if o.fields == nil {
o.fields = make(map[string]string)
}
o.fields[resource] = strings.Join(fields, ",")
}
}
// WithIncludes specifies the related resources you want to be returned by API.
func WithIncludes(include ...string) RequestOpt {
return func(o *options) {
o.include = strings.Join(include, ",")
}
}
// WithPageSize specifies the number of items to return.
func WithPageSize(size int) RequestOpt {
return func(o *options) {
o.size = size
}
}
// WithFilter allows you to filter results based on a specific field and value.
func WithFilter(field, value string) RequestOpt {
return func(o *options) {
if o.filters == nil {
o.filters = make(map[string]string)
}
o.filters[field] = value
}
}
// withCursor controls cursor-based pagination. Cursor will also be extracted from navigation links for convenience.
func withCursor(cursor *string) RequestOpt {
return func(o *options) {
o.cursor = cursor
}
}
func getOptions(opts ...RequestOpt) options {
cfg := options{}
for _, fn := range opts {
fn(&cfg)
}
return cfg
}