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
|
var request = require('request');
var Timestamp = require('unix-timestamp');
Timestamp.round = true;
var RefreshClient = function(refreshString) {
if (typeof refreshString !== 'string') {
throw new Error('Refresh token must be supplied');
}
this._refreshToken = refreshString;
this._accessToken = '';
this._accessTokenExpiration = Timestamp.now(-1);
};
RefreshClient.prototype._doRequest = function(url) {
var self = this;
var base = 'https://access.active911.com/interface/open_api/api';
return new Promise(function(fulfill, reject) {
self._getAccessToken().then(function(token) {
request({
method: 'GET',
url: base + url,
headers: {
'Authorization': 'Bearer ' + token
}
}, function(err, res, body) {
if (err) {
reject(err);
} else {
var result = JSON.parse(body);
if (result.result == 'success') {
var properties = Object.getOwnPropertyNames(result.message);
if (properties.length == 0) {
fulfill({});
} else {
var propertyToReturn = properties[0];
fulfill(result.message[propertyToReturn]);
}
} else {
reject(result.message);
}
}
});
}).catch(function(err) {
reject(err);
});
});
};
RefreshClient.prototype._getAccessToken = function() {
var self = this;
return new Promise(function(fulfill, reject) {
var currentTime = Timestamp.now();
if (self._accessTokenExpiration - currentTime <= 10) {
fulfill(self._refreshAccessToken());
} else {
fulfill(self._accessToken);
}
});
};
RefreshClient.prototype._refreshAccessToken = function() {
var self = this;
return new Promise(function(fulfill, reject) {
request({
url: 'https://www.active911.com/interface/dev/api_access.php',
method: 'POST',
form: {
'refresh_token': self._refreshToken
}
}, function(err, res, body) {
if (err) {
reject(err);
} else {
var response = JSON.parse(body);
if (response.access_token && response.expiration) {
self._accessToken = response.access_token;
self._accessTokenExpiration = response.expiration;
fulfill(response.access_token);
} else {
reject(new Error('Malformed access token response'));
}
}
});
});
};
RefreshClient.prototype.getAgency = function() {
return this._doRequest('/');
};
RefreshClient.prototype.getDevice = function(id) {
return this._doRequest('/devices/' + id);
};
RefreshClient.prototype.getAlerts = function() {
return this._doRequest('/alerts');
};
RefreshClient.prototype.getAlert = function(id) {
return this._doRequest('/alerts/' + id);
};
RefreshClient.prototype.getDeviceAlerts = function(id) {
return this._doRequest('/devices/' + id + '/alerts');
};
RefreshClient.prototype.getLocations = function() {
return this._doRequest('/locations');
};
RefreshClient.prototype.getLocation = function(id) {
return this._doRequest('/locations/' + id);
};
RefreshClient.prototype.getResource = function(id) {
return this._doRequest('/resources/' + id);
};
module.exports = RefreshClient;
|