@@ -3,6 +3,7 @@ import extend = require('extend');
33import { Agent , OutgoingHttpHeaders } from 'http' ;
44import { UserOptions } from 'ibm-cloud-sdk-core' ;
55import isStream = require( 'isstream' ) ;
6+ import { resolve } from 'path' ;
67import { getSdkHeaders } from '../lib/common' ;
78import RecognizeStream = require( '../lib/recognize-stream' ) ;
89import GeneratedSpeechToTextV1 = require( './v1-generated' ) ;
@@ -49,77 +50,61 @@ class SpeechToTextV1 extends GeneratedSpeechToTextV1 {
4950 * @param {Number } [params.times=30] - maximum number of attempts
5051 * @param {Function } callback
5152 */
52- whenCorporaAnalyzed ( params : SpeechToTextV1 . WhenCorporaAnalyzedParams , callback : SpeechToTextV1 . Callback < GeneratedSpeechToTextV1 . Corpora > ) : void {
53+ async whenCorporaAnalyzed ( params : SpeechToTextV1 . WhenCorporaAnalyzedParams ) : Promise < any > {
5354 const self = this ;
5455
55- async . parallel (
56- [
57- // validate that it has at least one corpus
58- ( next : SpeechToTextV1 . Callback < GeneratedSpeechToTextV1 . Corpora > ) => {
59- self . listCorpora ( params , ( err , res ) => {
60- const result = res . result ;
61- if ( err ) {
62- return next ( err ) ;
63- }
64- if ( ! result . corpora . length ) {
65- const sttError : SpeechToTextV1 . SpeechToTextError = new Error (
66- 'Customization has no corpa and therefore corpus cannot be analyzed'
67- ) ;
68- sttError . code = SpeechToTextV1 . ERR_NO_CORPORA ;
69- return next ( sttError ) ;
70- }
71- next ( null ) ;
72- } ) ;
73- } ,
74- // check the customization status repeatedly until it's available
75- ( next : SpeechToTextV1 . Callback < GeneratedSpeechToTextV1 . Corpora > ) => {
76- const options : SpeechToTextV1 . WhenCorporaAnalyzedOptions = extend (
77- {
78- interval : 5000 ,
79- times : 30
80- } ,
81- params ,
82- {
83- errorFilter : ( err : SpeechToTextV1 . SpeechToTextError ) : boolean => {
84- // if it's a timeout error, then listCorpora is called again after params.interval
85- // otherwise the error is passed back to the user
86- // if the params.times limit is reached, the error will be passed to the user regardless
87- return err . code === SpeechToTextV1 . ERR_TIMEOUT ;
88- }
89- }
90- ) ;
56+ try {
57+ const res = await self . listCorpora ( params )
58+ const result = res . result ;
59+ if ( ! result . corpora . length ) {
60+ const sttError : SpeechToTextV1 . SpeechToTextError = new Error (
61+ 'Customization has no corpa and therefore corpus cannot be analyzed'
62+ ) ;
63+ sttError . code = SpeechToTextV1 . ERR_NO_CORPORA ;
64+ return Promise . reject < any > ( sttError )
65+ }
66+ } catch ( err ) {
67+ return Promise . reject < any > ( err )
68+ }
9169
92- async . retry (
93- options ,
94- ( done : SpeechToTextV1 . Callback < GeneratedSpeechToTextV1 . Corpora > ) => {
95- self . listCorpora ( params , ( err , res ) => {
96- const corpora = res . result ;
97- if ( err ) {
98- done ( err ) ;
99- } else if ( corpora !== undefined && isProcessing ( corpora ) ) {
100- // if the loop times out, async returns the last error, which will be this one.
101- const sttError : SpeechToTextV1 . SpeechToTextError = new Error (
102- 'Corpora is still being processed, try increasing interval or times params'
103- ) ;
104- sttError . code = SpeechToTextV1 . ERR_TIMEOUT ;
105- done ( sttError ) ;
106- } else if ( corpora !== undefined && isAnalyzed ( corpora ) ) {
107- done ( null , corpora ) ;
108- } else {
109- done ( new Error ( 'Unexpected corpus analysis status' ) ) ;
110- }
111- } ) ;
112- } ,
113- next
114- ) ;
70+ const options : SpeechToTextV1 . WhenCorporaAnalyzedOptions = extend (
71+ {
72+ interval : 5000 ,
73+ times : 30
74+ } ,
75+ params ,
76+ {
77+ errorFilter : ( err : SpeechToTextV1 . SpeechToTextError ) : boolean => {
78+ // if it's a timeout error, then listCorpora is called again after params.interval
79+ // otherwise the error is passed back to the user
80+ // if the params.times limit is reached, the error will be passed to the user regardless
81+ return err . code === SpeechToTextV1 . ERR_TIMEOUT ;
11582 }
116- ] ,
117- ( err : Error | SpeechToTextV1 . SpeechToTextError | null , res ?: [ null , GeneratedSpeechToTextV1 . Corpora ] ) => {
118- const result = res ;
119- if ( err ) {
120- return callback ( err ) ;
83+ }
84+ ) ;
85+
86+ return async . retry (
87+ options ,
88+ async ( done ) => {
89+ try {
90+ const res = await self . listCorpora ( params )
91+ const corpora = res . result
92+
93+ if ( corpora !== undefined && isProcessing ( corpora ) ) {
94+ // if the loop times out, async returns the last error, which will be this one.
95+ const sttError : SpeechToTextV1 . SpeechToTextError = new Error (
96+ 'Corpora is still being processed, try increasing interval or times params'
97+ ) ;
98+ sttError . code = SpeechToTextV1 . ERR_TIMEOUT ;
99+ done ( sttError )
100+ } else if ( corpora !== undefined && isAnalyzed ( corpora ) ) {
101+ done ( null , corpora )
102+ } else {
103+ done ( new Error ( 'Unexpected corpus analysis status' ) ) ;
104+ }
105+ } catch ( err ) {
106+ done ( err )
121107 }
122- callback ( null , result [ 1 ] ) ; // callback with the final customization object
123108 }
124109 ) ;
125110 }
@@ -152,13 +137,12 @@ class SpeechToTextV1 extends GeneratedSpeechToTextV1 {
152137 return new RecognizeStream ( streamParams ) ;
153138 }
154139
155- recognize ( params : GeneratedSpeechToTextV1 . RecognizeParams , callback : GeneratedSpeechToTextV1 . Callback < GeneratedSpeechToTextV1 . SpeechRecognitionResults > ) : Promise < GeneratedSpeechToTextV1 . Response < GeneratedSpeechToTextV1 . SpeechRecognitionResults > > {
140+ recognize ( params : GeneratedSpeechToTextV1 . RecognizeParams ) : Promise < GeneratedSpeechToTextV1 . Response < GeneratedSpeechToTextV1 . SpeechRecognitionResults > > {
156141 if ( params && params . audio && isStream ( params . audio ) && ! params . contentType ) {
157- callback ( new Error ( 'If providing `audio` as a Stream, `contentType` is required.' ) ) ;
158- return ;
142+ return Promise . reject ( new Error ( 'If providing `audio` as a Stream, `contentType` is required.' ) )
159143 }
160144
161- return super . recognize ( params , callback ) ;
145+ return super . recognize ( params ) ;
162146 }
163147
164148 /**
@@ -174,11 +158,10 @@ class SpeechToTextV1 extends GeneratedSpeechToTextV1 {
174158 * @param {Number } [params.times=30] - maximum number of attempts
175159 * @param {Function } callback
176160 */
177- whenCustomizationReady ( params : SpeechToTextV1 . WhenCustomizationReadyParams , callback : SpeechToTextV1 . Callback < GeneratedSpeechToTextV1 . LanguageModel > ) : void {
161+ async whenCustomizationReady ( params : SpeechToTextV1 . WhenCustomizationReadyParams ) : Promise < any > {
178162 const self = this ;
179163
180164 // check the customization status repeatedly until it's ready or available
181-
182165 const options : SpeechToTextV1 . WhenCustomizationReadyOptions = extend (
183166 {
184167 interval : 5000 ,
@@ -194,47 +177,48 @@ class SpeechToTextV1 extends GeneratedSpeechToTextV1 {
194177 }
195178 }
196179 ) ;
197- async . retry (
180+ return async . retry (
198181 options ,
199- ( next : SpeechToTextV1 . Callback < GeneratedSpeechToTextV1 . LanguageModel > ) => {
200- self . getLanguageModel ( params , ( err , res ) => {
201- const customization = err ? null : res . result ;
202- if ( err ) {
203- next ( err ) ;
204- } else if (
205- customization . status === 'pending' ||
206- customization . status === 'training'
207- ) {
208- // if the loop times out, async returns the last error, which will be this one.
209- const sttError : SpeechToTextV1 . SpeechToTextError = new Error (
210- 'Customization is still pending, try increasing interval or times params' ,
211- ) ;
212- sttError . code = SpeechToTextV1 . ERR_TIMEOUT ;
213- next ( sttError ) ;
214- } else if (
215- customization . status === 'ready' ||
216- customization . status === 'available'
217- ) {
218- next ( null , customization ) ;
219- } else if ( customization . status === 'failed' ) {
220- next ( new Error ( 'Customization training failed' ) ) ;
221- } else {
222- next (
223- new Error (
224- 'Unexpected customization status: ' + customization . status
225- )
226- ) ;
227- }
228- } ) ;
229- } ,
230- callback
182+ async ( done ) => {
183+ try {
184+ const res = await self . getLanguageModel ( params )
185+ const customization = res . result ;
186+ if (
187+ customization . status === 'pending' ||
188+ customization . status === 'training'
189+ ) {
190+ // if the loop times out, async returns the last error, which will be this one.
191+ const sttError : SpeechToTextV1 . SpeechToTextError = new Error (
192+ 'Customization is still pending, try increasing interval or times params' ,
193+ ) ;
194+ sttError . code = SpeechToTextV1 . ERR_TIMEOUT ;
195+ done ( sttError ) ;
196+ }
197+
198+ else if ( customization . status === 'ready' ||
199+ customization . status === 'available' ) {
200+ done ( null , customization ) ;
201+ }
202+
203+ else if ( customization . status === 'failed' ) {
204+ done ( new Error ( 'Customization training failed' ) ) ;
205+ }
206+ else {
207+ done (
208+ new Error (
209+ 'Unexpected customization status: ' + customization . status
210+ )
211+ ) ;
212+ }
213+ } catch ( err ) {
214+ done ( err )
215+ }
216+ }
231217 ) ;
232218 }
233219}
234220
235221namespace SpeechToTextV1 {
236- export type Callback < T > = ( err : Error | SpeechToTextError | null , res ?: T ) => void ;
237-
238222 export interface SpeechToTextError extends Error {
239223 message : string ;
240224 code ?: string ;
0 commit comments