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

vision: add promise support #1712

Merged
merged 1 commit into from
Oct 18, 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/vision/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
});
```


Expand Down
2 changes: 1 addition & 1 deletion packages/vision/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
79 changes: 79 additions & 0 deletions packages/vision/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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');
12 changes: 6 additions & 6 deletions packages/vision/system-test/vision.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]);

Expand Down
13 changes: 12 additions & 1 deletion packages/vision/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down