-
Notifications
You must be signed in to change notification settings - Fork 28
/
card.go
67 lines (56 loc) · 1.52 KB
/
card.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
package aiven
import (
"context"
"fmt"
)
type (
// Card represents the card model on Aiven.
Card struct {
Brand string `json:"brand"`
CardID string `json:"card_id"`
Country string `json:"country"`
CountryCode string `json:"country_code"`
ExpMonth int `json:"exp_month"`
ExpYear int `json:"exp_year"`
Last4 string `json:"last4"`
Name string `json:"name"`
ProjectNames []string `json:"projects"`
}
// CardsHandler is the client that interacts with the cards endpoints on
// Aiven.
CardsHandler struct {
client *Client
}
// CardListResponse is the response for listing cards.
CardListResponse struct {
APIResponse
Cards []*Card `json:"cards"`
}
)
// List returns all the cards linked to the authenticated account.
func (h *CardsHandler) List(ctx context.Context) ([]*Card, error) {
bts, err := h.client.doGetRequest(ctx, "/card", nil)
if err != nil {
return nil, err
}
var r CardListResponse
errR := checkAPIResponse(bts, &r)
return r.Cards, errR
}
// Get card by card ID. The ID may be either last 4 digits of the card or the actual ID
func (h *CardsHandler) Get(ctx context.Context, cardID string) (*Card, error) {
if len(cardID) == 0 {
return nil, nil
}
cards, err := h.List(ctx)
if err != nil {
return nil, err
}
for _, card := range cards {
if card.CardID == cardID || card.Last4 == cardID {
return card, nil
}
}
err = Error{Message: fmt.Sprintf("Card with ID %v not found", cardID), Status: 404}
return nil, err
}