-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
86 lines (71 loc) · 2.1 KB
/
account.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
package ziggy
import (
"context"
"fmt"
)
type Account struct {
ID string `json:"accountId"`
Name string `json:"accountName"`
Number string `json:"accountNumber"`
Product string `json:"productName"`
Reference string `json:"referenceName"`
}
// AccountResponse describes the response data returned for retrieving a list
// of accounts.
type AccountResponse struct {
Data struct {
Accounts []Account `json:"accounts"`
} `json:"data"`
Links struct {
Self string `json:"self"`
} `json:"links"`
Metadata struct {
TotalPages int64 `json:"totalPages"`
} `json:"meta"`
}
// GetAccounts obtains a list of accounts.
func (c *Client) GetAccounts(ctx context.Context) ([]Account, error) {
res, err := c.transport.Get(ctx, "/za/pb/v1/accounts", nil)
if err != nil {
return nil, fmt.Errorf("failed to get accounts: %w", err)
}
var accountsResponse AccountResponse
if err = res.JSON(&accountsResponse); err != nil {
return nil, fmt.Errorf("failed to unmarshal account response: %w", err)
}
return accountsResponse.Data.Accounts, nil
}
type Balance struct {
AccountID string `json:"accountId"`
Available float64 `json:"availableBalance"`
Currency string `json:"currency"`
Current float64 `json:"currentBalance"`
}
// BalanceResponse describes the response data returned for retrieving an
// account's balance.
type BalanceResponse struct {
Data struct {
Balance
} `json:"data"`
Links struct {
Self string `json:"self"`
} `json:"links"`
Metadata struct {
TotalPages int64 `json:"totalPages"`
} `json:"meta"`
}
// GetAccountBalance obtains a specified account's balance.
func (c *Client) GetAccountBalance(ctx context.Context, accountID string) (
*Balance, error) {
res, err := c.transport.Post(ctx, fmt.Sprintf(
"/za/pb/v1/accounts/%s/balance", accountID), nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to get account balance %s: %w",
accountID, err)
}
var balanceResponse BalanceResponse
if err = res.JSON(&balanceResponse); err != nil {
return nil, fmt.Errorf("failed to unmarshal balance response: %w", err)
}
return &balanceResponse.Data.Balance, nil
}