Skip to content

Commit 5244ed0

Browse files
lukesneeringerstephenplusplus
authored andcommitted
Add arbitrary request options to single-feature helpers. (#2555)
1 parent 1b5a6da commit 5244ed0

File tree

2 files changed

+96
-37
lines changed

2 files changed

+96
-37
lines changed

packages/google-cloud-vision/src/helpers.js

Lines changed: 78 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,22 @@ var coerceImage = (image, callback) => {
7474
* asking for the single feature annotation.
7575
*/
7676
var _createSingleFeatureMethod = featureValue => {
77-
return function(image, options) {
78-
return this.annotateImage({
79-
image: image,
80-
features: [{type: featureValue}],
81-
}, options);
77+
return function(annotateImageRequest, callOptions) {
78+
annotateImageRequest.features = annotateImageRequest.features || [{
79+
type: featureValue,
80+
}];
81+
// If the user submitted explicit features that do not line up with
82+
// the precise method called, throw an exception.
83+
for (let feature of annotateImageRequest.features) {
84+
if (feature.type !== featureValue) {
85+
throw new Error(
86+
'Setting explicit features is not supported on this method. ' +
87+
'Use the #annotateImage method instead.'
88+
);
89+
}
90+
}
91+
// Call the underlying #annotateImage method.
92+
return this.annotateImage(annotateImageRequest, callOptions);
8293
};
8394
};
8495

@@ -98,9 +109,10 @@ module.exports = apiVersion => {
98109
/**
99110
* Annotate a single image with the requested features.
100111
*
101-
* @param {Object=} request
112+
* @param {Object} request
102113
* A representation of the request being sent to the Vision API.
103-
* @param {Object=} request.image
114+
* This is an [AnnotateImageRequest]{@link AnnotateImageRequest}.
115+
* @param {Object} request.image
104116
* A dictionary-like object representing the image. This should have a
105117
* single key (`source`, `content`).
106118
*
@@ -110,7 +122,10 @@ module.exports = apiVersion => {
110122
* If the key is `content`, the value should be a Buffer.
111123
* @param {Array} request.features
112124
* An array of the specific annotation features being requested.
113-
* @param {Object=} options
125+
* This should take a form such as:
126+
* [{type: vision.types.Feature.Type.FACE_DETECTION},
127+
* {type: vision.types.Feature.Type.WEB_DETECTION}]
128+
* @param {Object=} callOptions
114129
* Optional parameters. You can override the default settings for this
115130
* call, e.g, timeout, retries, paginations, etc. See
116131
* [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
@@ -137,12 +152,12 @@ module.exports = apiVersion => {
137152
* console.error(err);
138153
* });
139154
*/
140-
methods.annotateImage = promisify(function(request, options, callback) {
155+
methods.annotateImage = promisify(function(request, callOptions, callback) {
141156
// If a callback was provided and options were skipped, normalize
142157
// the argument names.
143-
if (is.undefined(callback) && is.function(options)) {
144-
callback = options;
145-
options = undefined;
158+
if (is.undefined(callback) && is.function(callOptions)) {
159+
callback = callOptions;
160+
callOptions = undefined;
146161
}
147162

148163
// If there is no image, throw an exception.
@@ -159,7 +174,7 @@ module.exports = apiVersion => {
159174
request.image = image;
160175

161176
// Call the GAPIC batch annotation function.
162-
return this.batchAnnotateImages([request], options, (err, r) => {
177+
return this.batchAnnotateImages([request], callOptions, (err, r) => {
163178
// If there is an error, handle it.
164179
if (err) {
165180
return callback(err);
@@ -187,15 +202,18 @@ module.exports = apiVersion => {
187202
/**
188203
* Annotate a single image with face detection.
189204
*
190-
* @param {Object=} image
205+
* @param {Object} request
206+
* A representation of the request being sent to the Vision API.
207+
* This is an [AnnotateImageRequest]{@link AnnotateImageRequest}.
208+
* @param {Object} request.image
191209
* A dictionary-like object representing the image. This should have a
192210
* single key (`source`, `content`).
193211
*
194212
* If the key is `source`, the value should be another object containing
195213
* `imageUri` or `filename` as a key and a string as a value.
196214
*
197215
* If the key is `content`, the value should be a Buffer.
198-
* @param {Object=} options
216+
* @param {Object=} callOptions
199217
* Optional parameters. You can override the default settings for this
200218
* call, e.g, timeout, retries, paginations, etc. See
201219
* [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
@@ -227,15 +245,18 @@ module.exports = apiVersion => {
227245
/**
228246
* Annotate a single image with landmark detection.
229247
*
230-
* @param {Object=} image
248+
* @param {Object} request
249+
* A representation of the request being sent to the Vision API.
250+
* This is an [AnnotateImageRequest]{@link AnnotateImageRequest}.
251+
* @param {Object} request.image
231252
* A dictionary-like object representing the image. This should have a
232253
* single key (`source`, `content`).
233254
*
234255
* If the key is `source`, the value should be another object containing
235256
* `imageUri` or `filename` as a key and a string as a value.
236257
*
237258
* If the key is `content`, the value should be a Buffer.
238-
* @param {Object=} options
259+
* @param {Object=} callOptions
239260
* Optional parameters. You can override the default settings for this
240261
* call, e.g, timeout, retries, paginations, etc. See
241262
* [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
@@ -267,15 +288,18 @@ module.exports = apiVersion => {
267288
/**
268289
* Annotate a single image with logo detection.
269290
*
270-
* @param {Object=} image
291+
* @param {Object} request
292+
* A representation of the request being sent to the Vision API.
293+
* This is an [AnnotateImageRequest]{@link AnnotateImageRequest}.
294+
* @param {Object} request.image
271295
* A dictionary-like object representing the image. This should have a
272296
* single key (`source`, `content`).
273297
*
274298
* If the key is `source`, the value should be another object containing
275299
* `imageUri` or `filename` as a key and a string as a value.
276300
*
277301
* If the key is `content`, the value should be a Buffer.
278-
* @param {Object=} options
302+
* @param {Object=} callOptions
279303
* Optional parameters. You can override the default settings for this
280304
* call, e.g, timeout, retries, paginations, etc. See
281305
* [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
@@ -307,15 +331,18 @@ module.exports = apiVersion => {
307331
/**
308332
* Annotate a single image with label detection.
309333
*
310-
* @param {Object=} image
334+
* @param {Object} request
335+
* A representation of the request being sent to the Vision API.
336+
* This is an [AnnotateImageRequest]{@link AnnotateImageRequest}.
337+
* @param {Object} request.image
311338
* A dictionary-like object representing the image. This should have a
312339
* single key (`source`, `content`).
313340
*
314341
* If the key is `source`, the value should be another object containing
315342
* `imageUri` or `filename` as a key and a string as a value.
316343
*
317344
* If the key is `content`, the value should be a Buffer.
318-
* @param {Object=} options
345+
* @param {Object=} callOptions
319346
* Optional parameters. You can override the default settings for this
320347
* call, e.g, timeout, retries, paginations, etc. See
321348
* [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
@@ -347,15 +374,18 @@ module.exports = apiVersion => {
347374
/**
348375
* Annotate a single image with text detection.
349376
*
350-
* @param {Object=} image
377+
* @param {Object} request
378+
* A representation of the request being sent to the Vision API.
379+
* This is an [AnnotateImageRequest]{@link AnnotateImageRequest}.
380+
* @param {Object} request.image
351381
* A dictionary-like object representing the image. This should have a
352382
* single key (`source`, `content`).
353383
*
354384
* If the key is `source`, the value should be another object containing
355385
* `imageUri` or `filename` as a key and a string as a value.
356386
*
357387
* If the key is `content`, the value should be a Buffer.
358-
* @param {Object=} options
388+
* @param {Object=} callOptions
359389
* Optional parameters. You can override the default settings for this
360390
* call, e.g, timeout, retries, paginations, etc. See
361391
* [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
@@ -387,15 +417,18 @@ module.exports = apiVersion => {
387417
/**
388418
* Annotate a single image with document text detection.
389419
*
390-
* @param {Object=} image
420+
* @param {Object} request
421+
* A representation of the request being sent to the Vision API.
422+
* This is an [AnnotateImageRequest]{@link AnnotateImageRequest}.
423+
* @param {Object} request.image
391424
* A dictionary-like object representing the image. This should have a
392425
* single key (`source`, `content`).
393426
*
394427
* If the key is `source`, the value should be another object containing
395428
* `imageUri` or `filename` as a key and a string as a value.
396429
*
397430
* If the key is `content`, the value should be a Buffer.
398-
* @param {Object=} options
431+
* @param {Object=} callOptions
399432
* Optional parameters. You can override the default settings for this
400433
* call, e.g, timeout, retries, paginations, etc. See
401434
* [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
@@ -427,15 +460,18 @@ module.exports = apiVersion => {
427460
/**
428461
* Annotate a single image with safe search detection.
429462
*
430-
* @param {Object=} image
463+
* @param {Object} request
464+
* A representation of the request being sent to the Vision API.
465+
* This is an [AnnotateImageRequest]{@link AnnotateImageRequest}.
466+
* @param {Object} request.image
431467
* A dictionary-like object representing the image. This should have a
432468
* single key (`source`, `content`).
433469
*
434470
* If the key is `source`, the value should be another object containing
435471
* `imageUri` or `filename` as a key and a string as a value.
436472
*
437473
* If the key is `content`, the value should be a Buffer.
438-
* @param {Object=} options
474+
* @param {Object=} callOptions
439475
* Optional parameters. You can override the default settings for this
440476
* call, e.g, timeout, retries, paginations, etc. See
441477
* [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
@@ -467,15 +503,18 @@ module.exports = apiVersion => {
467503
/**
468504
* Annotate a single image with image properties.
469505
*
470-
* @param {Object=} image
506+
* @param {Object} request
507+
* A representation of the request being sent to the Vision API.
508+
* This is an [AnnotateImageRequest]{@link AnnotateImageRequest}.
509+
* @param {Object} request.image
471510
* A dictionary-like object representing the image. This should have a
472511
* single key (`source`, `content`).
473512
*
474513
* If the key is `source`, the value should be another object containing
475514
* `imageUri` or `filename` as a key and a string as a value.
476515
*
477516
* If the key is `content`, the value should be a Buffer.
478-
* @param {Object=} options
517+
* @param {Object=} callOptions
479518
* Optional parameters. You can override the default settings for this
480519
* call, e.g, timeout, retries, paginations, etc. See
481520
* [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
@@ -507,15 +546,18 @@ module.exports = apiVersion => {
507546
/**
508547
* Annotate a single image with crop hints.
509548
*
510-
* @param {Object=} image
549+
* @param {Object} request
550+
* A representation of the request being sent to the Vision API.
551+
* This is an [AnnotateImageRequest]{@link AnnotateImageRequest}.
552+
* @param {Object} request.image
511553
* A dictionary-like object representing the image. This should have a
512554
* single key (`source`, `content`).
513555
*
514556
* If the key is `source`, the value should be another object containing
515557
* `imageUri` or `filename` as a key and a string as a value.
516558
*
517559
* If the key is `content`, the value should be a Buffer.
518-
* @param {Object=} options
560+
* @param {Object=} callOptions
519561
* Optional parameters. You can override the default settings for this
520562
* call, e.g, timeout, retries, paginations, etc. See
521563
* [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
@@ -547,15 +589,18 @@ module.exports = apiVersion => {
547589
/**
548590
* Annotate a single image with web detection.
549591
*
550-
* @param {Object=} image
592+
* @param {Object} request
593+
* A representation of the request being sent to the Vision API.
594+
* This is an [AnnotateImageRequest]{@link AnnotateImageRequest}.
595+
* @param {Object} request.image
551596
* A dictionary-like object representing the image. This should have a
552597
* single key (`source`, `content`).
553598
*
554599
* If the key is `source`, the value should be another object containing
555600
* `imageUri` or `filename` as a key and a string as a value.
556601
*
557602
* If the key is `content`, the value should be a Buffer.
558-
* @param {Object=} options
603+
* @param {Object=} callOptions
559604
* Optional parameters. You can override the default settings for this
560605
* call, e.g, timeout, retries, paginations, etc. See
561606
* [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}

packages/google-cloud-vision/test/helpers.test.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ describe('Vision helper methods', () => {
260260

261261
// Ensure that the annotateImage method does *not* pass the callback
262262
// on to batchAnnotateImages, but rather handles it itself.
263-
var image = {content: new Buffer('bogus==')};
264-
return vision.logoDetection(image).then(r => {
263+
var imageRequest = {image: {content: new Buffer('bogus==')}};
264+
return vision.logoDetection(Object.assign({}, imageRequest)).then(r => {
265265
var response = r[0];
266266

267267
// Ensure that we got the slice of the response that we expected.
@@ -272,12 +272,26 @@ describe('Vision helper methods', () => {
272272
// Inspect the calls to annotateImage and batchAnnotateImages and
273273
// ensure they matched the expected signature.
274274
assert(annotate.callCount === 1);
275-
assert(annotate.calledWith({image: image, features: [{type: 3}]}));
275+
assert(annotate.calledWith({
276+
features: [{type: 3}],
277+
image: imageRequest.image,
278+
}));
276279
assert(batchAnnotate.callCount === 1);
277280
assert(batchAnnotate.calledWith(
278-
[{image: image, features: [{type: 3}]}]
281+
[{image: imageRequest.image, features: [{type: 3}]}]
279282
));
280283
});
281284
});
285+
286+
it('throws an exception if conflicting features are given', () => {
287+
var vision = Vision.v1();
288+
var imageRequest = {
289+
image: {content: new Buffer('bogus==')},
290+
features: [{type: 0}],
291+
};
292+
vision.logoDetection(imageRequest).then(assert.fail).catch(ex => {
293+
assert(ex.message.indexOf('Setting explicit') > -1);
294+
});
295+
});
282296
});
283297
});

0 commit comments

Comments
 (0)