Permalink
Browse files

Normalize request mechanism

New APIs should be a lot easier to add now
  • Loading branch information...
1 parent c6dd3fe commit 15589d8121f33a671f5f63682d6bb02c77eec28b @agnoster agnoster committed Feb 1, 2011
Showing with 60 additions and 58 deletions.
  1. +60 −58 nodester.js
View
@@ -18,78 +18,80 @@ var nodester = function (username, password, basehost) {
};
function process_response(cb, errfn) {
- return function(err, response, body) {
- var errCode = null;
- if (!err && response && response.statusCode >= 400) {
- errCode = response.statusCode;
- }
- var data = {};
- try {
- data = JSON.parse(body);
- } catch (e) {
- if (!err) err = { message: body };
- data = body;
- }
- if (errfn) {
- if (!err) err = errfn(data);
- } else {
- if (!err && data.status && data.status != 'success') err = { message: data.status }
- }
- if (errCode) {
- if (err) { err.code = errCode; err.message = '[HTTP ' + errCode + '] ' + err.message; }
- else err = { code: errCode, message: "HTTP Error " + errCode };
- }
- cb(err, data);
+ return function(err, response, body) {
+ var errCode = null;
+ if (!err && response && response.statusCode >= 400) {
+ errCode = response.statusCode;
}
+ var data = {};
+ try {
+ data = JSON.parse(body);
+ } catch (e) {
+ if (!err) err = { message: body };
+ data = body;
+ }
+ if (errfn) {
+ if (!err) err = errfn(data);
+ } else {
+ if (!err && data.status && !/^success/.exec(data.status)) err = { message: data.status }
+ }
+ if (errCode) {
+ if (err) { err.code = errCode; err.message = '[HTTP ' + errCode + '] ' + err.message; }
+ else err = { code: errCode, message: "HTTP Error " + errCode };
+ }
+ cb(err, data);
+ }
}
-nodester.prototype.coupon_request = function (email, cb) {
- request({uri: this.baseurl + "coupon", method: "POST", body: querystring.stringify({email: email}), headers: headers}, process_response(cb, function (data) {
- if (!data.status) return data;
- if (!/^success/.exec(data.status)) {
- return { message: data.status };
- }
- return null;
- })
+nodester.prototype.request = function(method, path, body, cb) {
+ request({
+ uri: this.baseurl + path,
+ method: method,
+ body: querystring.stringify(body),
+ headers: headers
+ },
+ process_response(cb)
);
-};
+}
+
+nodester.prototype.get = function(path, cb) { this.request('GET', path, null, cb); }
+nodester.prototype.post = function(path, body, cb) { this.request('POST', path, body, cb); }
+nodester.prototype.put = function(path, body, cb) { this.request('PUT', path, body, cb); }
+nodester.prototype.del = function(path, body, cb) { this.request('DELETE', path, body, cb); }
+
+nodester.prototype.coupon_request = function (email, cb) {
+ this.post('coupon', { email: email }, cb)
+}
nodester.prototype.user_create = function (user, pass, email, rsakey, coupon, cb) {
- var rsadata = fs.readFileSync(rsakey).toString();
- if (rsadata.length < 40) {
- cb({ message: "Error: Invalid SSH key file." });
- } else {
- request({
- uri: this.baseurl + "user",
- method: 'POST',
- body: querystring.stringify({
- user: user,
- password: pass,
- email: email,
- coupon: coupon,
- rsakey: rsadata
- }),
- headers: headers
- },
- process_response(cb)
- );
- }
+ fs.readFile(rsakey, 'ascii', function(err, rsadata) {
+ if (err) cb(err, null);
+ if (rsadata.length < 40) {
+ cb({ message: "Error: Invalid SSH key file." });
+ } else {
+ this.post('user', { user: user, password: pass, email: email, coupon: coupon, rsakey: rsadata }, cb);
+ }
+ }.bind(this));
};
nodester.prototype.user_setpass = function (newpass, cb) {
- request({uri: this.baseurl + "user", method: 'PUT', body: querystring.stringify({password: newpass}), headers: headers}, process_response(cb));
+ this.put('user', { password: newpass }, cb);
};
nodester.prototype.user_setkey = function (rsakey, cb) {
- request({uri: this.baseurl + "user", method: 'PUT', body: querystring.stringify({rsakey: rsakey}), headers: headers}, process_response(cb));
+ this.put('user', { rsakey: rsakey }, cb);
};
nodester.prototype.apps_list = function (cb) {
- request({uri: this.baseurl + "apps", method: 'GET'}, process_response(cb));
+ this.get('apps', cb);
};
nodester.prototype.app_create = function (name, start, cb) {
- request({uri: this.baseurl + "app", method: 'POST', body: querystring.stringify({appname: name, start: start}), headers: headers}, process_response(cb));
+ this.post('app', { appname: name, start: start }, cb);
+};
+
+nodester.prototype.status = function (cb) {
+ this.get('status', cb);
};
/*
@@ -99,7 +101,7 @@ nodester.prototype.app_set_start = function (name, start, cb) {
*/
nodester.prototype.app_running = function (name, running, cb) {
- request({uri: this.baseurl + "app", method: 'PUT', body: querystring.stringify({appname: name, running: running}), headers: headers}, process_response(cb));
+ this.put('app', { appname: name, running: running }, cb);
};
nodester.prototype.app_start = function (name, cb) {
@@ -115,15 +117,15 @@ nodester.prototype.app_stop = function (name, cb) {
};
nodester.prototype.app_delete = function (name, cb) {
- request({uri: this.baseurl + "app", method: 'DELETE', body: querystring.stringify({appname: name}), headers: headers}, process_response(cb));
+ this.del('app', { appname: name }, cb);
};
nodester.prototype.app_info = function (name, cb) {
- request({uri: this.baseurl + "app/" + name, method: 'GET', headers: headers}, process_response(cb));
+ this.get('app/' + name, cb);
};
nodester.prototype.appnpm_handler = function (name, package, action, cb) {
- request({uri: this.baseurl + "appnpm", method: 'POST', headers: headers, body: querystring.stringify({appname: name, package: package, action: action})}, process_response(cb));
+ this.post('appnpm', { appname: name, package: package, action: action }, cb);
};
nodester.prototype.appnpm_install = function (name, package, cb) {

0 comments on commit 15589d8

Please sign in to comment.