-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathaccounts.go
206 lines (177 loc) · 5.55 KB
/
accounts.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
package nordigen
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type AccountMetadata struct {
Id string `json:"id,omitempty"`
Created string `json:"created,omitempty"`
LastAccessed string `json:"last_accessed,omitempty"`
Iban string `json:"iban,omitempty"`
InstitutionId string `json:"institution_id,omitempty"`
// There is an issue in the api, the status is still a string
// like in v1
Status string `json:"status,omitempty"`
//Status []string `json:"status"`
}
type AccountBalanceAmount struct {
Amount string `json:"amount,omitempty"`
Currency string `json:"currency,omitempty"`
}
type AccountBalance struct {
BalanceAmount AccountBalanceAmount `json:"balanceAmount,omitempty"`
BalanceType string `json:"balanceType,omitempty"`
}
type AccountBalances struct {
Balances []AccountBalance `json:"balances,omitempty"`
}
type AccountDetails struct {
Account struct {
ResourceId string `json:"resourceId,omitempty"`
Iban string `json:"iban,omitempty"`
Currency string `json:"currency,omitempty"`
OwnerName string `json:"ownerName,omitempty"`
Product string `json:"product,omitempty,"`
Status string `json:"status,omitempty"`
} `json:"account"`
}
type Transaction struct {
TransactionId string `json:"transactionId,omitempty"`
EntryReference string `json:"entryReference,omitempty"`
BookingDate string `json:"bookingDate,omitempty"`
BookingDateTime string `json:"bookingDateTime,omitempty"`
ValueDate string `json:"valueDate,omitempty"`
ValueDateTime string `json:"valueDateTime,omitempty"`
TransactionAmount struct {
Amount string `json:"amount,omitempty"`
Currency string `json:"currency,omitempty"`
} `json:"transactionAmount,omitempty"`
CreditorName string `json:"creditorName,omitempty"`
CreditorAccount struct {
Iban string `json:"iban,omitempty"`
} `json:"creditorAccount,omitempty"`
UltimateCreditor string `json:"ultimateCreditor,omitempty"`
DebtorName string `json:"debtorName,omitempty"`
DebtorAccount struct {
Iban string `json:"iban,omitempty"`
} `json:"debtorAccount,omitempty"`
UltimateDebtor string `json:"ultimateDebtor,omitempty"`
RemittanceInformationUnstructured string `json:"remittanceInformationUnstructured"`
RemittanceInformationUnstructuredArray []string `json:"RemittanceInformationUnstructuredArray"`
BankTransactionCode string `json:"bankTransactionCode,omitempty"`
AdditionalInformation string `json:"additionalInformation,omitempty"`
InternalTransactionId string `json:"internalTransactionId,omitempty"`
}
type AccountTransactions struct {
Transactions struct {
Booked []Transaction `json:"booked,omitempty"`
Pending []Transaction `json:"pending,omitempty"`
} `json:"transactions,omitempty"`
}
const accountPath = "accounts"
const balancesPath = "balances"
const detailsPath = "details"
const transactionsPath = "transactions"
func (c Client) GetAccountMetadata(id string) (AccountMetadata, error) {
req := http.Request{
Method: http.MethodGet,
URL: &url.URL{
Path: strings.Join([]string{accountPath, id, ""}, "/"),
},
}
resp, err := c.c.Do(&req)
if err != nil {
return AccountMetadata{}, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return AccountMetadata{}, err
}
if resp.StatusCode != http.StatusOK {
return AccountMetadata{}, &APIError{resp.StatusCode, string(body), err}
}
accMtdt := AccountMetadata{}
err = json.Unmarshal(body, &accMtdt)
if err != nil {
return AccountMetadata{}, err
}
return accMtdt, nil
}
func (c Client) GetAccountBalances(id string) (AccountBalances, error) {
req := http.Request{
Method: http.MethodGet,
URL: &url.URL{
Path: strings.Join([]string{accountPath, id, balancesPath, ""}, "/"),
},
}
resp, err := c.c.Do(&req)
if err != nil {
return AccountBalances{}, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return AccountBalances{}, err
}
if resp.StatusCode != http.StatusOK {
return AccountBalances{}, &APIError{resp.StatusCode, string(body), err}
}
accBlnc := AccountBalances{}
err = json.Unmarshal(body, &accBlnc)
if err != nil {
return AccountBalances{}, err
}
return accBlnc, nil
}
func (c Client) GetAccountDetails(id string) (AccountDetails, error) {
req := http.Request{
Method: http.MethodGet,
URL: &url.URL{
Path: strings.Join([]string{accountPath, id, detailsPath, ""}, "/"),
},
}
resp, err := c.c.Do(&req)
if err != nil {
return AccountDetails{}, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return AccountDetails{}, err
}
if resp.StatusCode != http.StatusOK {
return AccountDetails{}, &APIError{resp.StatusCode, string(body), err}
}
accDtl := AccountDetails{}
err = json.Unmarshal(body, &accDtl)
if err != nil {
return AccountDetails{}, err
}
return accDtl, nil
}
func (c Client) GetAccountTransactions(id string) (AccountTransactions, error) {
req := http.Request{
Method: http.MethodGet,
URL: &url.URL{
Path: strings.Join([]string{accountPath, id, transactionsPath, ""}, "/"),
},
}
resp, err := c.c.Do(&req)
if err != nil {
return AccountTransactions{}, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return AccountTransactions{}, err
}
if resp.StatusCode != http.StatusOK {
return AccountTransactions{}, &APIError{resp.StatusCode, string(body), err}
}
accTxns := AccountTransactions{}
err = json.Unmarshal(body, &accTxns)
if err != nil {
return AccountTransactions{}, err
}
return accTxns, nil
}