-
Notifications
You must be signed in to change notification settings - Fork 1
/
monzo.go
171 lines (159 loc) · 4.43 KB
/
monzo.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
package myzo
import (
"bytes"
"encoding/json"
utils "github.com/Noy/Go-Utilities"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
)
const (
BaseURL = "https://api.monzo.com"
BalanceEndpoint = "/balance"
TransactionsEndpoint = "/transactions"
AccountEndpoint = "/accounts"
PotsEndpoint = "/pots"
)
type Myzo struct {
ClientID string
UserID string
AccessToken string
AccountIDs map[string]string
Debug bool
ResponseBody []byte
}
/**
Authentication with Monzo.
*/
func (auth *Myzo) authenticate(method, url string, data io.Reader) ([]byte, error) {
client := &http.Client{Timeout: time.Second * 2}
req, err := http.NewRequest(method, url, data)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+auth.AccessToken)
if method == "PUT" || method == "POST" {
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
jsonResponse, err := ioutil.ReadAll(resp.Body)
if auth.Debug {
log.Println(url)
auth.ResponseBody = jsonResponse
log.Println(utils.JsonPrettyPrint(string(jsonResponse)))
}
if err != nil {
return nil, err
}
return jsonResponse, nil
}
/**
For all GET requests
*/
func (auth *Myzo) getFromMonzo(endpoint, params, accountId string) ([]byte, error) {
var accountIdParam = "?account_id="
if endpoint == PotsEndpoint {
accountIdParam = "?current_account_id="
}
jsonResponse, err := auth.authenticate("GET", BaseURL+endpoint+accountIdParam+accountId+params, nil)
if err != nil {
return nil, err
}
return jsonResponse, nil
}
/**
For all POST/PUT requests, please note the authentication adds the 'application/x-www-form-urlencoded' for all of these requests.
*/
func (auth *Myzo) postOrPutToMonzo(method, endPoint, extraData string, data url.Values) ([]byte, error) {
resp, err := auth.authenticate(method, BaseURL+endPoint+extraData, bytes.NewBufferString(data.Encode()))
if err != nil {
return nil, err
}
return resp, nil
}
/**
Base request for handling balance responses.
*/
func (auth *Myzo) balanceResponseHandler(accountId string) (*BalanceResponse, error) {
resp, err := auth.getFromMonzo(BalanceEndpoint, "", accountId)
if err != nil {
return nil, err
}
var balanceResponse BalanceResponse
if err := json.Unmarshal(resp, &balanceResponse); err != nil {
return nil, err
}
return &balanceResponse, nil
}
/**
Base request for handling Pot responses.
*/
func (auth *Myzo) potResponseHandler(accountId string) (*PotResponse, error) {
resp, err := auth.getFromMonzo(PotsEndpoint, "", accountId)
if err != nil {
return nil, err
}
var potResponse PotResponse
if err := json.Unmarshal(resp, &potResponse); err != nil {
return nil, err
}
return &potResponse, nil
}
/**
Base request for handling transaction responses.
*/
func (auth *Myzo) transactionResponseHandler(bulkRequest bool, from, to string, expandBy, optionalId, accountId string) (*TransactionsResponse, error) {
var resp []byte
var err error
if bulkRequest {
resp, err = auth.getFromMonzo(TransactionsEndpoint+optionalId, "&since="+from+"&expand[]="+expandBy+"&before="+to, accountId)
} else {
resp, err = auth.getFromMonzo(TransactionsEndpoint+optionalId, "&expand[]="+expandBy, accountId)
}
if err != nil {
return nil, err
}
var transactionResponse TransactionsResponse
if err := json.Unmarshal(resp, &transactionResponse); err != nil {
return nil, err
}
return &transactionResponse, nil
}
/**
Base request for handling account responses.
*/
func (auth *Myzo) accountResponseHandler() (*AccountResponse, error) {
resp, err := auth.getFromMonzo(AccountEndpoint, "", "") // no need for account ID
if err != nil {
return nil, err
}
var accountResponse AccountResponse
if err := json.Unmarshal(resp, &accountResponse); err != nil {
return nil, err
}
return &accountResponse, nil
}
/**
Send custom feed items to your timeline.
*/
func (auth *Myzo) FeedHandler(URL string, params [6]string, accountId string) ([]byte, error) {
data := url.Values{}
data.Set("params[title]", params[0])
data.Set("params[image_url]", params[1])
data.Set("params[background_color]", params[2])
data.Set("params[body_color]", params[3])
data.Set("params[title_color]", params[4])
data.Set("params[body]", params[5])
respBody, err := auth.postOrPutToMonzo("POST", "/feed", "?account_id="+accountId+"&type=basic"+"&url="+URL, data)
if err != nil {
return nil, err
}
return respBody, nil
}