This repository has been archived by the owner on Oct 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
executable file
·205 lines (165 loc) · 3.99 KB
/
client.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env node
'use strict';
var _ = require('lodash');
var rest = require('restler');
var Promise = require('bluebird');
Promise.longStackTraces();
var urls = require('url');
var util = require('util');
var VError = require('verror');
// @todo: @bcauldwell - Needs to point to the production instance.
// @todo: @bcauldwell - Should probably be https, and there should
// be some barebones security.
/*
* Default target.
*/
var DEFAULT_TARGET = {
protocol: 'http',
hostname: '127.0.0.1',
port: '3030'
};
/*
* Return a formatted string (aka pretty print).
*/
var pp = function(obj) {
return JSON.stringify(obj, null, ' ');
};
/*
* Return current time in JSON format.
*/
var ts = function() {
return new Date().toJSON();
};
/*
* Constructor.
*/
function Client(id, address) {
// @todo: @bcauldwell - Do some argument processing here.
// The id argument is optional.
this.id = id;
// The address argument is also optional.
if (address) {
this.target = urls.parse(address);
} else {
// Grab the default target that points to the production instance.
this.target = DEFAULT_TARGET;
}
}
/*
* Send and handle a REST request.
*/
Client.prototype.__request = function(verb, pathname, data) {
// Save for later.
var self = this;
// Build url.
return Promise.try(function() {
var obj = _.extend(self.target, {pathname: pathname});
return urls.format(obj);
})
.then(function(url) {
// Send REST request.
return new Promise(function(fulfill, reject) {
rest[verb](url, data)
.on('success', fulfill)
.on('fail', function(data, resp) {
var err = new Error(pp(data));
reject(err);
})
.on('error', reject);
})
// Give request a 10 second timeout.
.timeout(10 * 1000)
// Wrap errors for more information.
.catch(function(err) {
var dataString = typeof data === 'object' ? JSON.stringify(data) : data;
throw new VError(err,
'Error during REST request. url=%s, data=%s.',
[verb, url].join(':'),
dataString
);
});
});
};
/*
* Get full list of all metrics record ids.
*/
Client.prototype.getAll = function(username, password) {
var opts = {
username: username,
password: password
};
return this.__request('get', 'metrics/v1/admin', opts);
};
/*
* Get one metric record.
*/
Client.prototype.getOne = function(id, username, password) {
var opts = {
username: username,
password: password
};
return this.__request('get', 'metrics/v1/admin/' + id, opts);
};
/*
* Create a new metric record and return it's ID.
*/
Client.prototype.create = function() {
// Send REST request.
return this.__request('post', 'metrics/v1/')
// Validate response and return ID.
.then(function(data) {
if (!data || !data.id) {
throw new Error('Invalid create response: ' + pp(data));
} else {
return data.id;
}
});
};
/*
* Return the metric record's ID, or create one if it doesn't have one.
*/
Client.prototype.__getId = function() {
var self = this;
if (self.id) {
// ID is already set, just return it.
return Promise.resolve(self.id);
} else {
// No metic record exists, so create one.
return self.create()
.tap(function(id) {
self.id = id;
});
}
};
/*
* Get the current metric record to this instance.
*/
Client.prototype.get = function() {
var self = this;
// Get ID.
return self.__getId()
// Send REST request.
.then(function(id) {
return self.__request('get', 'metrics/v1/' + id);
});
};
/*
* Report meta data for this metric client instance.
*/
Client.prototype.report = function(metaData) {
// Save for later.
var self = this;
// Build request data.
var record = {
created: ts(),
data: metaData
};
// Get this metric client's ID.
return self.__getId()
// Send REST request.
.then(function(id) {
return self.__request('putJson', 'metrics/v1/' + id, record);
});
};
// Return constructor as the module object.
module.exports = Client;