Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 64aee6f

Browse files
replace import api call with fetch (#661)
Co-authored-by: Emilio Munoz <emmunozp@microsoft.com>
1 parent daf5b7d commit 64aee6f

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

packages/lu/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"dependencies": {
3737
"@azure/cognitiveservices-luis-authoring": "4.0.0-preview.1",
3838
"@azure/ms-rest-azure-js": "2.0.1",
39+
"@types/node-fetch": "~2.5.5",
3940
"antlr4": "^4.7.2",
4041
"chalk": "2.4.1",
4142
"console-stream": "^0.1.1",
@@ -46,7 +47,7 @@
4647
"globby": "^10.0.1",
4748
"intercept-stdout": "^0.1.2",
4849
"lodash": "^4.17.15",
49-
"node-fetch": "^2.1.2",
50+
"node-fetch": "~2.6.0",
5051
"semver": "^5.5.1",
5152
"tslib": "^1.10.0"
5253
},

packages/lu/src/parser/lubuild/builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ export class Builder {
313313
this.handler(`Creating LUIS.ai application: ${currentApp.name} version:${currentApp.versionId}\n`)
314314
await delay(delayDuration)
315315
const response = await luBuildCore.importApplication(currentApp)
316-
recognizer.setAppId(typeof response.body === 'string' ? response.body : response.body[Object.keys(response.body)[0]])
316+
recognizer.setAppId(typeof response === 'string' ? response : response[Object.keys(response)[0]])
317317
return true
318318
}
319319

packages/lu/src/parser/lubuild/core.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,23 @@ import {isEqual, differenceWith} from 'lodash'
1010
import {CognitiveServicesCredentials} from '@azure/ms-rest-azure-js'
1111
import {LUISAuthoringClient} from '@azure/cognitiveservices-luis-authoring'
1212
import * as path from 'path'
13+
import fetch from 'node-fetch'
14+
const retCode = require('./../utils/enums/CLI-errors')
15+
const exception = require('./../utils/exception')
1316
const Content = require('./../lu/lu')
1417
const LUOptions = require('./../lu/luOptions')
1518

1619
export class LuBuildCore {
1720
private readonly client: any
21+
private readonly subscriptionKey: string
22+
private readonly endpoint: string
23+
24+
constructor(subscriptionKey: string, endpoint: string) {
25+
this.subscriptionKey = subscriptionKey
26+
this.endpoint = endpoint
1827

19-
constructor(authoringKey: string, endpoint: string) {
2028
// new luis api client
21-
const creds = new CognitiveServicesCredentials(authoringKey)
29+
const creds = new CognitiveServicesCredentials(subscriptionKey)
2230
this.client = new LUISAuthoringClient(creds, endpoint)
2331
}
2432

@@ -35,9 +43,23 @@ export class LuBuildCore {
3543
}
3644

3745
public async importApplication(currentApp: any): Promise<any> {
38-
let response = await this.client.apps.importMethod(currentApp)
46+
// let response = await this.client.apps.importMethod(currentApp)
3947

40-
return response
48+
const name = `?appName=${currentApp.name}`
49+
const url = this.endpoint + '/luis/authoring/v3.0-preview/apps/import' + name
50+
const headers = {
51+
'Content-Type': 'application/json',
52+
'Ocp-Apim-Subscription-Key': this.subscriptionKey
53+
}
54+
55+
const response = await fetch(url, {method: 'POST', headers, body: JSON.stringify(currentApp)})
56+
const messageData = await response.json()
57+
58+
if (messageData.error) {
59+
throw (new exception(retCode.errorCode.LUIS_API_CALL_FAILED, messageData.error.message))
60+
}
61+
62+
return messageData
4163
}
4264

4365
public async exportApplication(appId: string, versionId: string) {
@@ -91,7 +113,23 @@ export class LuBuildCore {
91113
}
92114

93115
public async importNewVersion(appId: string, app: any, options: any) {
94-
await this.client.versions.importMethod(appId, app, options)
116+
// await this.client.versions.importMethod(appId, app, options)
117+
118+
const versionId = `?versionId=${options.versionId}`
119+
let url = this.endpoint + '/luis/authoring/v3.0-preview/apps/' + appId + '/versions/import' + versionId
120+
const headers = {
121+
'Content-Type': 'application/json',
122+
'Ocp-Apim-Subscription-Key': this.subscriptionKey
123+
}
124+
125+
const response = await fetch(url, {method: 'POST', headers, body: JSON.stringify(app)})
126+
const messageData = await response.json()
127+
128+
if (messageData.error) {
129+
throw (new exception(retCode.errorCode.LUIS_API_CALL_FAILED, messageData.error.message))
130+
}
131+
132+
return messageData
95133
}
96134

97135
public async listApplicationVersions(appId: string) {

packages/lu/src/parser/utils/enums/CLI-errors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports = {
2828
INVALID_URI: 21,
2929
INVALID_REGEX_ENTITY: 22,
3030
INVALID_COMPOSITE_ENTITY: 23,
31+
LUIS_API_CALL_FAILED: 24,
3132
UNKNOWN_ERROR: 99,
3233
BOUNDARY_INTENTS: 501,
3334
BOUNDARY_PATTERNANYENTITY: 502,

0 commit comments

Comments
 (0)