-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathautomatic-api.js
179 lines (133 loc) · 5.57 KB
/
automatic-api.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
// a node.js module to interface with the Automatic cloud API
// cf., https://www.automatic.com/developer/
var events = require('events')
, oauth = require('oauth')
, util = require('util')
, uuid = require('node-uuid')
;
var DEFAULT_LOGGER = { error : function(msg, props) { console.log(msg); if (!!props) console.log(props); }
, warning : function(msg, props) { console.log(msg); if (!!props) console.log(props); }
, notice : function(msg, props) { console.log(msg); if (!!props) console.log(props); }
, info : function(msg, props) { console.log(msg); if (!!props) console.log(props); }
, debug : function(msg, props) { console.log(msg); if (!!props) console.log(props); }
};
var AutomaticAPI = function(options) {
var k;
var self = this;
if (!(self instanceof AutomaticAPI)) return new AutomaticAPI(options);
self.options = options;
if ((!self.options.clientID) || (!self.options.clientSecret)) throw new Error('clientID and clientSecret required');
self.logger = self.options.logger || {};
for (k in DEFAULT_LOGGER) {
if ((DEFAULT_LOGGER.hasOwnProperty(k)) && (typeof self.logger[k] === 'undefined')) self.logger[k] = DEFAULT_LOGGER[k];
}
self.oauth2 = new oauth.OAuth2(self.options.clientID, self.options.clientSecret, 'https://www.automatic.com',
'/oauth/authorize', '/oauth/access_token');
self.oauth2.setAuthMethod('token'); // not 'Bearer'
};
util.inherits(AutomaticAPI, events.EventEmitter);
AutomaticAPI.prototype.setState = function(state) {
var self = this;
self.state = state;
return self;
};
AutomaticAPI.prototype.authenticateURL = function(scopes, redirectURL) {
var params;
var self = this;
if (!scopes) scopes = AutomaticAPI.allScopes;
self.cookie = uuid.v4();
params = { scope : scopes.join(' ')
, response_type : 'code'
, redirect_url : redirectURL
, state : self.cookie
};
if (!!redirectURL) params.redirect_url = redirectURL;
return self.oauth2.getAuthorizeUrl(params);
};
AutomaticAPI.prototype.authorize = function(code, state, callback) {
var self = this;
if (typeof callback !== 'function') throw new Error('callback is mandatory for login');
if (self.cookie !== state) callback(new Error('cross-site request forgery suspected'));
self.oauth2.getOAuthAccessToken(code, { grant_type: 'authorization_code'},
function (err, accessToken, refreshToken, results) {
var json;
if (!!err) {
if ((!err.message) && (!!err.data)) {
try { json = JSON.parse(err.data); err = new Error(err.statusCode + ': ' + json.error_description); } catch(ex) {}
}
return callback(err);
}
if (!!results.expires_in) self.expiresAt = new Date().getTime() + (results.expires_in * 1000);
self.state = { id : results.user.id
, scopes : (!!results.scope) ? results.scope.split(' ') : null
, cookie : self.cookie
, accessToken : accessToken
, refreshToken : refreshToken
, expiresAt : self.expiresAt
};
callback(null, results.user, self.state, self.state.scopes);
});
return self;
};
AutomaticAPI.prototype.roundtrip = function(method, path, json, callback) {
var self = this;
if ((!callback) && (typeof json === 'function')) {
callback = json;
json = null;
}
return self.invoke(method, path, json, function(err, code, results) {
callback(err, results);
});
};
AutomaticAPI.prototype.invoke = function(method, path, json, callback) {
var headers;
var self = this;
if ((!callback) && (typeof json === 'function')) {
callback = json;
json = null;
}
if (!callback) {
callback = function(err, results) {
if (!!err) self.logger.error('invoke', { exception: err }); else self.logger.info(path, { results: results });
};
}
headers = { Authorization: self.oauth2.buildAuthHeader(self.state.accessToken) };
if (!!json) {
headers['Content-Type'] = 'application/json';
headers['Content-Length'] = json.length;
}
self.oauth2._request(method, 'https://api.automatic.com/v1' + path, headers, json, null, function(oops, body, response) {
var expected = { GET : [ 200 ]
, PUT : [ 200 ]
, POST : [ 200, 201, 202 ]
, DELETE : [ 200 ]
}[method];
var results = {};
if (!!oops) return callback(new Error(oops.data), oops.statusCode);
try { results = JSON.parse(body); } catch(ex) {
self.logger.error(path, { event: 'json', diagnostic: ex.message, body: body });
return callback(ex, response.statusCode);
}
if (expected.indexOf(response.statusCode) === -1) {
self.logger.error(path, { event: 'https', code: response.statusCode, body: body });
return callback(new Error('HTTP response ' + response.statusCode), response.statusCode, results);
}
callback(null, response.statusCode, results);
});
return self;
};
AutomaticAPI.allScopes =
[ 'scope:location'
, 'scope:vehicle'
, 'scope:trip:summary'
, 'scope:ignition:on'
, 'scope:ignition:off'
, 'scope:notification:speeding'
, 'scope:notification:hard_brake'
, 'scope:notification:hard_accel'
, 'scope:region:changed'
, 'scope:parking:changed'
, 'scope:mil:on'
, 'scope:mil:off'
];
exports.AutomaticAPI = AutomaticAPI;