-
Notifications
You must be signed in to change notification settings - Fork 44
/
recurly.go
500 lines (437 loc) · 13.8 KB
/
recurly.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package recurly
import (
"bytes"
"context"
"encoding/base64"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"runtime"
"strconv"
"strings"
"time"
)
// apiVersion is the API version in use by this client.
// NOTE: v2.19:
// - Parent/child accounts not yet implemented.
const apiVersion = "2.27"
// uaVersion is the userAgent version sent to Recurly so they can track usage
// of this library.
const uaVersion = "1.0.0"
// HTTPDoer is used for making HTTP requests. This implementation is generally
// a *http.Client.
type HTTPDoer interface {
Do(req *http.Request) (*http.Response, error)
}
// Client manages communication with the Recurly API.
type Client struct {
// apiKey is your account's API key used for authentication.
apiKey string
// baseURL is the base url for requests.
baseURL *url.URL
// userAgent sets the User-Agent header for requests so Recurly can
// track usage of the client.
// See https://github.com/blacklightcms/recurly/issues/41
userAgent string
// Client is the HTTP Client used to communicate with the API.
// By default this uses http.DefaultClient, so there are no timeouts
// configured. It's recommended you set your own HTTP client with
// reasonable timeouts for your application.
Client HTTPDoer
// Services used for talking with different parts of the Recurly API
Accounts AccountsService
Adjustments AdjustmentsService
AddOns AddOnsService
AutomatedExports AutomatedExportsService
Billing BillingService
Coupons CouponsService
CreditPayments CreditPaymentsService
Invoices InvoicesService
Plans PlansService
Purchases PurchasesService
Redemptions RedemptionsService
ShippingAddresses ShippingAddressesService
ShippingMethods ShippingMethodsService
Subscriptions SubscriptionsService
Transactions TransactionsService
Items ItemsService
}
type serviceImpl struct {
client *Client
}
// NewClient returns a new instance of *Client.
// By default this uses http.DefaultClient, so there are no timeouts configured.
// It's recommended you set your own HTTP client with reasonable timeouts
// for your application.
func NewClient(subdomain, apiKey string) *Client {
baseEndpoint, _ := url.Parse(fmt.Sprintf("https://%s.recurly.com/", subdomain))
client := &Client{
Client: http.DefaultClient,
baseURL: baseEndpoint,
apiKey: base64.StdEncoding.EncodeToString([]byte(apiKey)),
userAgent: fmt.Sprintf(
"Blacklight/%s; Go (%s) [%s-%s]",
uaVersion,
runtime.Version(),
runtime.GOARCH,
runtime.GOOS,
),
}
client.Accounts = &accountsImpl{client: client}
client.Adjustments = &adjustmentsImpl{client: client}
client.AddOns = &addOnsImpl{client: client}
client.AutomatedExports = &automatedExportsImpl{client: client}
client.Billing = &billingImpl{client: client}
client.Coupons = &couponsImpl{client: client}
client.CreditPayments = &creditInvoicesImpl{client: client}
client.Invoices = &invoicesImpl{client: client}
client.Plans = &plansImpl{client: client}
client.Purchases = &purchasesImpl{client: client}
client.Redemptions = &redemptionsImpl{client: client}
client.ShippingAddresses = &shippingAddressesImpl{client: client}
client.ShippingMethods = &shippingMethodsImpl{client: client}
client.Subscriptions = &subscriptionsImpl{client: client}
client.Transactions = &transactionsImpl{client: client}
client.Items = &itemsImpl{client: client}
return client
}
// newRequest creates an authenticated API request that is ready to send.
func (c *Client) newRequest(method string, path string, body interface{}) (*http.Request, error) {
path = fmt.Sprintf("/v2/%s", strings.TrimPrefix(path, "/"))
u, err := c.baseURL.Parse(path)
if err != nil {
return nil, err
}
// Request body
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
if err := xml.NewEncoder(buf).Encode(body); err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", c.apiKey))
req.Header.Set("Accept", "application/xml")
req.Header.Set("User-Agent", c.userAgent)
req.Header.Set("X-Api-Version", apiVersion)
if body != nil {
req.Header.Set("Content-Type", "application/xml; charset=utf-8")
}
return req, err
}
// newPagerRequest is used for pagination.
func (c *Client) newPagerRequest(method string, path string, opts *PagerOptions, body interface{}) (*http.Request, error) {
req, err := c.newRequest(method, path, body)
if err != nil {
return nil, err
} else if opts != nil {
opts.append(req.URL)
}
return req, nil
}
// newQueryRequest is used to create requests that require query strings.
func (c *Client) newQueryRequest(method string, path string, q query, body interface{}) (*http.Request, error) {
req, err := c.newRequest(method, path, body)
if err != nil {
return nil, err
} else if len(q) > 0 {
q.append(req.URL)
}
return req, nil
}
// do takes a prepared API request and makes the API call to Recurly.
// It will decode the XML into a destination struct you provide as well
// as parse any validation errors that may have occurred.
// It returns a Response object that provides a wrapper around http.Response
// with some convenience methods.
func (c *Client) do(ctx context.Context, req *http.Request, v interface{}) (*response, error) {
req = req.WithContext(ctx)
resp, err := c.Client.Do(req)
if err != nil {
// If we got an error, and the context has been canceled,
// the context's error is probably more useful.
select {
default:
case <-ctx.Done():
return nil, ctx.Err()
}
return nil, err
}
defer resp.Body.Close()
response := newResponse(resp)
if resp.StatusCode == http.StatusNoContent {
return response, nil
} else if resp.StatusCode == http.StatusTooManyRequests {
return nil, &RateLimitError{
Response: resp,
Rate: response.rate,
}
} else if v != nil && resp.StatusCode >= 200 && resp.StatusCode <= 299 {
if w, ok := v.(io.Writer); ok {
io.Copy(w, resp.Body)
} else if err := xml.NewDecoder(resp.Body).Decode(&v); err != nil && err != io.EOF {
return response, err
}
return response, nil
} else if resp.StatusCode >= 400 && resp.StatusCode <= 499 {
return response, response.parseClientError(v)
} else if resp.StatusCode >= 500 && resp.StatusCode <= 599 {
return nil, &ServerError{Response: resp}
}
return response, nil
}
// response is a Recurly API response. This wraps the standard http.Response
// returned from Recurly and provides access to pagination cursors and rate
// limits.
type response struct {
*http.Response
// The next cursor (if available) when paginating results.
cursor string
// Rate limits.
rate Rate
}
// NewResponse creates a new Response for the provided http.Response.
func newResponse(r *http.Response) *response {
resp := &response{Response: r}
resp.populatePageCursor()
resp.populateRateLimit()
return resp
}
func (r *response) populatePageCursor() {
links, ok := r.Response.Header["Link"]
if !ok || len(links) == 0 {
return
}
for _, link := range strings.Split(links[0], ",") {
segments := strings.Split(strings.TrimSpace(link), ";")
if len(segments) < 2 { // link must at least have href and rel
continue
} else if !strings.HasPrefix(segments[0], "<") || !strings.HasSuffix(segments[0], ">") { // ensure href is properly formatted
continue
}
// try to pull out cursor parameter
url, err := url.Parse(segments[0][1 : len(segments[0])-1])
if err != nil {
continue
}
cursor := url.Query().Get("cursor")
if cursor == "" {
continue
}
for _, segment := range segments[1:] {
switch strings.TrimSpace(segment) {
case `rel="next"`:
r.cursor = cursor
}
}
}
}
// populates rate limits.
func (r *response) populateRateLimit() {
if limit := r.Header.Get("X-RateLimit-Limit"); limit != "" {
r.rate.Limit, _ = strconv.Atoi(limit)
}
if remaining := r.Header.Get("X-RateLimit-Remaining"); remaining != "" {
r.rate.Remaining, _ = strconv.Atoi(remaining)
}
if reset := r.Header.Get("X-RateLimit-Reset"); reset != "" {
if v, _ := strconv.ParseInt(reset, 10, 64); v != 0 {
r.rate.Reset = time.Unix(v, 0)
}
}
}
// parses client errors.
func (r *response) parseClientError(v interface{}) error {
// Immediately return a client error if there is no response body.
if r.Header.Get("Content-Length") == "0" {
return &ClientError{Response: r.Response}
}
// Read the full response body so we can conditionally process the
// xml based on the top level tag that is returned.
b, err := ioutil.ReadAll(r.Response.Body)
if err != nil {
return err
} else if len(b) == 0 {
// Exit here to avoid io.EOF errors.
return &ClientError{Response: r.Response}
}
var name struct {
XMLName xml.Name
}
if err := xml.Unmarshal(b, &name); err != nil {
return err
}
switch name.XMLName.Local {
case "error":
var e xmlSingleError
if err := xml.Unmarshal(b, &e); err != nil {
return err
}
return &ClientError{
Response: r.Response,
ValidationErrors: []ValidationError{{
Description: e.Description,
Field: e.Field,
Symbol: e.Symbol,
}},
}
case "errors":
var e xmlMultiErrors
if err := xml.Unmarshal(b, &e); err != nil {
return err
}
// Any transaction errors return TransactionFailedError.
if e.Transaction != nil || e.TransactionError != nil {
transFailedErr := &TransactionFailedError{
Response: r.Response,
Transaction: e.Transaction,
}
if e.TransactionError != nil {
transFailedErr.TransactionError = *e.TransactionError
}
return transFailedErr
}
clientErr := &ClientError{Response: r.Response}
if len(e.Errors) > 0 {
clientErr.ValidationErrors = make([]ValidationError, len(e.Errors))
for i := range e.Errors {
clientErr.ValidationErrors[i] = ValidationError{
Description: e.Errors[i].Description,
Field: e.Errors[i].Field,
Symbol: e.Errors[i].Symbol,
}
}
}
return clientErr
}
// Unknown body.
return &ClientError{Response: r.Response}
}
// Rate represents the rate limit for the current client.
type Rate struct {
// The total request limit during the 5 minute window (e.g. requests/min * 5 min)
Limit int
// The number of requests remaining until your requests will be denied.
Remaining int
// The time when the current window will completely reset assuming no further API requests are made.
Reset time.Time
}
// RateLimitError occurs when Recurly returns a 429 Too Many Requests error.
type RateLimitError struct {
Response *http.Response
Rate Rate // Rate specifies the last known rate limit for the client
}
func (e *RateLimitError) Error() string {
return fmt.Sprintf("API rate limit exceeded: %s %s: %d %v",
e.Response.Request.Method,
e.Response.Request.URL.Path,
e.Response.StatusCode,
e.Rate.Reset.Sub(time.Now()),
)
}
// ClientError occurs when Recurly returns 400-499 status code.
// There are two known exceptions to this:
// 1) 429 Too Many Requests. See RateLimitError.
// 2) 422 Unprocessable Entity if a failed transaction occurred. See TransactionFailedError.
// All other 422 responses not related to failed transactions will return
// ClientError.
type ClientError struct {
Response *http.Response
// ValidationErrors holds an array of validation errors if any occurred.
ValidationErrors []ValidationError
}
// Is returns true if one of the validation errors has a matching symbol.
func (e *ClientError) Is(symbol string) bool {
for _, e := range e.ValidationErrors {
if e.Symbol == symbol {
return true
}
}
return false
}
func (e *ClientError) Error() string {
var b strings.Builder
for i, err := range e.ValidationErrors {
b.WriteString(err.Error())
if i > 0 {
b.WriteString(";")
}
}
return fmt.Sprintf("client error: %s %s: %d %v",
e.Response.Request.Method,
e.Response.Request.URL.Path,
e.Response.StatusCode,
b.String(),
)
}
// TransactionFailedError is returned when a transaction fails.
type TransactionFailedError struct {
Response *http.Response
// Transaction holds the failed transaction (if any).
Transaction *Transaction
// TransactionError holds the transaction error. This will always be
// available.
TransactionError TransactionError
}
func (e *TransactionFailedError) Error() string {
return fmt.Sprintf("transaction failed: %s %s: %d [%s/%s/%s]",
e.Response.Request.Method,
e.Response.Request.URL.Path,
e.Response.StatusCode,
e.TransactionError.ErrorCode,
e.TransactionError.ErrorCategory,
e.TransactionError.CustomerMessage,
)
}
// ServerError occurs when Recurly returns 500-599 status code.
type ServerError struct {
Response *http.Response
}
func (e *ServerError) Error() string {
return fmt.Sprintf("server error: %s %s: %d",
e.Response.Request.Method,
e.Response.Request.URL.Path,
e.Response.StatusCode)
}
// ValidationError is an individual validation error.
type ValidationError struct {
Description string
Field string
Symbol string
}
func (e *ValidationError) Error() string {
if e.Field != "" {
if e.Symbol == "" {
return fmt.Sprintf("%s %s", e.Field, e.Description)
}
return fmt.Sprintf("%s %s (%s)", e.Field, e.Description, e.Symbol)
} else if e.Symbol != "" {
return fmt.Sprintf("%s (%s)", e.Description, e.Symbol)
}
return fmt.Sprintf("%s", e.Description)
}
// xmlSingleError is returned as a standalone error.
type xmlSingleError struct {
XMLName xml.Name `xml:"error"`
Description string `xml:"description"`
Field string `xml:"field"`
Symbol string `xml:"symbol"`
}
// xmlMultiErrors is a collection of various errors.
type xmlMultiErrors struct {
XMLName xml.Name `xml:"errors"`
Errors []struct {
Description string `xml:",innerxml"`
Field string `xml:"field,attr"`
Symbol string `xml:"symbol,attr"`
} `xml:"error"`
Transaction *Transaction `xml:"transaction"`
TransactionError *TransactionError `xml:"transaction_error"`
}