33 * Licensed under the MIT License.
44 */
55
6- import { CognitiveServicesCredentials } from '@azure/ms-rest-azure-js'
7- import { LUISAuthoringClient } from '@azure/cognitiveservices-luis-authoring'
8- import fetch from 'node-fetch'
6+ import httpRequest from './http-request'
97
108const delay = require ( 'delay' )
119const os = require ( 'os' )
1210const Luis = require ( './../luis/luis' )
1311const packageJSON = require ( './../../../package' )
1412
15- const rateLimitErrorCode = 429
13+ const rateLimitErrorCode = ' 429'
1614const absoluteUrlPattern = / ^ h t t p s ? : \/ \/ / i
1715
1816export class LuBuildCore {
19- private readonly client : any
2017 private readonly subscriptionKey : string
2118 private readonly endpoint : string
2219 private readonly retryCount : number
@@ -26,7 +23,7 @@ export class LuBuildCore {
2623
2724 constructor ( subscriptionKey : string , endpoint : string , retryCount : number , retryDuration : number ) {
2825 this . subscriptionKey = subscriptionKey
29- this . endpoint = endpoint
26+ this . endpoint = ` ${ endpoint } /luis/authoring/v3.0-preview`
3027 this . retryCount = retryCount
3128 this . retryDuration = retryDuration
3229
@@ -47,30 +44,24 @@ export class LuBuildCore {
4744 'User-Agent' : luisUserAgent ,
4845 'Ocp-Apim-Subscription-Key' : this . subscriptionKey ,
4946 }
50-
51- // new luis api client
52- const options = {
53- userAgent : luisUserAgent
54- }
55-
56- const creds = new CognitiveServicesCredentials ( subscriptionKey )
57- this . client = new LUISAuthoringClient ( creds , endpoint , options )
5847 }
5948
6049 public async getApplicationList ( ) {
50+ const url = `${ this . endpoint } /apps`
51+
6152 let apps
6253 let retryCount = this . retryCount + 1
6354 let error
6455 while ( retryCount > 0 ) {
65- if ( error === undefined || error . statusCode === rateLimitErrorCode ) {
66- try {
67- apps = await this . client . apps . list ( undefined , undefined )
68- break
69- } catch ( e ) {
70- error = e
71- retryCount --
72- if ( retryCount > 0 ) await delay ( this . retryDuration )
73- }
56+ if ( error === undefined || error . code === rateLimitErrorCode ) {
57+
58+ apps = await httpRequest . get ( url , this . headers )
59+
60+ if ( apps . error === undefined ) break
61+
62+ error = apps . error
63+ retryCount --
64+ if ( retryCount > 0 ) await delay ( this . retryDuration )
7465 } else {
7566 throw error
7667 }
@@ -84,19 +75,20 @@ export class LuBuildCore {
8475 }
8576
8677 public async getApplicationInfo ( appId : string ) {
78+ const url = `${ this . endpoint } /apps/${ appId } `
79+
8780 let appInfo
8881 let retryCount = this . retryCount + 1
8982 let error
9083 while ( retryCount > 0 ) {
91- if ( error === undefined || error . statusCode === rateLimitErrorCode ) {
92- try {
93- appInfo = await this . client . apps . get ( appId )
94- break
95- } catch ( e ) {
96- error = e
97- retryCount --
98- if ( retryCount > 0 ) await delay ( this . retryDuration )
99- }
84+ if ( error === undefined || error . code === rateLimitErrorCode ) {
85+ appInfo = await httpRequest . get ( url , this . headers )
86+
87+ if ( appInfo . error === undefined ) break
88+
89+ error = appInfo . error
90+ retryCount --
91+ if ( retryCount > 0 ) await delay ( this . retryDuration )
10092 } else {
10193 throw error
10294 }
@@ -110,18 +102,15 @@ export class LuBuildCore {
110102 }
111103
112104 public async importApplication ( currentApp : any ) : Promise < any > {
113- // let response = await this.client.apps.importMethod(currentApp)
114-
115105 const name = `?appName=${ currentApp . name } `
116- const url = this . endpoint + '/luis/authoring/v3.0-preview/ apps/import' + name
106+ const url = ` ${ this . endpoint } / apps/import${ name } `
117107
118108 let messageData
119109 let retryCount = this . retryCount + 1
120110 let error : any
121111 while ( retryCount > 0 ) {
122- if ( error === undefined || error . code === rateLimitErrorCode . toString ( ) ) {
123- let response = await fetch ( url , { method : 'POST' , headers : this . headers , body : JSON . stringify ( currentApp ) } )
124- messageData = await response . json ( )
112+ if ( error === undefined || error . code === rateLimitErrorCode ) {
113+ messageData = await httpRequest . post ( url , JSON . stringify ( currentApp ) , this . headers )
125114
126115 if ( messageData . error === undefined ) break
127116
@@ -141,22 +130,20 @@ export class LuBuildCore {
141130 }
142131
143132 public async exportApplication ( appId : string , versionId : string ) {
144- const url = this . endpoint + '/luis/authoring/v3.0-preview/ apps/' + appId + ' /versions/' + versionId + ' /export?format=json'
133+ const url = ` ${ this . endpoint } / apps/${ appId } /versions/${ versionId } /export?format=json`
145134
146135 let messageData
147136 let retryCount = this . retryCount + 1
148137 let error
149138 while ( retryCount > 0 ) {
150- if ( error === undefined || error . statusCode === rateLimitErrorCode ) {
151- try {
152- const response = await fetch ( url , { method : 'GET' , headers : this . headers } )
153- messageData = await response . json ( )
154- break
155- } catch ( e ) {
156- error = e
157- retryCount --
158- if ( retryCount > 0 ) await delay ( this . retryDuration )
159- }
139+ if ( error === undefined || error . code === rateLimitErrorCode ) {
140+ messageData = await httpRequest . get ( url , this . headers )
141+
142+ if ( messageData . error === undefined ) break
143+
144+ error = messageData . error
145+ retryCount --
146+ if ( retryCount > 0 ) await delay ( this . retryDuration )
160147 } else {
161148 throw error
162149 }
@@ -166,10 +153,6 @@ export class LuBuildCore {
166153 throw error
167154 }
168155
169- if ( messageData . error ) {
170- throw new Error ( messageData . error . message )
171- }
172-
173156 return messageData
174157 }
175158
@@ -224,19 +207,15 @@ export class LuBuildCore {
224207 }
225208
226209 public async importNewVersion ( appId : string , app : any , options : any ) {
227- // await this.client.versions.importMethod(appId, app, options)
228-
229210 const versionId = `?versionId=${ options . versionId } `
230- let url = this . endpoint + '/luis/authoring/v3.0-preview/ apps/' + appId + ' /versions/import' + versionId
211+ let url = ` ${ this . endpoint } / apps/${ appId } /versions/import${ versionId } `
231212
232213 let messageData
233214 let retryCount = this . retryCount + 1
234215 let error : any
235216 while ( retryCount > 0 ) {
236- if ( error === undefined || error . code === rateLimitErrorCode . toString ( ) ) {
237- let response = await fetch ( url , { method : 'POST' , headers : this . headers , body : JSON . stringify ( app ) } )
238- messageData = await response . json ( )
239-
217+ if ( error === undefined || error . code === rateLimitErrorCode ) {
218+ messageData = await httpRequest . post ( url , JSON . stringify ( app ) , this . headers )
240219 if ( messageData . error === undefined ) break
241220
242221 error = messageData . error
@@ -255,19 +234,19 @@ export class LuBuildCore {
255234 }
256235
257236 public async listApplicationVersions ( appId : string ) {
237+ let url = `${ this . endpoint } /apps/${ appId } /versions`
238+
258239 let appVersions
259240 let retryCount = this . retryCount + 1
260241 let error
261242 while ( retryCount > 0 ) {
262- if ( error === undefined || error . statusCode === rateLimitErrorCode ) {
263- try {
264- appVersions = await this . client . versions . list ( appId )
265- break
266- } catch ( e ) {
267- error = e
268- retryCount --
269- if ( retryCount > 0 ) await delay ( this . retryDuration )
270- }
243+ if ( error === undefined || error . code === rateLimitErrorCode ) {
244+ appVersions = await httpRequest . get ( url , this . headers )
245+ if ( appVersions . error === undefined ) break
246+
247+ error = appVersions . error
248+ retryCount --
249+ if ( retryCount > 0 ) await delay ( this . retryDuration )
271250 } else {
272251 throw error
273252 }
@@ -281,18 +260,19 @@ export class LuBuildCore {
281260 }
282261
283262 public async deleteVersion ( appId : string , versionId : string ) {
263+ let url = `${ this . endpoint } /apps/${ appId } /versions/${ versionId } `
264+
265+ let messageData
284266 let retryCount = this . retryCount + 1
285267 let error
286268 while ( retryCount > 0 ) {
287- if ( error === undefined || error . statusCode === rateLimitErrorCode ) {
288- try {
289- await this . client . versions . deleteMethod ( appId , versionId )
290- break
291- } catch ( e ) {
292- error = e
293- retryCount --
294- if ( retryCount > 0 ) await delay ( this . retryDuration )
295- }
269+ if ( error === undefined || error . code === rateLimitErrorCode ) {
270+ messageData = await httpRequest . delete ( url , this . headers )
271+ if ( messageData . error === undefined ) break
272+
273+ error = messageData . error
274+ retryCount --
275+ if ( retryCount > 0 ) await delay ( this . retryDuration )
296276 } else {
297277 throw error
298278 }
@@ -301,20 +281,21 @@ export class LuBuildCore {
301281 if ( retryCount === 0 ) {
302282 throw error
303283 }
284+
285+ return messageData
304286 }
305287
306288 public async trainApplication ( appId : string , versionId : string , trainMode : string ) {
307289 let mode = trainMode || this . trainMode
308- let url = `${ this . endpoint } /luis/authoring/v3.0-preview/ apps/${ appId } /versions/${ versionId } /train`
290+ let url = `${ this . endpoint } /apps/${ appId } /versions/${ versionId } /train`
309291 url += mode ? `?mode=${ mode } ` : ''
310292
311293 let messageData
312294 let retryCount = this . retryCount + 1
313295 let error : any
314296 while ( retryCount > 0 ) {
315- if ( error === undefined || error . code === rateLimitErrorCode . toString ( ) ) {
316- let response = await fetch ( url , { method : 'POST' , headers : this . headers } )
317- messageData = await response . json ( )
297+ if ( error === undefined || error . code === rateLimitErrorCode ) {
298+ messageData = await httpRequest . post ( url , '' , this . headers )
318299
319300 if ( messageData . error === undefined ) break
320301
@@ -334,19 +315,19 @@ export class LuBuildCore {
334315 }
335316
336317 public async getTrainingStatus ( appId : string , versionId : string ) {
318+ let url = `${ this . endpoint } /apps/${ appId } /versions/${ versionId } /train`
319+
337320 let status
338321 let retryCount = this . retryCount + 1
339322 let error
340323 while ( retryCount > 0 ) {
341- if ( error === undefined || error . statusCode === rateLimitErrorCode ) {
342- try {
343- status = await this . client . train . getStatus ( appId , versionId )
344- break
345- } catch ( e ) {
346- error = e
347- retryCount --
348- if ( retryCount > 0 ) await delay ( this . retryDuration )
349- }
324+ if ( error === undefined || error . code === rateLimitErrorCode ) {
325+ status = await httpRequest . get ( url , this . headers )
326+ if ( status . error === undefined ) break
327+
328+ error = status . error
329+ retryCount --
330+ if ( retryCount > 0 ) await delay ( this . retryDuration )
350331 } else {
351332 throw error
352333 }
@@ -360,19 +341,21 @@ export class LuBuildCore {
360341 }
361342
362343 public async publishApplication ( appId : string , versionId : string , isStaging : boolean , directVersionPublish : boolean ) {
363- let url = this . endpoint + '/luis/authoring/v3.0-preview/ apps/' + appId + ' /publish'
344+ let url = ` ${ this . endpoint } / apps/${ appId } /publish`
364345
365346 let messageData
366347 let retryCount = this . retryCount + 1
367348 let error : any
368349 while ( retryCount > 0 ) {
369- if ( error === undefined || error . code === rateLimitErrorCode . toString ( ) ) {
370- let response = await fetch ( url , { method : 'POST' , headers : this . headers , body : JSON . stringify ( {
371- versionId,
372- isStaging,
373- directVersionPublish
374- } ) } )
375- messageData = await response . json ( )
350+ if ( error === undefined || error . code === rateLimitErrorCode ) {
351+ messageData = await httpRequest . post (
352+ url ,
353+ JSON . stringify ( {
354+ versionId,
355+ isStaging,
356+ directVersionPublish
357+ } ) ,
358+ this . headers )
376359
377360 if ( messageData . error === undefined ) break
378361
0 commit comments