-
Notifications
You must be signed in to change notification settings - Fork 3
/
generic.go
226 lines (185 loc) · 5.55 KB
/
generic.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package chartmogul
import (
"errors"
"strings"
backoff "github.com/cenkalti/backoff/v3"
"github.com/parnurzeal/gorequest"
)
// The methods here encompass common boilerplate for CRUD REST operations.
// They'd be generics if Go had generics...
// so these methods do *not* properly check types.
// Eg. nils cannot be easily checked (without reflection).
// static internal error helper
var errRetry = errors.New("Retrying")
// CREATE
func (api API) create(path string, input interface{}, output interface{}) error {
var res gorequest.Response
var body []byte
var errs []error
// Retry on HTTP 429 rate limit, or network error, see:
// https://dev.chartmogul.com/docs/rate-limits
// https://godoc.org/github.com/cenkalti/backoff#pkg-constants
// nolint:errcheck
backoff.Retry(func() error {
res, body, errs = api.req(gorequest.New().
Post(prepareURL(path))).
SendStruct(input).
EndStruct(output)
if networkErrors(errs) || isHTTPStatusRetryable(res) {
return errRetry
}
return nil
}, backoff.NewExponentialBackOff())
// wrapping []errors into compatible error & making HTTPError
return wrapErrors(res, body, errs)
}
// READ
func (api API) list(path string, output interface{}, query ...interface{}) error {
var res gorequest.Response
var body []byte
var errs []error
// nolint:errcheck
backoff.Retry(func() error {
req := api.req(gorequest.New().Get(prepareURL(path)))
for _, q := range query {
req.Query(q)
}
res, body, errs = req.EndStruct(output)
if networkErrors(errs) || isHTTPStatusRetryable(res) {
return errRetry
}
return nil
}, backoff.NewExponentialBackOff())
return wrapErrors(res, body, errs)
}
// RETRIEVE
func (api API) retrieve(path string, uuid string, output interface{}) error {
var res gorequest.Response
var body []byte
var errs []error
if uuid != "" {
path = strings.Replace(path, ":uuid", uuid, 1)
}
// nolint:errcheck
backoff.Retry(func() error {
res, body, errs = api.req(gorequest.New().Get(prepareURL(path))).
EndStruct(output)
if networkErrors(errs) || isHTTPStatusRetryable(res) {
return errRetry
}
return nil
}, backoff.NewExponentialBackOff())
return wrapErrors(res, body, errs)
}
// UPDATE
func (api API) merge(path string, input interface{}) error {
var res gorequest.Response
var body string
var errs []error
// nolint:errcheck
backoff.Retry(func() error {
res, body, errs = api.req(gorequest.New().
Post(prepareURL(path))).
SendStruct(input).
End()
if networkErrors(errs) || isHTTPStatusRetryable(res) {
return errRetry
}
return nil
}, backoff.NewExponentialBackOff())
return wrapErrors(res, []byte(body), errs)
}
// UPDATE
func (api API) unmerge(path string, input interface{}) error {
return api.merge(path, input)
}
// updateImpl adds another meta level, because this same pattern
// uses multiple HTTP methods in API.
func (api API) updateImpl(path string, uuid string, input interface{}, output interface{}, method string) error {
var res gorequest.Response
var body []byte
var errs []error
path = strings.Replace(path, ":uuid", uuid, 1)
path = prepareURL(path)
// nolint:errcheck
backoff.Retry(func() error {
req := gorequest.New()
switch method {
case "update":
req = req.Patch(path)
case "add":
req = req.Post(path)
case "putTo":
req = req.Put(path)
}
req = api.req(req).SendStruct(input)
res, body, errs = req.EndStruct(output)
if networkErrors(errs) || isHTTPStatusRetryable(res) {
return errRetry
}
return nil
}, backoff.NewExponentialBackOff())
return wrapErrors(res, body, errs)
}
func (api API) update(path string, uuid string, input interface{}, output interface{}) error {
return api.updateImpl(path, uuid, input, output, "update")
}
// add is like update, but POST
func (api API) add(path string, uuid string, input interface{}, output interface{}) error {
return api.updateImpl(path, uuid, input, output, "add")
}
// putTo is like update, but PUT
func (api API) putTo(path string, uuid string, input interface{}, output interface{}) error {
return api.updateImpl(path, uuid, input, output, "putTo")
}
// DELETE
func (api API) delete(path string, uuid string) error {
var res gorequest.Response
var body string
var errs []error
path = strings.Replace(path, ":uuid", uuid, 1)
// nolint:errcheck
backoff.Retry(func() error {
res, body, errs = api.req(gorequest.New().Delete(prepareURL(path))).
End()
if networkErrors(errs) || isHTTPStatusRetryable(res) {
return errRetry
}
return nil
}, backoff.NewExponentialBackOff())
return wrapErrors(res, []byte(body), errs)
}
func (api API) deleteWhat(path string, uuid string, input interface{}, output interface{}) error {
var res gorequest.Response
var body []byte
var errs []error
path = strings.Replace(path, ":uuid", uuid, 1)
// nolint:errcheck
backoff.Retry(func() error {
res, body, errs = api.req(gorequest.New().Delete(prepareURL(path))).
SendStruct(input).
EndStruct(output)
if networkErrors(errs) || isHTTPStatusRetryable(res) {
return errRetry
}
return nil
}, backoff.NewExponentialBackOff())
return wrapErrors(res, body, errs)
}
func (api API) deleteWithData(path string, input interface{}) error {
var res gorequest.Response
var body string
var errs []error
backoff.Retry(func() error {
res, body, errs = api.req(gorequest.New().
Delete(prepareURL(path))).
SendStruct(input).
End()
if networkErrors(errs) || isHTTPStatusRetryable(res) {
return errRetry
}
return nil
}, backoff.NewExponentialBackOff())
// wrapping []errors into compatible error & making HTTPError
return wrapErrors(res, []byte(body), errs)
}