forked from keenlabs/KeenClient-Node
/
index.js
188 lines (168 loc) · 5.66 KB
/
index.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
182
183
184
185
186
187
188
var rest = require('superagent');
var _ = require('underscore');
var crypto = require('crypto');
function KeenApi(config) {
if (!config) {
throw new Error("The 'config' parameter must be specified and must be a JS object.");
}
if (!config.projectId) {
throw new Error("The 'config' object must contain a 'projectId'.");
}
this.projectId = config.projectId;
this.writeKey = config.writeKey;
this.readKey = config.readKey;
this.masterKey = config.masterKey;
this.baseUrl = config.baseUrl || 'https://api.keen.io/';
this.apiVersion = config.apiVersion || '3.0';
var baseUrl = this.baseUrl;
var apiVersion = this.apiVersion;
var self = this;
var request = {
get: function(apiKey, path, data, callback) {
rest
.get(baseUrl + apiVersion + path)
.set('Authorization', apiKey)
.send(data || {})
.end(function(err, res) {
processResponse(err, res, callback);
});
},
post: function(apiKey, path, data, callback) {
rest
.post(baseUrl + apiVersion + path)
.set('Authorization', apiKey)
.set('Content-Type', 'application/json')
.send(data || {})
.end(function(err, res) {
processResponse(err, res, callback);
});
},
del: function(apiKey, path, callback) {
rest
.del(baseUrl + apiVersion + path)
.set('Authorization', apiKey)
.set('Content-Length', 0)
.end(function(err, res) {
processResponse(err, res, callback);
});
}
};
// Handle logic of processing response, including error messages
// The error handling should be strengthened over time to be more meaningful and robust
function processResponse(err, res, callback) {
callback = callback || function() {};
if (res && !res.ok && !err) {
var is_err = res.body && res.body.error_code;
err = new Error(is_err ? res.body.message : 'Unknown error occurred');
err.code = is_err ? res.body.error_code : 'UnknownError';
}
if (err) return callback(err);
return callback(null, res.body);
}
// Public Methods
this.projects = {
list: function(callback) {
request.get(this.masterKey, '/projects', callback);
},
view: function(projectId, callback) {
request.get(this.masterKey, '/projects/' + projectId, callback);
}
};
this.events = {
list: function(projectId, callback) {
request.get(this.masterKey, '/projects/' + projectId + '/events', callback);
},
insert: function(projectId, events, callback) {
events = events || [];
var data = {};
events.forEach(function(event) {
var collection = event.collection;
if (typeof data[collection] == 'undefined') {
data[collection] = [];
}
var item = (event.data || {});
if (typeof event.keen == 'object') {
item.keen = event.keen;
}
data[collection].push(item);
});
request.post(this.writeKey, '/projects/' + projectId + '/events', data, callback);
}
};
this.properties = {
view: function(projectId, collection, property, callback) {
request.get(this.masterKey, '/projects/' + projectId + '/events/' + collection + '/properties/' + property, callback);
},
remove: function(projectId, collection, property, callback) {
request.del(this.masterKey, '/projects/' + projectId + '/events/' + collection + '/properties/' + property, callback);
}
};
this.collections = {
view: function(projectId, collection, callback) {
request.get(this.masterKey, '/projects/' + projectId + '/events/' + collection, callback);
},
remove: function(projectId, collection, callback) {
request.del(this.masterKey, '/projects/' + projectId + '/events/' + collection, callback);
}
};
this.addEvent = function(eventCollection, event, callback) {
if (!this.writeKey) {
var errorMessage = "You must specify a non-null, non-empty 'writeKey' in your 'config' object when calling keen.configure()!";
var error = new Error(errorMessage);
if (callback) {
callback(error);
} else {
throw error;
}
return;
}
request.post(this.writeKey, "/projects/" + this.projectId + "/events/" + eventCollection, event, callback);
};
this.addEvents = function(events, callback) {
if (!this.writeKey) {
var errorMessage = "You must specify a non-null, non-empty 'writeKey' in your 'config' object when calling keen.configure()!";
var error = new Error(errorMessage);
if (callback) {
callback(error);
} else {
throw error;
}
return;
}
request.post(this.writeKey, "/projects/" + this.projectId + "/events", events, callback);
};
this.queries = {
extraction: function(projectId, collection, params, callback){
var path = '/projects/' + projectId + '/queries/extraction?event_collection=' + collection;
request.get(self.readKey, path, params, callback);
}
};
}
function configure(config) {
return new KeenApi(config);
}
function encryptScopedKey(apiKey, options) {
var iv = crypto.randomBytes(16);
var cipher = crypto.createCipheriv("aes-256-cbc", apiKey, iv);
var jsonOptions = JSON.stringify(options);
var encryptOutput1 = cipher.update(jsonOptions, "utf8", "hex");
var encryptOutput2 = cipher.final("hex");
var ivPlusEncrypted = iv.toString("hex") + encryptOutput1 + encryptOutput2;
return ivPlusEncrypted;
}
function decryptScopedKey(apiKey, scopedKey) {
var hexedIv = scopedKey.substring(0, 32);
var hexedCipherText = scopedKey.substring(32, scopedKey.length);
var iv = new Buffer(hexedIv, "hex");
var cipherText = new Buffer(hexedCipherText, "hex");
var decipher = crypto.createDecipheriv("aes-256-cbc", apiKey, iv);
var decryptOutput1 = decipher.update(cipherText);
var decryptOutput2 = decipher.final();
var decrypted = decryptOutput1 + decryptOutput2;
return JSON.parse(decrypted);
}
module.exports = {
configure: configure,
encryptScopedKey: encryptScopedKey,
decryptScopedKey: decryptScopedKey
};