/
list.go
90 lines (80 loc) · 1.94 KB
/
list.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
package kong
import (
"bytes"
"context"
"encoding/json"
)
// ListOpt aids in paginating through list endpoints
type ListOpt struct {
// Size of the page
Size int `url:"size,omitempty"`
// Offset for the current page
Offset string `url:"offset,omitempty"`
// Tags to use for filtering the list.
Tags []*string `url:"tags,omitempty"`
// Tags are ORed by default, meaning entities
// containing even a single tag in the list are listed.
// If true, tags are ANDed, meaning only entities
// matching each tag in the Tags array are listed.
MatchAllTags bool
}
// qs is used to construct query string for list endpoints
type qs struct {
Size int `url:"size,omitempty"`
Offset string `url:"offset,omitempty"`
Tags string `url:"tags,omitempty"`
}
// list fetches a list of an entity in Kong.
// opt can be used to control pagination.
func (c *Client) list(ctx context.Context,
endpoint string, opt *ListOpt,
) ([]json.RawMessage, *ListOpt, error) {
q := constructQueryString(opt)
req, err := c.NewRequest("GET", endpoint, &q, nil)
if err != nil {
return nil, nil, err
}
var list struct {
Data []json.RawMessage `json:"data"`
Next *string `json:"offset"`
}
_, err = c.Do(ctx, req, &list)
if err != nil {
return nil, nil, err
}
// convinient for end user to use this opt till it's nil
var next *ListOpt
if list.Next != nil {
next = &ListOpt{
Offset: *list.Next,
}
if opt != nil && next != nil {
next.Size = opt.Size
next.Tags = opt.Tags
next.MatchAllTags = opt.MatchAllTags
}
}
return list.Data, next, nil
}
func constructQueryString(opt *ListOpt) qs {
var q qs
if opt == nil {
return q
}
q.Size = opt.Size
q.Offset = opt.Offset
var tagQS bytes.Buffer
tagCount := len(opt.Tags)
for i := 0; i < tagCount; i++ {
tagQS.WriteString(*opt.Tags[i])
if i+1 < tagCount {
if opt.MatchAllTags {
tagQS.WriteByte(',')
} else {
tagQS.WriteByte('/')
}
}
}
q.Tags = tagQS.String()
return q
}