forked from guilhermebr/go-stone-openbank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.go
163 lines (122 loc) · 3.41 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
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
package openbank
import (
"errors"
"fmt"
"net/http"
"github.com/stone-co/go-stone-openbank/types"
)
// AccountService handles communication with Stone Openbank API
type AccountService struct {
client *Client
}
//TODO: CreateNewIdentity
// Get account info
func (s *AccountService) Get(id string) (*types.Account, *Response, error) {
path := fmt.Sprintf("/api/v1/accounts/%s", id)
req, err := s.client.NewAPIRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var account types.Account
resp, err := s.client.Do(req, &account)
if err != nil {
return nil, resp, err
}
return &account, resp, err
}
// List accounts
func (s *AccountService) List() ([]types.Account, *Response, error) {
path := "/api/v1/accounts?paginate=true"
req, err := s.client.NewAPIRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var dataResp struct {
Cursor types.Cursor `json:"cursor"`
Data []types.Account `json:"data"`
}
resp, err := s.client.Do(req, &dataResp)
if err != nil {
return nil, resp, err
}
return dataResp.Data, resp, err
}
//Get Account Balance
func (s *AccountService) GetBalance(id string) (*types.Balance, *Response, error) {
path := fmt.Sprintf("/api/v1/accounts/%s/balance", id)
req, err := s.client.NewAPIRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var balance types.Balance
resp, err := s.client.Do(req, &balance)
if err != nil {
return nil, resp, err
}
return &balance, resp, err
}
//Get Account Statement
func (s *AccountService) GetStatement(id string) ([]types.Statement, *Response, error) {
path := fmt.Sprintf("/api/v1/accounts/%s/statement", id)
req, err := s.client.NewAPIRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var dataResp struct {
Cursor types.Cursor `json:"cursor"`
Data []types.Statement `json:"data"`
}
resp, err := s.client.Do(req, &dataResp)
if err != nil {
return nil, resp, err
}
return dataResp.Data, resp, err
}
// Get Statement Entry
func (s *AccountService) GetStatementEntry(id string) (*types.Statement, *Response, error) {
path := fmt.Sprintf("/api/v1/statement/entries/%s", id)
req, err := s.client.NewAPIRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var statement types.Statement
resp, err := s.client.Do(req, &statement)
if err != nil {
return nil, resp, err
}
return &statement, resp, err
}
// Get Account Fees of FeeType
func (s *AccountService) GetFees(accountID string, feeType string) (*types.Fee, *Response, error) {
if feeType == "" {
return nil, nil, errors.New("missing feeType value")
}
path := fmt.Sprintf("/api/v1/accounts/%s/fees/%s", accountID, feeType)
req, err := s.client.NewAPIRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var fee types.Fee
resp, err := s.client.Do(req, &fee)
if err != nil {
return nil, resp, err
}
return &fee, resp, err
}
// List Account Fees
func (s *AccountService) ListFees(accountID string) ([]types.Fee, *Response, error) {
path := fmt.Sprintf("/api/v1/accounts/%s/fees", accountID)
req, err := s.client.NewAPIRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var dataResp struct {
Cursor types.Cursor `json:"cursor"`
Data []types.Fee `json:"data"`
}
resp, err := s.client.Do(req, &dataResp)
if err != nil {
return nil, resp, err
}
return dataResp.Data, resp, err
}