Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage: add promise support #1697

Merged
merged 2 commits into from
Oct 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ remoteReadStream.pipe(localWriteStream);
var localReadStream = fs.createReadStream('/photos/zoo/zebra.jpg');
var remoteWriteStream = bucket.file('zebra.jpg').createWriteStream();
localReadStream.pipe(remoteWriteStream);

// Promises are also supported by omitting callbacks.
bucket.upload('/photos/zoo/zebra.jpg').then(function(data) {
var file = data[0];
});

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


// It's also possible to integrate with third-party Promise libraries.

This comment was marked as spam.

This comment was marked as spam.

var gcs = require('@google-cloud/storage')({
promise: require('bluebird')
});
```


Expand Down
2 changes: 1 addition & 1 deletion packages/storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"storage"
],
"dependencies": {
"@google-cloud/common": "^0.6.0",
"@google-cloud/common": "^0.7.0",
"arrify": "^1.0.0",
"async": "^2.0.1",
"concat-stream": "^1.5.0",
Expand Down
77 changes: 72 additions & 5 deletions packages/storage/src/acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'use strict';

var arrify = require('arrify');
var common = require('@google-cloud/common');
var is = require('is');
var util = require('util');

Expand Down Expand Up @@ -107,6 +108,14 @@ function Acl(options) {
* entity: 'user-email@example.com',
* role: gcs.acl.OWNER_ROLE
* }, function(err, aclObject) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* myFile.acl.owners.addUser('email@example.com').then(function(data) {
* var aclObject = data[0];
* var apiResponse = data[1];
* });
*/
Acl.prototype.owners = {};

Expand Down Expand Up @@ -144,6 +153,14 @@ Acl.prototype.owners = {};
* entity: 'user-email@example.com',
* role: gcs.acl.READER_ROLE
* }, function(err, aclObject) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* myFile.acl.readers.addUser('email@example.com').then(function(data) {
* var aclObject = data[0];
* var apiResponse = data[1];
* });
*/
Acl.prototype.readers = {};

Expand Down Expand Up @@ -181,6 +198,14 @@ Acl.prototype.readers = {};
* entity: 'user-email@example.com',
* role: gcs.acl.WRITER_ROLE
* }, function(err, aclObject) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* myFile.acl.writers.addUser('email@example.com').then(function(data) {
* var aclObject = data[0];
* var apiResponse = data[1];
* });
*/
Acl.prototype.writers = {};

Expand All @@ -204,10 +229,12 @@ util.inherits(Acl, AclRoleAccessorMethods);
* @param {object} callback.apiResponse - The full API response.
*
* @example
* myBucket.acl.add({
* var options = {
* entity: 'user-useremail@example.com',
* role: gcs.acl.OWNER_ROLE
* }, function(err, aclObject, apiResponse) {});
* };
*
* myBucket.acl.add(options, function(err, aclObject, apiResponse) {});
*
* //-
* // For file ACL operations, you can also specify a `generation` property.
Expand All @@ -219,6 +246,14 @@ util.inherits(Acl, AclRoleAccessorMethods);
* role: gcs.acl.OWNER_ROLE,
* generation: 1
* }, function(err, aclObject, apiResponse) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* myBucket.acl.add(options).then(function(data) {
* var aclObject = data[0];
* var apiResponse = data[1];
* });
*/
Acl.prototype.add = function(options, callback) {
var self = this;
Expand Down Expand Up @@ -273,6 +308,13 @@ Acl.prototype.add = function(options, callback) {
* entity: 'user-useremail@example.com',
* generation: 1
* }, function(err, apiResponse) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* myFile.acl.delete().then(function(data) {
* var apiResponse = data[0];
* });
*/
Acl.prototype.delete = function(options, callback) {
var query = {};
Expand Down Expand Up @@ -333,6 +375,14 @@ Acl.prototype.delete = function(options, callback) {
* entity: 'user-useremail@example.com',
* generation: 1
* }, function(err, aclObject, apiResponse) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* myBucket.acl.get().then(function(data) {
* var aclObject = data[0];
* var apiResponse = data[1];
* });
*/
Acl.prototype.get = function(options, callback) {
var self = this;
Expand Down Expand Up @@ -389,10 +439,12 @@ Acl.prototype.get = function(options, callback) {
* @param {object} callback.apiResponse - The full API response.
*
* @example
* myBucket.acl.update({
* var options = {
* entity: 'user-useremail@example.com',
* role: gcs.acl.WRITER_ROLE
* }, function(err, aclObject, apiResponse) {});
* };
*
* myBucket.acl.update(options, function(err, aclObject, apiResponse) {});
*
* //-
* // For file ACL operations, you can also specify a `generation` property.
Expand All @@ -402,6 +454,14 @@ Acl.prototype.get = function(options, callback) {
* role: gcs.acl.WRITER_ROLE,
* generation: 1
* }, function(err, aclObject, apiResponse) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* myFile.acl.update(options).then(function(data) {
* var aclObject = data[0];
* var apiResponse = data[1];
* });
*/
Acl.prototype.update = function(options, callback) {
var self = this;
Expand Down Expand Up @@ -463,6 +523,13 @@ Acl.prototype.request = function(reqOpts, callback) {
this.request_(reqOpts, callback);
};

/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
common.util.promisifyAll(Acl);

module.exports = Acl;

/**
Expand Down Expand Up @@ -539,7 +606,7 @@ AclRoleAccessorMethods.prototype._assignAccessMethods = function(role) {
callback = entityId;
}

self[accessMethod]({
return self[accessMethod]({
entity: apiEntity,
role: role
}, callback);
Expand Down
Loading