From e8b5c5fc343c977eeac9311c2842bc9ec439b157 Mon Sep 17 00:00:00 2001 From: Dave Gramlich Date: Mon, 17 Oct 2016 19:50:17 -0400 Subject: [PATCH] vision: add promise support --- packages/vision/README.md | 10 ++++ packages/vision/package.json | 2 +- packages/vision/src/index.js | 79 +++++++++++++++++++++++++++ packages/vision/system-test/vision.js | 12 ++-- packages/vision/test/index.js | 13 ++++- 5 files changed, 108 insertions(+), 8 deletions(-) diff --git a/packages/vision/README.md b/packages/vision/README.md index 181871ba81b..7e70bb2576e 100644 --- a/packages/vision/README.md +++ b/packages/vision/README.md @@ -107,6 +107,16 @@ vision.detectFaces('./image.jpg', function(err, faces) { // } // ] }); + +// Promises are also supported by omitting callbacks. +vision.detectFaces('./image.jpg').then(function(data) { + var faces = data[0]; +}); + +// It's also possible to integrate with third-party Promise libraries. +var vision = require('@google-cloud/vision')({ + promise: require('bluebird') +}); ``` diff --git a/packages/vision/package.json b/packages/vision/package.json index f7cdf09f63d..c87af6571c3 100644 --- a/packages/vision/package.json +++ b/packages/vision/package.json @@ -50,7 +50,7 @@ "vision" ], "dependencies": { - "@google-cloud/common": "^0.6.0", + "@google-cloud/common": "^0.7.0", "arrify": "^1.0.0", "async": "^2.0.1", "extend": "^3.0.0", diff --git a/packages/vision/src/index.js b/packages/vision/src/index.js index 662c9e5151c..46e8224c6e2 100644 --- a/packages/vision/src/index.js +++ b/packages/vision/src/index.js @@ -113,6 +113,14 @@ Vision.likelihood = { * vision.annotate(annotateImageReq, function(err, annotations, apiResponse) { * // annotations = apiResponse.responses * }); + * + * //- + * // If the callback is omitted, we'll return a Promise. + * //- + * vision.annotate(annotateImageReq).then(function(data) { + * var annotations = data[0]; + * var apiResponse = data[1]; + * }); */ Vision.prototype.annotate = function(requests, callback) { this.request({ @@ -263,6 +271,14 @@ Vision.prototype.annotate = function(requests, callback) { * // Errors occurred while trying to use this image for a face annotation. * } * }); + * + * //- + * // If the callback is omitted, we'll return a Promise. + * //- + * vision.detect('image.jpg', types).then(function(data) { + * var detections = data[0]; + * var apiResponse = data[1]; + * }); */ Vision.prototype.detect = function(images, options, callback) { var self = this; @@ -869,6 +885,14 @@ Vision.prototype.detect = function(images, options, callback) { * top: 'MIDPOINT_BETWEEN_EYES' * } * }; + * + * //- + * // If the callback is omitted, we'll return a Promise. + * //- + * vision.detectFaces('image.jpg').then(function(data) { + * var faces = data[0]; + * var apiResponse = data[1]; + * }); */ Vision.prototype.detectFaces = function(images, options, callback) { if (is.fn(options)) { @@ -925,6 +949,14 @@ Vision.prototype.detectFaces = function(images, options, callback) { * // // ... * // ] * }); + * + * //- + * // If the callback is omitted, we'll return a Promise. + * //- + * vision.detectLabels('image.jpg').then(function(data) { + * var labels = data[0]; + * var apiResponse = data[1]; + * }); */ Vision.prototype.detectLabels = function(images, options, callback) { if (is.fn(options)) { @@ -997,6 +1029,14 @@ Vision.prototype.detectLabels = function(images, options, callback) { * // } * // ] * }); + * + * //- + * // If the callback is omitted, we'll return a Promise. + * //- + * vision.detectLandmarks('image.jpg').then(function(data) { + * var landmarks = data[0]; + * var apiResponse = data[1]; + * }); */ Vision.prototype.detectLandmarks = function(images, options, callback) { if (is.fn(options)) { @@ -1061,6 +1101,14 @@ Vision.prototype.detectLandmarks = function(images, options, callback) { * // } * // ] * }); + * + * //- + * // If the callback is omitted, we'll return a Promise. + * //- + * vision.detectLogos('image.jpg').then(function(data) { + * var logos = data[0]; + * var apiResponse = data[1]; + * }); */ Vision.prototype.detectLogos = function(images, options, callback) { if (is.fn(options)) { @@ -1134,6 +1182,14 @@ Vision.prototype.detectLogos = function(images, options, callback) { * // ] * // } * }); + * + * //- + * // If the callback is omitted, we'll return a Promise. + * //- + * vision.detectProperties('image.jpg').then(function(data) { + * var props = data[0]; + * var apiResponse = data[1]; + * }); */ Vision.prototype.detectProperties = function(images, options, callback) { if (is.fn(options)) { @@ -1166,6 +1222,14 @@ Vision.prototype.detectProperties = function(images, options, callback) { * // violence: true * // } * }); + * + * //- + * // If the callback is omitted, we'll return a Promise. + * //- + * vision.detectSafeSearch('image.jpg').then(function(data) { + * var safeSearch = data[0]; + * var apiResponse = data[1]; + * }); */ Vision.prototype.detectSafeSearch = function(images, options, callback) { if (is.fn(options)) { @@ -1226,6 +1290,14 @@ Vision.prototype.detectSafeSearch = function(images, options, callback) { * // } * // ] * }); + * + * //- + * // If the callback is omitted, we'll return a Promise. + * //- + * vision.detectText('image.jpg').then(function(data) { + * var text = data[0]; + * var apiResponse = data[1]; + * }); */ Vision.prototype.detectText = function(images, options, callback) { if (is.fn(options)) { @@ -1547,5 +1619,12 @@ Vision.formatSafeSearchAnnotation_ = function(ssAnnotation, options) { return ssAnnotation; }; +/*! Developer Documentation + * + * All async methods (except for streams) will return a Promise in the event + * that a callback is omitted. + */ +common.util.promisifyAll(Vision); + module.exports = Vision; module.exports.v1 = require('./v1'); diff --git a/packages/vision/system-test/vision.js b/packages/vision/system-test/vision.js index 963c894d20d..1ca750f9e56 100644 --- a/packages/vision/system-test/vision.js +++ b/packages/vision/system-test/vision.js @@ -416,13 +416,13 @@ describe('Vision', function() { assert.deepEqual(properties.colors, [ '3b3027', '727d81', - '3f2f22', + '3f3022', '838e92', - '482a16', - '5f4f3c', - '261b14', - 'b39b7f', - '51473f', + '482b17', + '5f4e3d', + '261c14', + 'b29a7f', + '51473e', '2c1e12' ]); diff --git a/packages/vision/test/index.js b/packages/vision/test/index.js index 7790d726ac2..04de4e373ea 100644 --- a/packages/vision/test/index.js +++ b/packages/vision/test/index.js @@ -29,7 +29,14 @@ var Service = require('@google-cloud/common').Service; var tmp = require('tmp'); var util = require('@google-cloud/common').util; -var fakeUtil = extend({}, util); +var promisified = false; +var fakeUtil = extend({}, util, { + promisifyAll: function(Class) { + if (Class.name === 'Vision') { + promisified = true; + } + } +}); function FakeService() { this.calledWith_ = arguments; @@ -101,6 +108,10 @@ describe('Vision', function() { ]); assert.deepEqual(calledWith.packageJson, require('../package.json')); }); + + it('should promisify all the things', function() { + assert(promisified); + }); }); describe('constants', function() {