@@ -40,7 +40,10 @@ module.exports = function(RED) {
40
40
async = require ( 'async' ) ,
41
41
toArray = require ( 'stream-to-array' ) ,
42
42
sAPIKey = null ,
43
- service = null ;
43
+ iamAPIKey = false ,
44
+ service = null ,
45
+ endpoint = '' ,
46
+ sEndpoint = '' ;
44
47
45
48
// temp is being used for file streaming to allow the file to arrive so it can be processed.
46
49
temp . track ( ) ;
@@ -49,6 +52,7 @@ module.exports = function(RED) {
49
52
50
53
if ( service ) {
51
54
sAPIKey = service . api_key ;
55
+ sEndpoint = service . url ;
52
56
}
53
57
54
58
RED . httpAdmin . get ( '/watson-visual-recognition/vcap' , function ( req , res ) {
@@ -154,13 +158,23 @@ module.exports = function(RED) {
154
158
}
155
159
156
160
var serviceSettings = {
157
- api_key : node . apikey ,
158
161
version_date : '2018-03-19' ,
159
162
headers : {
160
163
'User-Agent' : pkg . name + '-' + pkg . version
161
164
}
162
165
} ;
163
166
167
+ if ( endpoint ) {
168
+ serviceSettings . url = endpoint ;
169
+ }
170
+
171
+ // VR instances created post 22 May 2018, are expecting an iam API Key
172
+ if ( iamAPIKey ) {
173
+ serviceSettings . iam_apikey = node . apikey ;
174
+ } else {
175
+ serviceSettings . api_key = node . apikey ;
176
+ }
177
+
164
178
// The change to watson-developer-cloud 3.0.x has resulted in a
165
179
// change in how the Accept-Language is specified. It now needs
166
180
// to go in as a header.
@@ -469,6 +483,7 @@ module.exports = function(RED) {
469
483
shape : 'dot' ,
470
484
text : 'Calling ' + feature + ' ...'
471
485
} ) ;
486
+
472
487
if ( feature === 'classifyImage' || feature === 'detectFaces' ) {
473
488
return executeService ( feature , params , node , msg ) ;
474
489
//return Promise.resolve();
@@ -481,6 +496,22 @@ module.exports = function(RED) {
481
496
}
482
497
}
483
498
499
+ function determineEndpoint ( config ) {
500
+ // Any VR instances created post 22 May 2018, have a different endpoint
501
+ // and mechanism for authentication. This function detemines if this
502
+ // new authentication mechanism is being utlised.
503
+ iamAPIKey = false ;
504
+
505
+ endpoint = sEndpoint ;
506
+ if ( ! endpoint && config [ 'vr-service-endpoint' ] ) {
507
+ endpoint = config [ 'vr-service-endpoint' ] ;
508
+ }
509
+ if ( endpoint && 'https://gateway.watsonplatform.net/visual-recognition/api' === endpoint ) {
510
+ iamAPIKey = true ;
511
+ }
512
+ return Promise . resolve ( ) ;
513
+ }
514
+
484
515
// This is the Watson Visual Recognition V3 Node
485
516
function WatsonVisualRecognitionV3Node ( config ) {
486
517
var node = this ,
@@ -506,6 +537,9 @@ module.exports = function(RED) {
506
537
. then ( function ( ) {
507
538
return verifyInputs ( feature , msg ) ;
508
539
} )
540
+ . then ( function ( ) {
541
+ return determineEndpoint ( config ) ;
542
+ } )
509
543
. then ( function ( ) {
510
544
return verifyServiceCredentials ( node , msg ) ;
511
545
} )
0 commit comments