forked from kevholditch/gokong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumers.go
204 lines (153 loc) · 6.1 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
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
197
198
199
200
201
202
203
204
package gokong
import (
"encoding/json"
"fmt"
)
type ConsumerClient struct {
config *Config
}
type ConsumerRequest struct {
Username string `json:"username,omitempty"`
CustomId string `json:"custom_id,omitempty"`
}
type Consumer struct {
Id string `json:"id,omitempty"`
CustomId string `json:"custom_id,omitempty"`
Username string `json:"username,omitempty"`
}
type Consumers struct {
Results []*Consumer `json:"data,omitempty"`
Total int `json:"total,omitempty"`
Next string `json:"next,omitempty"`
}
type ConsumerFilter struct {
Id string `url:"id,omitempty"`
CustomId string `url:"custom_id,omitempty"`
Username string `url:"username,omitempty"`
Size int `url:"size,omitempty"`
Offset int `url:"offset,omitempty"`
}
type ConsumerPluginConfig struct {
Id string `json:"id,omitempty"`
Body string
}
const ConsumersPath = "/consumers/"
func (consumerClient *ConsumerClient) GetByUsername(username string) (*Consumer, error) {
return consumerClient.GetById(username)
}
func (consumerClient *ConsumerClient) GetById(id string) (*Consumer, error) {
_, body, errs := newGet(consumerClient.config, consumerClient.config.HostAddress+ConsumersPath+id).End()
if errs != nil {
return nil, fmt.Errorf("could not get consumer, error: %v", errs)
}
consumer := &Consumer{}
err := json.Unmarshal([]byte(body), consumer)
if err != nil {
return nil, fmt.Errorf("could not parse consumer get response, error: %v", err)
}
if consumer.Id == "" {
return nil, nil
}
return consumer, nil
}
func (consumerClient *ConsumerClient) Create(consumerRequest *ConsumerRequest) (*Consumer, error) {
_, body, errs := newPost(consumerClient.config, consumerClient.config.HostAddress+ConsumersPath).Send(consumerRequest).End()
if errs != nil {
return nil, fmt.Errorf("could not create new consumer, error: %v", errs)
}
createdConsumer := &Consumer{}
err := json.Unmarshal([]byte(body), createdConsumer)
if err != nil {
return nil, fmt.Errorf("could not parse consumer creation response, error: %v", err)
}
if createdConsumer.Id == "" {
return nil, fmt.Errorf("could not create consumer, error: %v", body)
}
return createdConsumer, nil
}
func (consumerClient *ConsumerClient) List() (*Consumers, error) {
return consumerClient.ListFiltered(nil)
}
func (consumerClient *ConsumerClient) ListFiltered(filter *ConsumerFilter) (*Consumers, error) {
address, err := addQueryString(consumerClient.config.HostAddress+ConsumersPath, filter)
if err != nil {
return nil, fmt.Errorf("could not build query string for consumer filter, error: %v", err)
}
_, body, errs := newGet(consumerClient.config, address).End()
if errs != nil {
return nil, fmt.Errorf("could not get consumers, error: %v", errs)
}
consumers := &Consumers{}
err = json.Unmarshal([]byte(body), consumers)
if err != nil {
return nil, fmt.Errorf("could not parse consumers list response, error: %v", err)
}
return consumers, nil
}
func (consumerClient *ConsumerClient) DeleteByUsername(username string) error {
return consumerClient.DeleteById(username)
}
func (consumerClient *ConsumerClient) DeleteById(id string) error {
res, _, errs := newDelete(consumerClient.config, consumerClient.config.HostAddress+ConsumersPath+id).End()
if errs != nil {
return fmt.Errorf("could not delete consumer, result: %v error: %v", res, errs)
}
return nil
}
func (consumerClient *ConsumerClient) UpdateByUsername(username string, consumerRequest *ConsumerRequest) (*Consumer, error) {
return consumerClient.UpdateById(username, consumerRequest)
}
func (consumerClient *ConsumerClient) UpdateById(id string, consumerRequest *ConsumerRequest) (*Consumer, error) {
_, body, errs := newPatch(consumerClient.config, consumerClient.config.HostAddress+ConsumersPath+id).Send(consumerRequest).End()
if errs != nil {
return nil, fmt.Errorf("could not update consumer, error: %v", errs)
}
updatedConsumer := &Consumer{}
err := json.Unmarshal([]byte(body), updatedConsumer)
if err != nil {
return nil, fmt.Errorf("could not parse consumer update response, error: %v", err)
}
if updatedConsumer.Id == "" {
return nil, fmt.Errorf("could not update consumer, error: %v", body)
}
return updatedConsumer, nil
}
func (consumerClient *ConsumerClient) CreatePluginConfig(consumerId string, pluginName string, pluginConfig string) (*ConsumerPluginConfig, error) {
_, body, errs := newPost(consumerClient.config, consumerClient.config.HostAddress+ConsumersPath+consumerId+"/"+pluginName).Send(pluginConfig).End()
if errs != nil {
return nil, fmt.Errorf("could not configure plugin for consumer, error: %v", errs)
}
createdConsumerPluginConfig := &ConsumerPluginConfig{}
err := json.Unmarshal([]byte(body), createdConsumerPluginConfig)
if err != nil {
return nil, fmt.Errorf("could not parse consumer plugin config created response, error: %v", err)
}
if createdConsumerPluginConfig.Id == "" {
return nil, fmt.Errorf("could not create consumer plugin config, error: %v", body)
}
createdConsumerPluginConfig.Body = body
return createdConsumerPluginConfig, nil
}
func (consumerClient *ConsumerClient) GetPluginConfig(consumerId string, pluginName string, id string) (*ConsumerPluginConfig, error) {
_, body, errs := newGet(consumerClient.config, consumerClient.config.HostAddress+ConsumersPath+consumerId+"/"+pluginName+"/"+id).End()
if errs != nil {
return nil, fmt.Errorf("could not get plugin config for consumer, error: %v", errs)
}
consumerPluginConfig := &ConsumerPluginConfig{}
err := json.Unmarshal([]byte(body), consumerPluginConfig)
if err != nil {
return nil, fmt.Errorf("could not parse consumer plugin config response, error: %v", err)
}
if consumerPluginConfig.Id == "" {
return nil, nil
}
consumerPluginConfig.Body = body
return consumerPluginConfig, nil
}
func (consumerClient *ConsumerClient) DeletePluginConfig(consumerId string, pluginName string, id string) error {
_, _, errs := newDelete(consumerClient.config, consumerClient.config.HostAddress+ConsumersPath+consumerId+"/"+pluginName+"/"+id).End()
if errs != nil {
return fmt.Errorf("could not delete plugin config for consumer, error: %v", errs)
}
return nil
}