-
Notifications
You must be signed in to change notification settings - Fork 4
/
options.go
55 lines (45 loc) · 994 Bytes
/
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
package nationbuilder
import (
"net/url"
"strconv"
)
// Options describes available options for the API
// In particular the limit and pagination options for paginated resource endpoints
type Options struct {
Limit int
PageToken string
PageNonce string
queryOpts url.Values
}
// Set arbitrary query options
func (o *Options) SetQueryOption(key string, value string) {
if o.queryOpts == nil {
o.queryOpts = make(url.Values)
}
o.queryOpts.Set(key, value)
}
func (o *Options) setQuery(u *url.URL) {
q := u.Query()
if o.queryOpts == nil {
o.queryOpts = make(url.Values)
}
for k, v := range o.queryOpts {
for _, s := range v {
q.Add(k, s)
}
}
if o.Limit != 0 {
q.Set("limit", strconv.Itoa(o.Limit))
}
if o.PageToken != "" && o.PageNonce != "" {
q.Set("__token", o.PageToken)
q.Set("__nonce", o.PageNonce)
}
u.RawQuery = q.Encode()
}
// Instantiate an Options object
func NewOptions() *Options {
return &Options{
queryOpts: make(url.Values),
}
}