-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomers.go
106 lines (78 loc) · 2.51 KB
/
customers.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
package tasty
import (
"fmt"
"net/http"
)
// Get authenticated customer.
func (c *Client) GetMyCustomerInfo() (Customer, *http.Response, error) {
path := "/customers/me"
type customerResponse struct {
Customer Customer `json:"data"`
}
customersRes := new(customerResponse)
resp, err := c.request(http.MethodGet, path, nil, nil, customersRes)
if err != nil {
return Customer{}, resp, err
}
return customersRes.Customer, resp, nil
}
// Get a full customer resource.
func (c *Client) GetCustomer(customerID string) (Customer, *http.Response, error) {
path := fmt.Sprintf("/customers/%s", customerID)
type customerResponse struct {
Customer Customer `json:"data"`
}
customersRes := new(customerResponse)
resp, err := c.request(http.MethodGet, path, nil, nil, customersRes)
if err != nil {
return Customer{}, resp, err
}
return customersRes.Customer, resp, nil
}
// Get a list of all the customer account resources attached to the current customer.
func (c *Client) GetCustomerAccounts(customerID string) ([]Account, *http.Response, error) {
path := fmt.Sprintf("/customers/%s/accounts", customerID)
type customerResponse struct {
Data struct {
Items []struct {
Account Account `json:"account"`
} `json:"items"`
} `json:"data"`
}
customersRes := new(customerResponse)
resp, err := c.request(http.MethodGet, path, nil, nil, customersRes)
if err != nil {
return []Account{}, resp, err
}
var accounts []Account
for _, acct := range customersRes.Data.Items {
accounts = append(accounts, acct.Account)
}
return accounts, resp, nil
}
// Get a full customer account resource.
func (c *Client) GetCustomerAccount(customerID, accountNumber string) (Account, *http.Response, error) {
path := fmt.Sprintf("/customers/%s/accounts/%s", customerID, accountNumber)
type customerResponse struct {
Account Account `json:"data"`
}
customersRes := new(customerResponse)
resp, err := c.request(http.MethodGet, path, nil, nil, customersRes)
if err != nil {
return Account{}, resp, err
}
return customersRes.Account, resp, nil
}
// Get authenticated user's full account resource.
func (c *Client) GetMyAccount(accountNumber string) (Account, *http.Response, error) {
path := fmt.Sprintf("/customers/me/accounts/%s", accountNumber)
type customerResponse struct {
Account Account `json:"data"`
}
customersRes := new(customerResponse)
resp, err := c.request(http.MethodGet, path, nil, nil, customersRes)
if err != nil {
return Account{}, resp, err
}
return customersRes.Account, resp, nil
}