Skip to content

Commit

Permalink
add get method
Browse files Browse the repository at this point in the history
  • Loading branch information
Junho Kyung committed Oct 19, 2011
1 parent fcf64d0 commit 1c3e307
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
41 changes: 26 additions & 15 deletions lib/swift.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ function Swift(options, callback) {
'X-Storage-User': this.options.user
, 'X-Storage-Pass': this.options.pass
}
}, function(res, result) {
if (res.statusCode == 200) {
this.account = res.headers['x-storage-url'].split('v1/')[1];
this.token = res.headers['x-auth-token'];
}, function(result, headers) {
if (headers['x-storage-url'] && headers['x-auth-token']) {
this.account = headers['x-storage-url'].split('v1/')[1];
this.token = headers['x-auth-token'];
}
callback && callback(res);
callback && callback(result, headers);
}.wrap(this));
}

Expand All @@ -48,7 +48,7 @@ function extend(destination, source) {
};


function request(options, callback) {
function request(options, callback, _res) {
options = extend({
host: this.options.host
, port: this.options.port
Expand All @@ -58,26 +58,37 @@ function request(options, callback) {
}, options);

var req = https.request(options, function(res) {
if (options.method == 'DELETE') return callback && callback(res);
res.on('data', function(buffer) {
clog.info(res.statusCode, res.headers, buffer.toString());
var buffers = [];
if (_res) {
_res.header('Content-Length', res.headers['content-length']);
_res.header('Content-Type', res.headers['content-type']);
}

var result = buffer.toString();
try {
result = JSON.parse(result);
} catch(e) {}
res.on('data', function(buffer) {
if (_res) _res.write(buffer);
else buffers.push(buffer);
});

callback && callback(res, result);
res.on('end', function(){
clog.info(res.statusCode, res.headers);
callback && callback(buffers.join(''), res.headers);
_res && _res.end();
});
});

req.end();

req.on('error', function(e) {
clog.error(e);
});
};

Swift.prototype.get = function(container, object, callback, strime) {
request.call(this, {
path: '/v1.0/' + this.account + '/' + container + '/' + object
}, callback, strime);
};


/*
* Storage Account Services
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "swift",
"author": "Firejune (http://firejune.com/)",
"version": "0.1.5",
"version": "0.1.6",
"description": "OpenStack Object Storage(Swift) REST client API for Node.JS",
"homepage": "http://github.com/firejune/swift",
"keywords": ["swift", "OpenStack", "Object Storage"],
"keywords": ["Swift", "OpenStack", "Object Storage"],
"main": "lib/swift",
"directories": {
"lib": "lib"
Expand Down

0 comments on commit 1c3e307

Please sign in to comment.