-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monzo.go
214 lines (173 loc) · 5.47 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
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
207
208
209
210
211
212
213
214
package monzo
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// APIBase is the root of the Monzo API.
const APIBase string = "https://api.monzo.com"
// Client is the way to interact with the Monzo API.
type Client struct {
Token string
http.Client
}
// NewClient uses the passed token to create a new Monzo Client.
// No validation is done on this method so users should call
// the Ping method after creating a client to ensure that
// the new connection has been created successsfully.
func NewClient(token string) *Client {
return &Client{
Token: token,
}
}
// Ping attempts to connect to the Monzo API using the given
// client.
func (c *Client) Ping() error {
if c.Token == "" {
return errors.New("error pinging Monzo API. Client token cannot be empty")
}
req, err := c.NewRequest(http.MethodGet, "ping/whoami", nil)
if err != nil {
return err
}
resp, _ := c.Do(req)
if resp.StatusCode != http.StatusOK {
b := new(bytes.Buffer)
b.ReadFrom(resp.Body)
str := b.String()
return fmt.Errorf("error pinging Monzo API. JSON response: %s", str)
}
return nil
}
// NewRequest creates an *http.Request with some Monzo-specific
// sensible defaults, such as Authorization headers.
func (c *Client) NewRequest(method string, endpoint string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, APIBase+"/"+endpoint, body)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+c.Token)
return req, nil
}
func (c *Client) resourceRequest(resource string) (*http.Request, error) {
req, err := c.NewRequest(http.MethodGet, resource, nil)
if err != nil {
return nil, err
}
return req, nil
}
// Account returns a single Account from the Monzo API. If the
// account is not found (does not exist), an error is returned.
func (c *Client) Account(id string) (Account, error) {
accs, err := c.accounts()
if err != nil {
return Account{}, err
}
// Monzo doesn't have the capability to retrieve a single
// account, so we need to get all the users accounts
// and filter them down using the provided id.
for _, acc := range accs {
if acc.ID == id {
return acc, nil
}
}
return Account{}, fmt.Errorf("no account found with ID %s", id)
}
// Accounts returns a slice of Account structs, one for each of
// the Monzo accounts associated with the authentication.
func (c *Client) Accounts() ([]Account, error) {
return c.accounts()
}
func (c *Client) accounts() ([]Account, error) {
req, err := c.resourceRequest("accounts")
if err != nil {
return nil, err
}
resp, _ := c.Do(req)
b := new(bytes.Buffer)
b.ReadFrom(resp.Body)
str := b.String()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to fetch accounts: %s", str)
}
bytes := b.Bytes()
var accounts []Account
if err := unwrapJSON(bytes, "accounts", &accounts); err != nil {
return nil, err
}
var accs []Account
for _, acc := range accounts {
// The API still returns the Monzo beta prepaid accounts.
// These can't be actioned meaningfully, so they are
// removed from the slice if they exist.
if acc.Type != PrepaidAccount {
// To provide a fluent API for the account, it needs
// to know how to talk to Monzo. The monzo.Client
// is embedded in the Account struct so that
// calls can be passed to it.
acc.client = c
accs = append(accs, acc)
}
}
return accs, nil
}
// Deposit creates a new Deposit struct. Monzo uses a 'dedupe_id'
// to ensure that the request is idempotent, so the deposit is
// not ran when it is created. To action the deposit, call
// the `Run` method on it.
func (a Account) Deposit(p Pot, amt int) (*Deposit, error) {
endpoint := "/pots/" + p.ID + "/deposit"
data := url.Values{}
data.Add("source_account_id", a.ID)
data.Add("amount", strconv.Itoa(amt))
src := rand.NewSource(time.Now().UnixNano())
r := rand.New(src)
data.Add("dedupe_id", strconv.FormatFloat(r.Float64(), 'f', 6, 64))
req, err := a.client.NewRequest(http.MethodPut, endpoint, strings.NewReader(data.Encode()))
if err != nil {
return &Deposit{}, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
return &Deposit{req, &a.client.Client}, nil
}
// Withdraw creates a new Withdrawal struct. Monzo uses a 'dedupe_id'
// to ensure that the request is idempotent, so the withdrawal is
// not ran when it is created. To action the withdrawal, call
// the `Run` method on it.
func (a Account) Withdraw(p Pot, amt int) (*Withdrawal, error) {
endpoint := "/pots/" + p.ID + "/withdraw"
data := url.Values{}
data.Add("destination_account_id", a.ID)
data.Add("amount", strconv.Itoa(amt))
src := rand.NewSource(time.Now().UnixNano())
r := rand.New(src)
data.Add("dedupe_id", strconv.FormatFloat(r.Float64(), 'f', 6, 64))
req, err := a.client.NewRequest(http.MethodPut, endpoint, strings.NewReader(data.Encode()))
if err != nil {
return &Withdrawal{}, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
return &Withdrawal{req, &a.client.Client}, nil
}
// unwrapJSON takes a JSON response and unmarshals the contents
// of the first item. Responses from Monzo are wrapped in a key
// pertaining to the resource, which needs removing before
// unmarshalling.
func unwrapJSON(data []byte, wrapper string, v interface{}) error {
var objmap map[string]*json.RawMessage
if err := json.Unmarshal(data, &objmap); err != nil {
return err
}
if err := json.Unmarshal(*objmap[wrapper], v); err != nil {
return err
}
return nil
}