Skip to content

Commit

Permalink
Merge pull request #22 from IvanKalinin/feature--axios
Browse files Browse the repository at this point in the history
NEW MAJOR VERSION - added large file support and upload progress.
  • Loading branch information
yakovkhalinsky authored Mar 15, 2017
2 parents 25a6336 + df9c621 commit 885d9c2
Show file tree
Hide file tree
Showing 14 changed files with 1,198 additions and 242 deletions.
58 changes: 54 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ is located in a similar folder to the node module that is being tested.

Always run `npm test` before you commit.

### Upgrading from 0.9.x to 1.0.x

For this update, we've switched the back end HTTP request library from `request` to `axios` as it has better Promise and progress support built in. However, there are a couple changes that will break your code and ruin your day. Here are the changes:
* The Promise resolution has a different data structure. Where previously, the request response data was the root object in the promise resolution (`res`), this data now resides in `res.data`.
* In v0.9.12, we added request progress reporting via the third parameter to `then()`. Because we are no longer using the same promise library, this functionality has been removed. However, progress reporting is still available by passing a callback function into the `b2.method()` that you're calling. See the documentation below for details.
* In v0.9.x, `b2.downloadFileById()` accepted a `fileId` parameter as a String or Number. As of 1.0.0, the first parameter is now expected to be a plain Object of arguments.

### Usage

Expand All @@ -51,7 +57,10 @@ Always run `npm test` before you commit.
b2.authorize(); // returns promise

// create bucket
b2.createBucket(bucketName, bucketType); // returns promise
b2.createBucket(
bucketName,
bucketType // one of `allPublic`, `allPrivate`
); // returns promise

// delete bucket
b2.deleteBucket(bucketId); // returns promise
Expand All @@ -77,7 +86,8 @@ Always run `npm test` before you commit.
// valid characters should be a-z, A-Z and '-', all other characters will cause an error to be thrown
key1: value
key2: value
}
},
onUploadProgress: function(event) || null // progress monitoring
}); // returns promise
// list file names
Expand Down Expand Up @@ -108,15 +118,55 @@ Always run `npm test` before you commit.
// download file by name
b2.downloadFileByName({
bucketName: 'bucketName',
fileName: 'fileName'
fileName: 'fileName',
onDownloadProgress: function(event) || null // progress monitoring
}); // returns promise
// download file by fileId
b2.downloadFileById(fileId); // returns promise
b2.downloadFileById({
fileId: 'fileId',
onDownloadProgress: function(event) || null // progress monitoring
}); // returns promise

// delete file version
b2.deleteFileVersion({
fileId: 'fileId',
fileName: 'fileName'
}); // returns promise

// start large file
b2.startLargeFile({
bucketId: 'bucketId',
fileName: 'fileName'
}) // returns promise

// get upload part url
b2.getUploadPartUrl({
fileId: 'fileId'
}) // returns promise

// get upload part
b2.uploadPart({
partNumber: 'partNumber', // A number from 1 to 10000
uploadUrl: 'uploadUrl',
uploadAuthToken: 'uploadAuthToken', // comes from getUploadPartUrl();
data: Buffer // this is expecting a Buffer not an encoded string,
onUploadProgress: function(event) || null // progress monitoring
}) // returns promise

// finish large file
b2.finishLargeFile({
fileId: 'fileId',
partSha1Array: [partSha1Array] // array of sha1 for each part
}) // returns promise

// cancel large file
b2.cancelLargeFile({
fileId: 'fileId'
}) // returns promise


### Authors
* Yakov Khalinsky (@yakovkhalinsky)
* Ivan Kalinin (@IvanKalinin) at Isolary
* Brandon Patton (@crazyscience) at Isolary
12 changes: 5 additions & 7 deletions lib/actions/auth.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
var q = require('q');

var conf = require('../../conf');
var request = require('./../request');
var utils = require('./../utils');

exports.authorize = function(b2) {
var deferred = q.defer();
var options = getRequestOptions(b2.accountId, b2.applicationKey);

var requestInstance = request.getInstance();
requestInstance(options, utils.getProcessAuthSuccess(b2, deferred));

return deferred.promise;
var axiosInstance = request.getInstance();
return axiosInstance(options).then(function(res) {
utils.saveAuthContext(b2, res.data);
return res; // For testing and/or Promise chaining
});
};

function getRequestOptions(accountId, applicationKey) {
Expand Down
23 changes: 12 additions & 11 deletions lib/actions/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ exports.TYPES = {
exports.create = function(b2, bucketName, bucketType) {
var options = {
url: getCreateUrl(b2, bucketName, bucketType),
qs: {
method: 'POST',
headers: utils.getAuthHeaderObjectWithToken(b2),
data: {
accountId: b2.accountId,
bucketName: bucketName,
bucketType: bucketType
},
headers: utils.getAuthHeaderObjectWithToken(b2)
}
};
return request.sendRequest(options);
};
Expand All @@ -24,10 +25,10 @@ exports.delete = function(b2, bucketId) {
var options = {
url: getDeleteUrl(b2),
method: 'POST',
body: JSON.stringify({
data: {
accountId: b2.accountId,
bucketId: bucketId
}),
},
headers: utils.getAuthHeaderObjectWithToken(b2)
};
return request.sendRequest(options);
Expand All @@ -37,9 +38,9 @@ exports.list = function(b2) {
var options = {
url: getListUrl(b2),
method: 'POST',
body: JSON.stringify({
data: {
accountId: b2.accountId
}),
},
headers: utils.getAuthHeaderObjectWithToken(b2)

};
Expand All @@ -50,11 +51,11 @@ exports.update = function(b2, bucketId, bucketType) {
var options = {
url: getUpdateUrl(b2),
method: 'POST',
body: JSON.stringify({
data: {
accountId: b2.accountId,
bucketId: bucketId,
bucketType: bucketType
}),
},
headers: utils.getAuthHeaderObjectWithToken(b2)
};
return request.sendRequest(options);
Expand All @@ -64,9 +65,9 @@ exports.getUploadUrl = function(b2, bucketId) {
var options = {
url: getGetUploadUrl(b2),
method: 'POST',
body: JSON.stringify({
data: {
bucketId: bucketId
}),
},
headers: utils.getAuthHeaderObjectWithToken(b2)
};
return request.sendRequest(options);
Expand Down
Loading

0 comments on commit 885d9c2

Please sign in to comment.