forked from Kong/go-kong
/
consumer_service.go
196 lines (171 loc) · 4.78 KB
/
consumer_service.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
package kong
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// AbstractConsumerService handles Consumers in Kong.
type AbstractConsumerService interface {
// Create creates a Consumer in Kong.
Create(ctx context.Context, consumer *Consumer) (*Consumer, error)
// Get fetches a Consumer in Kong.
Get(ctx context.Context, usernameOrID *string) (*Consumer, error)
// GetByCustomID fetches a Consumer in Kong.
GetByCustomID(ctx context.Context, customID *string) (*Consumer, error)
// Update updates a Consumer in Kong
Update(ctx context.Context, consumer *Consumer) (*Consumer, error)
// Delete deletes a Consumer in Kong
Delete(ctx context.Context, usernameOrID *string) error
// List fetches a list of Consumers in Kong.
List(ctx context.Context, opt *ListOpt) ([]*Consumer, *ListOpt, error)
// ListAll fetches all Consumers in Kong.
ListAll(ctx context.Context) ([]*Consumer, error)
}
// ConsumerService handles Consumers in Kong.
type ConsumerService service
// Create creates a Consumer in Kong.
// If an ID is specified, it will be used to
// create a consumer in Kong, otherwise an ID
// is auto-generated.
func (s *ConsumerService) Create(ctx context.Context,
consumer *Consumer,
) (*Consumer, error) {
queryPath := "/consumers"
method := "POST"
if consumer.ID != nil {
queryPath = queryPath + "/" + *consumer.ID
method = "PUT"
}
req, err := s.client.NewRequest(method, queryPath, nil, consumer)
if err != nil {
return nil, err
}
var createdConsumer Consumer
_, err = s.client.Do(ctx, req, &createdConsumer)
if err != nil {
return nil, err
}
return &createdConsumer, nil
}
// Get fetches a Consumer in Kong.
func (s *ConsumerService) Get(ctx context.Context,
usernameOrID *string,
) (*Consumer, error) {
if isEmptyString(usernameOrID) {
return nil, fmt.Errorf("usernameOrID cannot be nil for Get operation")
}
endpoint := fmt.Sprintf("/consumers/%v", *usernameOrID)
req, err := s.client.NewRequest("GET", endpoint, nil, nil)
if err != nil {
return nil, err
}
var consumer Consumer
_, err = s.client.Do(ctx, req, &consumer)
if err != nil {
return nil, err
}
return &consumer, nil
}
// GetByCustomID fetches a Consumer in Kong.
func (s *ConsumerService) GetByCustomID(ctx context.Context,
customID *string,
) (*Consumer, error) {
if isEmptyString(customID) {
return nil, fmt.Errorf("customID cannot be nil for Get operation")
}
type QS struct {
CustomID string `url:"custom_id,omitempty"`
}
req, err := s.client.NewRequest("GET", "/consumers",
&QS{CustomID: *customID}, nil)
if err != nil {
return nil, err
}
type Response struct {
Data []Consumer
}
var resp Response
_, err = s.client.Do(ctx, req, &resp)
if err != nil {
return nil, err
}
if len(resp.Data) == 0 {
return nil, NewAPIError(http.StatusNotFound, "Not found")
}
return &resp.Data[0], nil
}
// Update updates a Consumer in Kong
func (s *ConsumerService) Update(ctx context.Context,
consumer *Consumer,
) (*Consumer, error) {
if isEmptyString(consumer.ID) {
return nil, fmt.Errorf("ID cannot be nil for Update operation")
}
endpoint := fmt.Sprintf("/consumers/%v", *consumer.ID)
req, err := s.client.NewRequest("PATCH", endpoint, nil, consumer)
if err != nil {
return nil, err
}
var updatedAPI Consumer
_, err = s.client.Do(ctx, req, &updatedAPI)
if err != nil {
return nil, err
}
return &updatedAPI, nil
}
// Delete deletes a Consumer in Kong
func (s *ConsumerService) Delete(ctx context.Context,
usernameOrID *string,
) error {
if isEmptyString(usernameOrID) {
return fmt.Errorf("usernameOrID cannot be nil for Delete operation")
}
endpoint := fmt.Sprintf("/consumers/%v", *usernameOrID)
req, err := s.client.NewRequest("DELETE", endpoint, nil, nil)
if err != nil {
return err
}
_, err = s.client.Do(ctx, req, nil)
return err
}
// List fetches a list of Consumers in Kong.
// opt can be used to control pagination.
func (s *ConsumerService) List(ctx context.Context,
opt *ListOpt,
) ([]*Consumer, *ListOpt, error) {
data, next, err := s.client.list(ctx, "/consumers", opt)
if err != nil {
return nil, nil, err
}
var consumers []*Consumer
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, nil, err
}
var consumer Consumer
err = json.Unmarshal(b, &consumer)
if err != nil {
return nil, nil, err
}
consumers = append(consumers, &consumer)
}
return consumers, next, nil
}
// ListAll fetches all Consumers in Kong.
// This method can take a while if there
// a lot of Consumers present.
func (s *ConsumerService) ListAll(ctx context.Context) ([]*Consumer, error) {
var consumers, data []*Consumer
var err error
opt := &ListOpt{Size: pageSize}
for opt != nil {
data, opt, err = s.List(ctx, opt)
if err != nil {
return nil, err
}
consumers = append(consumers, data...)
}
return consumers, nil
}