Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use request for more straightforward code and streaming api

  • Loading branch information...
commit cfedee8cec436adf20f3b6e73b83aff09f12135a 1 parent 4cb2c17
@fent fent authored
Showing with 110 additions and 170 deletions.
  1. +108 −168 lib/dropbox-node.js
  2. +2 −2 package.json
View
276 lib/dropbox-node.js
@@ -1,9 +1,10 @@
var pathLib = require('path')
, querystring = require('querystring')
+ , fs = require('fs')
, escapePath = require('./util/escape-path')
, stringifyParams = require('./util/stringify-params')
, EventEmitter = require('events').EventEmitter
- , OAuth = require('oauth').OAuth
+ , request = require('request')
, API_URI = 'https://api.dropbox.com/1'
, CONTENT_API_URI = 'https://api-content.dropbox.com/1';
@@ -15,48 +16,83 @@ var DropboxClient = exports.DropboxClient =
this.consumer_secret = consumer_secret;
this.access_token = access_token || undefined;
this.access_token_secret = access_token_secret || undefined;
- this.oauth = new OAuth(API_URI + '/oauth/request_token'
- , API_URI + '/oauth/access_token'
- , consumer_key, consumer_secret
- , '1.0', null, 'HMAC-SHA1');
}
+// Creates a request to the API with OAuth credentials
+DropboxClient.prototype.request =
+ function(method, uri, optargs, body, callback) {
+ if (typeof body === 'function') callback = body, body = undefined;
+ optargs = optargs || {};
+ var oauth = {
+ consumer_key: this.consumer_key
+ , consumer_secret: this.consumer_secret
+ , token: optargs.token || this.access_token
+ , token_secret: optargs.secret || this.access_token_secret
+ };
+
+ return request[method]({ uri: uri, oauth: oauth, body: body }, callback ?
+ function(err, res, body) {
+ if (err) return callback(err);
+ var contentType = res.headers['content-type'];
+ console.log(res.statusCode);
+ console.log(contentType);
+ if (contentType === 'application/json' ||
+ contentType === 'text/javascript') {
+ body = JSON.parse(body);
+ if (body.error) {
+ var err = new Error(body.error);
+ err.statusCode = res.statusCode;
+ return callback(err);
+ }
+
+ } else if (res.statusCode === 411) {
+ var err = new Error('Chunked encoding is not supported.');
+ err.statusCode = res.statusCode;
+ return callback(err);
+ }
+
+ callback(null, body);
+ } : undefined);
+};
+
+
+// Convenience methods
+['get', 'post', 'put'].forEach(function(method) {
+ DropboxClient.prototype[method] = function(uri, optargs, body, callback) {
+ return this.request(method, uri, optargs, body, callback);
+ };
+});
+
+
// Fetches an access token and access token secret pair based on the user's
// email and password. As well as being stored internally, the key pair is
// returned to the callback in case the application developer requires it.
DropboxClient.prototype.getAccessToken = function(email, pwd, cb) {
// Validate email and pwd.
- if (typeof email === 'function') cb = email, email = undefined;
- else if (typeof pwd === 'function') cb = pwd, pwd = undefined;
if (!email || !pwd) return cb(Error('Invalid arguments. Please provide ' +
'a valid email and password.'));
+ var uri = API_URI + '/token?' +
+ querystring.stringify({email: email, password: pwd});
var self = this;
- this.oauth.get(API_URI + '/token?' +
- querystring.stringify({email: email, password: pwd})
- , null, null, function(err, data, res) {
- if (err) return cb(err);
- // Store the key pair and fire callback.
- self.access_token = JSON.parse(data).token;
- self.access_token_secret = JSON.parse(data).secret;
- cb(null, self.access_token, self.access_token_secret);
- });
+ this.get(uri, {}, function(err, data) {
+ if (err) return cb(err);
+ // Store the key pair and fire callback.
+ self.access_token = data.token;
+ self.access_token_secret = data.secret;
+ cb(null, data.token, data.secret);
+ });
}
// Retrieves information about the user's account as a JSON response.
DropboxClient.prototype.getAccountInfo = function(optargs, cb) {
if (typeof optargs == 'function') cb = optargs, optargs = {};
- this.oauth.get(API_URI + '/account/info/' +
- (optargs.status_in_response ? '?status_in_response=' +
- optargs.status_in_response : '')
- , optargs.token || this.access_token
- , optargs.secret || this.access_token_secret
- , function(err, data, res) {
- if (err) return cb(err);
- cb(null, JSON.parse(data));
- });
+ var uri = API_URI + '/account/info/' +
+ (optargs.status_in_response ? '?status_in_response=' +
+ optargs.status_in_response : '');
+ this.get(uri, optargs, cb);
}
@@ -71,12 +107,9 @@ DropboxClient.prototype.createAccount = function(email, first_name, last_name
, password: password
, status_in_response: optargs.status_in_response
}
- this.oauth.get(API_URI + '/account?' +
- querystring.stringify(params)
- , null, null, function(err, data, res) {
- if (err) return cb(err);
- cb(null, data);
- });
+ var uri = API_URI + '/account?' +
+ querystring.stringify(params);
+ this.get(uri, {}, cb);
}
@@ -84,26 +117,8 @@ DropboxClient.prototype.createAccount = function(email, first_name, last_name
// user's Dropbox root.
DropboxClient.prototype.getFile = function(path, optargs, cb) {
if (typeof optargs == 'function') cb = optargs, optargs = {};
- path = escapePath(path);
- this.oauth.get(CONTENT_API_URI + '/files/dropbox/' + path
- , optargs.token || this.access_token
- , optargs.secret || this.access_token_secret
- , function(err, data, res) {
- cb(err, data);
- });
-}
-
-
-// Sets up a streaming connection to fetch a file specified by path argument.
-// Returns a request EventEmitter object.
-DropboxClient.prototype.getFileStream = function(path, optargs) {
- optargs = optargs || {};
- path = escapePath(path);
- var req = this.oauth.get(CONTENT_API_URI + '/files/dropbox/' + path
- , optargs.token || this.access_token
- , optargs.secret || this.access_token_secret);
- req.end();
- return req;
+ var uri = CONTENT_API_URI + '/files/dropbox/' + escapePath(path);
+ return this.get(uri, optargs, cb);
}
@@ -111,29 +126,12 @@ DropboxClient.prototype.getFileStream = function(path, optargs) {
// application's directory.
DropboxClient.prototype.putFile = function(file, path, optargs, cb) {
if (typeof optargs == 'function') cb = optargs, optargs = {};
- var boundary = 'sAxIqse3tPlHqUIUI9ofVlHvtdt3tpaG'
- , content_type = 'multipart/form-data; boundary=' + boundary
- , self = this;
-
- require('fs').readFile(file, function(err, data) {
+ var uri = CONTENT_API_URI + '/files_put/dropbox/' + escapePath(path) +
+ '?' + stringifyParams(optargs);
+ var self = this;
+ fs.readFile(file, function(err, data) {
if (err) return cb(err);
- // Build request body.
- path = escapePath(path);
- var body = ['--' + boundary
- , 'Content-Disposition: form-data; name=file; filename="' + file + '"'
- , 'Content-Type: application/octet-stream'
- , '', data.toString('binary'), '--' + boundary + '--', ''
- ].join('\r\n');
-
- self.oauth.post(CONTENT_API_URI + '/files/dropbox/' + path +
- '?file=' + encodeURIComponent(file)
- , optargs.token || self.access_token
- , optargs.secret || self.access_token_secret
- , body, content_type
- , function(err, data, res) {
- if (err) return cb(err);
- cb(null, JSON.parse(data));
- });
+ self.request('put', uri, optargs, data, cb);
});
}
@@ -141,28 +139,9 @@ DropboxClient.prototype.putFile = function(file, path, optargs, cb) {
// Uploads contents to the specified path
DropboxClient.prototype.put = function(content, path, optargs, cb) {
if (typeof optargs == 'function') cb = optargs, optargs = {};
- var boundary = 'sAxIqse3tPlHqUIUI9ofVlHvtdt3tpaG'
- , content_type = 'multipart/form-data; boundary=' + boundary
- , self = this;
-
- // Build request body.
- var file = pathLib.basename(path);
- path = escapePath(pathLib.dirname(path));
- var body = ['--' + boundary
- , 'Content-Disposition: form-data; name=file; filename="' + file + '"'
- , 'Content-Type: application/octet-stream'
- , '', content, '--' + boundary + '--', ''
- ].join('\r\n');
-
- self.oauth.post(CONTENT_API_URI + '/files/dropbox/' + path +
- '?file=' + encodeURIComponent(file)
- , optargs.token || self.access_token
- , optargs.secret || self.access_token_secret
- , body, content_type
- , function(err, data, res) {
- if (err) return cb(err);
- cb(null, JSON.parse(data));
- });
+ var uri = CONTENT_API_URI + '/files_put/dropbox/' + escapePath(path) +
+ '?' + stringifyParams(optargs);
+ this.put(uri, optargs, content, cb);
}
@@ -170,15 +149,9 @@ DropboxClient.prototype.put = function(content, path, optargs, cb) {
// Dropbox root.
DropboxClient.prototype.getMetadata = function(path, optargs, cb) {
if (typeof optargs == 'function') cb = optargs, optargs = {};
- path = escapePath(path);
- this.oauth.get(API_URI + '/metadata/dropbox/' + path + '?' +
- stringifyParams(optargs)
- , optargs.token || this.access_token
- , optargs.secret || this.access_token_secret
- , function(err, data, res) {
- if (err) return cb(err);
- cb(null, JSON.parse(data));
- });
+ var uri = API_URI + '/metadata/dropbox/' + escapePath(path) + '?' +
+ stringifyParams(optargs);
+ this.get(uri, optargs, cb);
}
@@ -187,14 +160,10 @@ DropboxClient.prototype.getMetadata = function(path, optargs, cb) {
// valid size specifiers.
DropboxClient.prototype.getThumbnail = function(path, optargs, cb) {
if (typeof optargs == 'function') cb = optargs, optargs = {};
- path = escapePath(path);
- this.oauth.get(CONTENT_API_URI + '/thumbnails/dropbox/' + path + '?' +
- stringifyParams(optargs)
- , optargs.token || this.access_token
- , optargs.secret || this.access_token_secret
- , function(err, data, res) {
- cb(err, data);
- });
+ optargs = optargs || {};
+ var uri = CONTENT_API_URI + '/thumbnails/dropbox/' + escapePath(path) + '?' +
+ stringifyParams(optargs);
+ return this.get(uri, optargs, cb);
}
@@ -203,16 +172,11 @@ DropboxClient.prototype.getThumbnail = function(path, optargs, cb) {
// of arguments.
DropboxClient.prototype.copy = function(from_path, to_path, optargs, cb) {
if (typeof optargs == 'function') cb = optargs, optargs = {};
- this.oauth.get(API_URI + '/fileops/copy?' +
- querystring.stringify({root: 'dropbox'
- , from_path: from_path
- , to_path: to_path})
- , optargs.token || this.access_token
- , optargs.secret || this.access_token_secret
- , function(err, data, res) {
- if (err) return cb(err);
- cb(null, JSON.parse(data));
- });
+ var uri = API_URI + '/fileops/copy?' +
+ querystring.stringify({root: 'dropbox'
+ , from_path: from_path
+ , to_path: to_path});
+ this.get(uri, optargs, cb);
}
@@ -221,15 +185,10 @@ DropboxClient.prototype.copy = function(from_path, to_path, optargs, cb) {
// for explanation of arguments.
DropboxClient.prototype.createFolder = function(path, optargs, cb) {
if (typeof optargs == 'function') cb = optargs, optargs = {};
- this.oauth.get(API_URI + '/fileops/create_folder?' +
- querystring.stringify({root: 'dropbox'
- , path: path})
- , optargs.token || this.access_token
- , optargs.secret || this.access_token_secret
- , function(err, data, res) {
- if (err) return cb(err);
- cb(null, JSON.parse(data));
- });
+ var uri = API_URI + '/fileops/create_folder?' +
+ querystring.stringify({root: 'dropbox'
+ , path: path});
+ this.get(uri, optargs, cb);
}
@@ -238,14 +197,9 @@ DropboxClient.prototype.createFolder = function(path, optargs, cb) {
// explanation of arguments.
DropboxClient.prototype.deleteItem = function(path, optargs, cb) {
if (typeof optargs == 'function') cb = optargs, optargs = {};
- this.oauth.get(API_URI + '/fileops/delete?' +
- querystring.stringify({root: 'dropbox', path: path})
- , optargs.token || this.access_token
- , optargs.secret || this.access_token_secret
- , function(err, data, res) {
- if (err) return cb(err);
- cb(null, JSON.parse(data));
- });
+ var uri = API_URI + '/fileops/delete?' +
+ querystring.stringify({root: 'dropbox', path: path});
+ this.get(uri, optargs, cb);
}
@@ -254,16 +208,11 @@ DropboxClient.prototype.deleteItem = function(path, optargs, cb) {
// explanation of arguments.
DropboxClient.prototype.move = function(from_path, to_path, optargs, cb) {
if (typeof optargs == 'function') cb = optargs, optargs = {};
- this.oauth.get(API_URI + '/fileops/move?' +
- querystring.stringify({root: 'dropbox'
- , from_path: from_path
- , to_path: to_path})
- , optargs.token || this.access_token
- , optargs.secret || this.access_token_secret
- , function(err, data, res) {
- if (err) return cb(err);
- cb(null, JSON.parse(data));
- });
+ var uri = API_URI + '/fileops/move?' +
+ querystring.stringify({root: 'dropbox'
+ , from_path: from_path
+ , to_path: to_path});
+ this.get(uri, optargs, cb);
}
@@ -271,14 +220,9 @@ DropboxClient.prototype.move = function(from_path, to_path, optargs, cb) {
// See https://www.dropbox.com/developers/reference/api#search
DropboxClient.prototype.search = function(path, query, optargs, cb) {
if (typeof optargs === 'function') cb = optargs, optargs = {};
- this.oauth.get(API_URI + '/search/dropbox/' + path + '?' +
- querystring.stringify({query: query})
- , optargs.token || this.access_token
- , optargs.secret || this.access_token_secret
- , function(err, data, res) {
- if (err) return cb(err);
- cb(null, JSON.parse(data));
- });
+ var uri = API_URI + '/search/dropbox/' + path + '?' +
+ querystring.stringify({query: query});
+ this.get(uri, optargs, cb);
}
@@ -297,20 +241,16 @@ DropboxClient.prototype.delta = function(cursor, optargs, cb) {
}
}
- this.oauth.post(API_URI + '/delta' +
- (cursor ?
- '?' + querystring.stringify({cursor: cursor}) : '')
- , optargs.token || this.access_token
- , optargs.secret || this.access_token_secret
- , ''
- , function(err, data, res) {
- if (err) return cb(err);
- cb(null, JSON.parse(data));
- });
+ var uri = API_URI + '/delta' +
+ (cursor ? '?' + querystring.stringify({cursor: cursor}) : '')
+ this.post(uri, optargs, cb);
}
// Continously stream changes through DropboxClient#delta
+// Returns an event emitter that has `pause()` and `resume()` methods.
+// It emits `reset` events on detta resets, and `data` events with
+// parameters `path` and `metadata` on new delta changes.
DropboxClient.prototype.stream = function(cursor, optargs) {
optargs = optargs || {};
var ee = new EventEmitter();
@@ -329,7 +269,7 @@ DropboxClient.prototype.stream = function(cursor, optargs) {
for (var i = 0, len = data.entries.length; i < len; i++) {
var e = data.entries[i];
- ee.emit('data', { path: e[0], metadata: e[1] });
+ ee.emit('data', e[0], e[1]);
}
}
View
4 package.json
@@ -19,7 +19,7 @@
"test": "node specs.js"
},
"dependencies": {
- "oauth": ">=0.8.2 <2.0.0"
+ "request": "2.9.x"
},
"engines": {
"node": "*"
@@ -28,4 +28,4 @@
"lib": "./lib"
},
"devDependencies": {}
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.