forked from t4ke0/investecOpenAPI
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinvestecOpenAPI.go
More file actions
166 lines (132 loc) · 3.99 KB
/
investecOpenAPI.go
File metadata and controls
166 lines (132 loc) · 3.99 KB
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
package investecOpenAPI
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/t4ke0/investecOpenAPI/api"
)
const APIurl string = "https://openapi.investec.com"
type BankingClient struct {
UserCreds string
AccessToken string
httpClient *http.Client
}
func NewBankingClient(secret, clientID string) BankingClient {
userCreds := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", clientID, secret)))
return BankingClient{
UserCreds: userCreds,
httpClient: new(http.Client),
}
}
type authMode int32
const (
basic authMode = iota
bearer
)
func (b BankingClient) requestAPI(url, method string, mode authMode) (*http.Response, error) {
var (
req *http.Request
err error
)
if mode == basic {
req, err = http.NewRequest(method, url, strings.NewReader("grant_type=client_credentials&scope=accounts"))
} else {
req, err = http.NewRequest(method, url, nil)
}
if err != nil {
return nil, err
}
switch mode {
case basic:
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", b.UserCreds))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
case bearer:
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", b.AccessToken))
req.Header.Set("Accept", "application/json")
}
return b.httpClient.Do(req)
}
// GetAccessToken
func (b *BankingClient) GetAccessToken() error {
url := fmt.Sprintf("%s/identity/v2/oauth2/token", APIurl)
resp, err := b.requestAPI(url, http.MethodPost, basic)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to get access token oauth [%v] [%v]", resp.StatusCode, string(data))
}
var tokenResponse api.OAuth2Response
if err := json.Unmarshal(data, &tokenResponse); err != nil {
return err
}
b.AccessToken = tokenResponse.AccessToken
return nil
}
// GetAccounts
func (b BankingClient) GetAccounts() (api.Accounts, error) {
url := fmt.Sprintf("%s/za/pb/v1/accounts", APIurl)
resp, err := b.requestAPI(url, http.MethodGet, bearer)
if err != nil {
return api.Accounts{}, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return api.Accounts{}, err
}
if resp.StatusCode != http.StatusOK {
return api.Accounts{}, fmt.Errorf("failed to get accounts %v %v", resp.StatusCode, string(data))
}
var accounts api.Accounts
return accounts, json.Unmarshal(data, &accounts)
}
// GetAccountBalance
func (b BankingClient) GetAccountBalance(accountId string) (api.Balance, error) {
url := fmt.Sprintf("%s/za/pb/v1/accounts/%s/balance", APIurl, accountId)
resp, err := b.requestAPI(url, http.MethodGet, bearer)
if err != nil {
return api.Balance{}, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return api.Balance{}, err
}
if resp.StatusCode != http.StatusOK {
return api.Balance{}, fmt.Errorf("failed to get balance %v %v", resp.StatusCode, string(data))
}
var balance api.Balance
return balance, json.Unmarshal(data, &balance)
}
// GetAccountTransactions gets account transactions you can filter the result
// using the fromDate and toDate parameters and the dates need to follow ISO
// 8601 time format
func (b BankingClient) GetAccountTransactions(accountID string, fromDate, toDate string) (api.Transactions, error) {
var url string = fmt.Sprintf("%s/za/pb/v1/accounts/%s/transactions", APIurl, accountID)
if fromDate != "" && toDate != "" {
url += fmt.Sprintf("?fromDate=%s&toDate=%s", fromDate, toDate)
}
resp, err := b.requestAPI(url, http.MethodGet, bearer)
if err != nil {
return api.Transactions{}, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return api.Transactions{}, err
}
if resp.StatusCode != http.StatusOK {
return api.Transactions{}, fmt.Errorf("failed to get account Transactions %v %v", resp.StatusCode, string(data))
}
var transactions api.Transactions
return transactions, json.Unmarshal(data, &transactions)
}