/
consumers.go
148 lines (124 loc) · 4.32 KB
/
consumers.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
package kong
import (
"errors"
"fmt"
"net/http"
)
// ConsumersService handles communication with Kong's '/consumers' resource.
type ConsumersService struct {
*service
Plugins *ConsumersPlugins
}
// Consumers represents the object returned from Kong when querying for multiple consumer objects.
//
// In cases where the number of objects returned exceeds the maximum, Next holds the
// URI for the next set of results.
// i.e. "http://localhost:8001/consumers/?size=2&offset=4d924084-1adb-40a5-c042-63b19db421d1"
type Consumers struct {
Data []*Consumer `json:"data,omitempty"`
Total int `json:"total,omitempty"`
Next string `json:"next,omitempty"`
}
// Consumer represents a single Kong consumer object
type Consumer struct {
ID string `json:"id,omitempty"`
Username string `json:"username,omitempty"`
CustomID string `json:"custom_id,omitempty"`
CreatedAt int `json:"created_at,omitempty"`
}
// ConsumersService.Get queries for a single Kong consumer object, by name or id.
//
// Equivalent to GET /consumers/{name or id}
func (s *ConsumersService) Get(consumer string) (*Consumer, *http.Response, error) {
u := fmt.Sprintf("consumers/%v", consumer)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
uResp := new(Consumer)
resp, err := s.client.Do(req, uResp)
if err != nil {
return nil, resp, err
}
return uResp, resp, err
}
// ConsumersService.Patch updates an existing Kong consumer object.
// At least one of consumer.Username or consumer.ID must be specified
// in the passed *Consumer parameter.
//
// Equivalent to PATCH /consumers/{username or id}
func (s *ConsumersService) Patch(consumer *Consumer) (*http.Response, error) {
var u string
if consumer.ID != "" {
u = fmt.Sprintf("consumers/%v", consumer.ID)
} else if consumer.Username != "" {
u = fmt.Sprintf("consumers/%v", consumer.Username)
} else {
return nil, errors.New("At least one of consumer.Username or consumer.ID must be specified")
}
req, err := s.client.NewRequest("PATCH", u, consumer)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
return resp, err
}
// ConsumersService.Delete deletes a single Kong consumer object, by name or id.
//
// Equivalent to DELETE /consumers/{username or id}
func (s *ConsumersService) Delete(consumer string) (*http.Response, error) {
u := fmt.Sprintf("consumers/%v", consumer)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
return resp, err
}
return resp, err
}
// ConsumersService.Post creates a new Kong consumer object.
//
// Equivalent to POST /consumers
func (s *ConsumersService) Post(consumer *Consumer) (*http.Response, error) {
req, err := s.client.NewRequest("POST", "consumers", consumer)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
return resp, err
}
// ConsumersGetAllOptions specifies optional filter parameters to the
// ConsumersService.GetAll method.
//
// Additional information about filtering options can be found in the
// Kong documentation at:
// https://getkong.org/docs/0.9.x/admin-api/#list-consumers
type ConsumersGetAllOptions struct {
ID string `url:"id,omitempty"` // A filter on the list based on the consumer id field.
CustomID string `url:"custom_id,omitempty"` // A filter on the list based on the consumer custom_id field.
Username string `url:"username,omitempty"` // A filter on the list based on the consumer username field.
Size int `url:"size,omitempty"` // A limit on the number of objects to be returned.
Offset string `url:"offset,omitempty"` // A cursor used for pagination. offset is an object identifier that defines a place in the list.
}
// ConsumersService.GetAll queries for all Kong consumer objects.
// This query can be filtered by supplying the ConsumersGetAllOptions struct.
//
// Equivalent to GET /consumers?uri=params&from=opt
func (s *ConsumersService) GetAll(opt *ConsumersGetAllOptions) (*Consumers, *http.Response, error) {
u, err := addOptions("consumers", opt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
uResp := new(Consumers)
resp, err := s.client.Do(req, uResp)
if err != nil {
return nil, resp, err
}
return uResp, resp, err
}