1717module . exports = function ( RED ) {
1818 const request = require ( 'request' ) ,
1919 SERVICE_IDENTIFIER = 'language-translator' ,
20- SERVICE_VERSION = '2018-05-01' ;
20+ SERVICE_VERSION = '2018-05-01' ,
21+ LanguageTranslatorV3 = require ( 'ibm-watson/language-translator/v3' ) ,
22+ { IamAuthenticator } = require ( 'ibm-watson/auth' ) ;
2123
2224 var pkg = require ( '../../package.json' ) ,
23- LanguageTranslatorV3 = require ( 'watson-developer-cloud/language-translator/v3' ) ,
2425 fs = require ( 'fs' ) ,
2526 fileType = require ( 'file-type' ) ,
2627 temp = require ( 'temp' ) ,
@@ -54,6 +55,7 @@ module.exports = function (RED) {
5455 RED . httpAdmin . get ( '/watson-doc-translator/models' , function ( req , res ) {
5556 endpoint = req . query . e ? req . query . e : sEndpoint ;
5657 var lt = null ,
58+ authSettings = { } ,
5759 serviceSettings = {
5860 version : SERVICE_VERSION ,
5961 url : endpoint ,
@@ -63,24 +65,28 @@ module.exports = function (RED) {
6365 } ;
6466
6567 if ( sApikey || req . query . key ) {
66- serviceSettings . iam_apikey = sApikey ? sApikey : req . query . key ;
68+ authSettings . apikey = sApikey ? sApikey : req . query . key ;
6769 } else {
68- serviceSettings . username = sUsername ? sUsername : req . query . un ;
69- serviceSettings . password = sPassword ? sPassword : req . query . pwd ;
70+ authSettings . username = sUsername ? sUsername : req . query . un ;
71+ authSettings . password = sPassword ? sPassword : req . query . pwd ;
7072 }
73+ serviceSettings . authenticator = new IamAuthenticator ( authSettings ) ;
7174
7275 lt = new LanguageTranslatorV3 ( serviceSettings ) ;
7376
74- lt . listModels ( { } , function ( err , models ) {
75- if ( err ) {
76- res . json ( err ) ;
77- } else {
77+ lt . listModels ( { } )
78+ . then ( ( response ) => {
79+ let models = [ ] ;
80+ if ( response && response . result && response . result . models ) {
81+ models = response . result ;
82+ }
7883 res . json ( models ) ;
79- }
80- } ) ;
84+ } )
85+ . catch ( ( err ) => {
86+ res . json ( err ) ;
87+ } )
8188 } ) ;
8289
83-
8490 function Node ( config ) {
8591 var node = this ;
8692 RED . nodes . createNode ( this , config ) ;
@@ -144,6 +150,30 @@ module.exports = function (RED) {
144150 return Promise . resolve ( ) ;
145151 }
146152
153+ function getService ( ) {
154+ let authSettings = { } ,
155+ serviceSettings = {
156+ version : '2018-05-01' ,
157+ headers : {
158+ 'User-Agent' : pkg . name + '-' + pkg . version
159+ }
160+ } ;
161+
162+ if ( apikey ) {
163+ authSettings . apikey = apikey ;
164+ } else {
165+ authSettings . username = username ;
166+ authSettings . password = password ;
167+ }
168+ serviceSettings . authenticator = new IamAuthenticator ( authSettings ) ;
169+
170+ if ( endpoint ) {
171+ serviceSettings . url = endpoint ;
172+ }
173+
174+ return new LanguageTranslatorV3 ( serviceSettings ) ;
175+ }
176+
147177
148178 function buildAuthSettings ( ) {
149179 var authSettings = { } ;
@@ -186,7 +216,6 @@ module.exports = function (RED) {
186216 reject ( 'Document not found ' + response . statusCode ) ;
187217 break ;
188218 default :
189- console . log ( body ) ;
190219 reject ( 'Error Invoking API ' + response . statusCode ) ;
191220 break ;
192221 }
@@ -324,10 +353,46 @@ module.exports = function (RED) {
324353 }
325354
326355 function executeGetDocument ( msg ) {
327- var docid = docID ( msg ) ;
328- let uriAddress = `${ endpoint } /v3/documents/${ docid } /translated_document?version=${ SERVICE_VERSION } ` ;
356+ return new Promise ( function resolver ( resolve , reject ) {
357+ let lt = getService ( ) ,
358+ docid = docID ( msg ) ;
359+
360+ lt . getTranslatedDocument ( { documentId : docid } )
361+ . then ( ( response ) => {
362+ msg . payload = response ;
363+ if ( response && response . result ) {
364+ msg . payload = response . result ;
365+ }
366+ return payloadutils . checkForStream ( msg ) ;
367+ } )
368+ . then ( ( ) => {
369+ resolve ( msg . payload ) ;
370+ } )
371+ . catch ( ( err ) => {
372+ reject ( err ) ;
373+ } ) ;
374+ } ) ;
375+ }
329376
330- return executeGetRequest ( uriAddress ) ;
377+ function executeGetDocumentXX ( msg ) {
378+ return new Promise ( function resolver ( resolve , reject ) {
379+ var docid = docID ( msg ) ;
380+ let uriAddress = `${ endpoint } /v3/documents/${ docid } /translated_document?version=${ SERVICE_VERSION } ` ;
381+
382+ executeGetRequest ( uriAddress )
383+ . then ( ( response ) => {
384+ msg . payload = response ;
385+ return payloadutils . checkForStream ( msg ) ;
386+ } )
387+ . then ( ( ) => {
388+ resolve ( msg . payload ) ;
389+ } )
390+ . catch ( ( err ) => {
391+ reject ( err ) ;
392+ } ) ;
393+ } ) ;
394+
395+ // return executeGetRequest(uriAddress);
331396 }
332397
333398 function executeDeleteDocument ( msg ) {
@@ -416,7 +481,7 @@ module.exports = function (RED) {
416481 return Promise . resolve ( ) ;
417482 }
418483
419- function doit ( msg ) {
484+ function doit ( msg , send ) {
420485 let action = msg . action || config . action ;
421486
422487 translatorutils . credentialCheck ( username , password , apikey )
@@ -440,16 +505,16 @@ module.exports = function (RED) {
440505 . then ( function ( ) {
441506 temp . cleanup ( ) ;
442507 node . status ( { } ) ;
443- node . send ( msg ) ;
508+ send ( msg ) ;
444509 } )
445510 . catch ( function ( err ) {
446511 temp . cleanup ( ) ;
447512 payloadutils . reportError ( node , msg , err ) ;
448- node . send ( msg ) ;
513+ send ( msg ) ;
449514 } ) ;
450515 }
451516
452- this . on ( 'input' , function ( msg ) {
517+ this . on ( 'input' , function ( msg , send , done ) {
453518 // The dynamic nature of this node has caused problems with the password field. it is
454519 // hidden but not a credential. If it is treated as a credential, it gets lost when there
455520 // is a request to refresh the model list.
@@ -459,7 +524,7 @@ module.exports = function (RED) {
459524 apikey = sApikey || this . credentials . apikey || config . apikey ;
460525
461526 endpoint = sEndpoint ;
462- if ( ( ! config [ 'default-endpoint' ] ) && config [ 'service-endpoint' ] ) {
527+ if ( config [ 'service-endpoint' ] ) {
463528 endpoint = config [ 'service-endpoint' ] ;
464529 }
465530
@@ -476,12 +541,13 @@ module.exports = function (RED) {
476541 pos = i + 1 ;
477542 node . status ( { fill : 'blue' , shape : 'dot' , text : `Processing document ${ pos } of ${ len } ` } ) ;
478543 msgClone . payload = e ;
479- doit ( msgClone ) ;
544+ doit ( msgClone , send ) ;
480545 } ) ;
481546
482547 } else {
483- doit ( msg ) ;
548+ doit ( msg , send ) ;
484549 }
550+ done ( ) ;
485551 } ) ;
486552 }
487553
0 commit comments