-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mondo.js
181 lines (128 loc) · 4.16 KB
/
Mondo.js
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
import _ from 'lodash';
import oauth2 from 'simple-oauth2';
const API = 'https://production-api.gmon.io';
const ENDPOINTS = {
AUTHENTICATION: {
OAUTH2: '/oauth2/token',
PING: '/ping/whoami'
},
ACCOUNTS: '/accounts',
BALANCE: '/balance?account_id=<%= accountId %>',
TRANSACTION: '/transactions/<%= transactionId %>',
TRANSACTIONS: '/transactions',
FEED: '/feed'
};
export {ENDPOINTS, API};
export default class Mondo {
constructor(clientId, clientSecret, api = API) {
if (!_.isString(clientId) || !_.isString(clientSecret)) {
throw new Error('Client id and client password must be defined for instance construction.');
}
this.oauth2 = oauth2;
this.__oauthSet();
this.oauth = this.oauth2({
clientID: clientId,
clientSecret: clientSecret,
site: api,
tokenPath: api+ENDPOINTS.AUTHENTICATION.OAUTH2,
authorizationPath: api+ENDPOINTS.AUTHENTICATION.OAUTH2
});
}
__oauthSet () {
// This is for testing purposes. Oath2 is not easily shim-able as it keeps a lot of it's internals private.
}
auth (username, password) {
if (!_.isString(username) || !_.isString(password)) {
throw new Error('Username and password must be defined for authorization.');
}
return this.oauth.password.getToken({username, password})
.then((result) => {
this.token = this.oauth.accessToken.create(result);
return this.token;
});
;
}
expired () {
if(_.isUndefined(this.token)) {
throw new Error('Token not found.');
}
return this.token.expired();
}
refresh () {
if(_.isUndefined(this.token)) {
throw new Error('Token not found.');
}
return this.token.refresh();
}
revoke () {
if(_.isUndefined(this.token)) {
throw new Error('Token not found.');
}
console.warn('No revoke enpoint available.');
return new Promise((resolve, reject) => {
reject();
})
// token.revoke('refresh_token');
}
accounts () {
if(_.isUndefined(this.token)) {
throw new Error('Token not found.');
}
return this.oauth.api('GET', ENDPOINTS.ACCOUNTS, { access_token: this.token.token.access_token });
}
balance (accountId) {
if(_.isUndefined(this.token)) {
throw new Error('Token not found.');
}
if(!_.isString(accountId)) {
throw new Error('Account id is require to lookup balance.');
}
let endpoint = _.template(ENDPOINTS.BALANCE)({accountId});
return this.oauth.api('GET', endpoint, { access_token: this.token.token.access_token });
}
transactions (accountId) {
if(_.isUndefined(this.token)) {
throw new Error('Token not found.');
}
return this.oauth.api('GET', ENDPOINTS.TRANSACTIONS, { access_token: this.token.token.access_token, account_id: accountId });
}
transaction (transactionId) {
if(_.isUndefined(this.token)) {
throw new Error('Token not found.');
}
if(!_.isString(transactionId)) {
throw new Error('Transaction id is required to look up single transaction.');
}
let endpoint = _.template(ENDPOINTS.TRANSACTION)({transactionId});
return this.oauth.api('GET', endpoint, { access_token: this.token.token.access_token });
}
annotate (transactionId, annotations) {
if(!_.isString(transactionId) || _.isEmpty(annotations)) {
throw new Error('Transaction id is required to specify which transaction to annotate.');
}
let endpoint = _.template(ENDPOINTS.TRANSACTION)({transactionId});
let map = {};
_.forEach(annotations, (val, key) => {
map[`metadata[${key}]`] = val;
});
return this.oauth.api('PATCH', endpoint, _.extend({}, {access_token: this.token.token.access_token}, map));
}
feed (accountId, item = {}) {
if(_.isUndefined(this.token)) {
throw new Error('Token not found.');
}
const REQUIRED_KEYS = ['params[title]', 'params[image_url]'];
if(_.every(REQUIRED_KEYS, _.hasOwnProperty, item) === false) {
throw new Error('Both title and image_url are required for a new feed item.');
}
if(_.isString(accountId) === false) {
throw new Error('An account id is required to add items to it\'s feed');
}
let defaults = {
type: 'basic'
}
let headers = _.defaults(defaults, item);
headers.account_id = accountId;
return this.oauth.api('POST', ENDPOINTS.FEED, _.extend({}, {access_token: this.token.token.access_token}, headers));
}
}