Permalink
Browse files

Use the new HTTP request, reuse most of the functionality in differen…

…t requests
  • Loading branch information...
1 parent fa1de22 commit b6689814e0ae279ddca4bb403bda723c0d090812 @whizz whizz committed Dec 1, 2011
Showing with 32 additions and 100 deletions.
  1. +32 −100 lib/hbase-connection.js
View
@@ -1,131 +1,63 @@
var http = require('http');
-function createRequest(method,options, command, callback){
- var client = http.createClient(options.port,options.host);
- var request = client.request(method, command, {
- host: options.host,
- 'content-type':'application/json',
- 'Accept': 'application/json'
- });
- // After node v0.3.4 or something, socket is no longer accessible
- if( request.socket && request.socket.setTimeout ){
- request.socket.setTimeout(options.timeout);
- request.socket.addListener("timeout", function() {
- request.socket.destroy();
- var e = new Error('408: Client Request Timeout');
- e.code = 408;
- e.body = 'Client Request Timeout';
- callback(e);
- });
- }
- return request;
-}
-
var Connection = function(client){
this.client = client;
};
-Connection.prototype.get = function(command,callback){
+Connection.prototype.makeRequest = function(method, command, data, callback) {
var self = this;
- var request = createRequest('GET', this.client.options, command, callback);
- request.end();
- request.on('response',function(response){
- var body = '';
- response.on('data', function(chunk) {
+ var options = {
+ port: this.client.options.port,
+ host: this.client.options.host,
+ method: method,
+ path: command,
+ headers: {
+ 'content-type':'application/json',
+ 'Accept': 'application/json'
+ }
+ };
+ var req = http.request(options, function(res) {
+ var body = '';
+ res.on('data', function(chunk) {
body += chunk;
});
- response.on('end', function() {
+ res.on('end', function() {
var error = null;
try{
- body = self.handleJson(response,body);
+ body = self.handleJson(res,body);
}catch(e){
body = null;
error = e;
}
- callback(error, body, response);
+ callback(error, body, res);
});
- response.on('error', function(error) {
- callback(error,null);
+ res.on('close', function() {
+ var e = new Error('Connectino closed');
+ callback(e,null);
});
});
+ if (data && data != '') {
+ data = typeof data === 'string' ? data : JSON.stringify(data);
+ req.write(data, 'utf8');
+ }
+ req.end();
+};
+
+Connection.prototype.get = function(command,callback){
+ this.makeRequest('GET', command, '', callback);
};
Connection.prototype.put = function(command,data,callback){
- var self = this;
- var request = createRequest('PUT', this.client.options, command);
- data = typeof data === 'string' ? data : JSON.stringify(data);
- request.end(data, 'utf8');
- request.on('response',function(response){
- var body = '';
- response.on('data', function(chunk) {
- body += chunk;
- });
- response.on('end', function() {
- var error = null;
- try{
- body = self.handleJson(response, body);
- }catch(e){
- body = null;
- error = e;
- }
- callback(error, body, response);
- });
- response.on('error', function(error) {
- callback(error);
- });
- });
+ this.makeRequest('PUT', command, data, callback);
};
Connection.prototype.post = function(command,data,callback){
- var self = this;
- var request = createRequest('POST', this.client.options, command);
- data = typeof data === 'string' ? data : JSON.stringify(data);
- request.end(data, 'utf8');
- request.on('response',function(response){
- var body = '';
- response.on('data', function(chunk) {
- body += chunk;
- });
- response.on('end', function() {
- var error = null;
- try{
- body = self.handleJson(response, body);
- }catch(e){
- body = null;
- error = e;
- }
- callback(error, body, response);
- });
- response.on('error', function(error) {
- callback(error,null);
- });
- });
+ this.makeRequest('POST', command, data, callback);
};
Connection.prototype.delete = function(command,callback){
- var self = this;
- var request = createRequest('DELETE', this.client.options, command);
- request.end();
- request.on('response',function(response){
- var body = '';
- response.on('data', function(chunk) {
- body += chunk;
- });
- response.on('end', function() {
- var error = null;
- try{
- body = self.handleJson(response, body);
- }catch(e){
- body = null;
- error = e;
- }
- callback(error, body, response);
- });
- response.on('error', function(error) {
- callback(error,null);
- });
- });
+ this.makeRequest('DELETE', command, '', callback);
};
Connection.prototype.handleJson = function(response,body){

0 comments on commit b668981

Please sign in to comment.