-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
179 lines (153 loc) · 5.67 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
var _ = require('underscore');
var crypto = require('crypto');
var KeenRequests = require('./lib/requests');
var KeenQuery = require('./lib/query');
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;
// Public Methods
this.projects = {
list: function(callback) {
KeenRequests.get.call(self, self.masterKey, '/projects', null, callback);
},
view: function(projectId, callback) {
KeenRequests.get.call(self, self.masterKey, '/projects/' + projectId, null, callback);
}
};
this.events = {
list: function(projectId, callback) {
KeenRequests.get.call(self, self.masterKey, '/projects/' + projectId + '/events', null, 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);
});
KeenRequests.post.call(self, self.writeKey, '/projects/' + projectId + '/events', data, callback);
}
};
this.properties = {
view: function(projectId, collection, property, callback) {
KeenRequests.get.call(self, self.masterKey, '/projects/' + projectId + '/events/' + collection + '/properties/' + property, null, callback);
},
remove: function(projectId, collection, property, callback) {
KeenRequests.del.call(self, self.masterKey, '/projects/' + projectId + '/events/' + collection + '/properties/' + property, callback);
}
};
this.collections = {
view: function(projectId, collection, callback) {
KeenRequests.get.call(self, self.masterKey, '/projects/' + projectId + '/events/' + collection, null, callback);
},
remove: function(projectId, collection, callback) {
KeenRequests.del.call(self, self.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;
}
KeenRequests.post.call(self, this.writeKey, "/projects/" + this.projectId + "/events/" + eventCollection, event, callback);
};
this.request = function(method, keyType, path, params, callback) {
method = typeof method === 'string' && method.toLowerCase();
keyType += 'Key';
callback = callback || (typeof params === 'function') && params;
if (typeof path === 'string') {
path = '/projects/' + this.projectId + '/' + path.replace(/^\//,'');
} else {
throw new Error('\'path\' must be a string.');
}
if ( ! KeenRequests.hasOwnProperty(method)) {
throw new Error('Method must be of type: GET/POST/DEL');
}
if (!this.hasOwnProperty(keyType)) {
throw new Error('Key must be of type: master/write/read');
}
if (!this[keyType]) {
throw new Error('You must specify a nun-null, non-empty \'' + keyType + '\' in your config object.');
}
if(method === 'post' || method === 'get') {
return KeenRequests[method].call(self, this[keyType], path, params, callback);
}
KeenRequests[method].call(self, this[keyType], path, 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;
}
KeenRequests.post.call(self, this.writeKey, "/projects/" + this.projectId + "/events", events, callback);
};
this.queries = {
extraction: function(projectId, collection, params, callback){
var requestParams = _.extend({}, params, { 'event_collection': collection });
var path = '/projects/' + projectId + '/queries/extraction';
KeenRequests.get.call(self, self.readKey, path, requestParams, callback);
}
};
this.run = KeenQuery.client.run;
}
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,
Query: KeenQuery.Query
};