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

Commit a2ab9dd

Browse files
authored
Support to customize user agent for luis and qnamaker build api calls from process env (#1070)
* support output to file for kb:export command * enable luis and qnamaker build to customize user agent from env
1 parent 3315f42 commit a2ab9dd

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

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

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {LUISAuthoringClient} from '@azure/cognitiveservices-luis-authoring'
88
import fetch from 'node-fetch'
99

1010
const delay = require('delay')
11+
const os = require('os')
1112
const Luis = require('./../luis/luis')
13+
const packageJSON = require('./../../../package')
1214

1315
const rateLimitErrorCode = 429
1416
const absoluteUrlPattern = /^https?:\/\//i
@@ -19,6 +21,7 @@ export class LuBuildCore {
1921
private readonly endpoint: string
2022
private readonly retryCount: number
2123
private readonly retryDuration: number
24+
private readonly headers: any
2225

2326
constructor(subscriptionKey: string, endpoint: string, retryCount: number, retryDuration: number) {
2427
this.subscriptionKey = subscriptionKey
@@ -31,9 +34,23 @@ export class LuBuildCore {
3134
throw new Error(`Only absolute URLs are supported. "${endpoint}" is not an absolute LUIS endpoint URL.`)
3235
}
3336

37+
// set user agent
38+
const luisUserAgent = process.env['LUIS_USER_AGENT'] || this.getUserAgent()
39+
40+
// set headers
41+
this.headers = {
42+
'Content-Type': 'application/json',
43+
'User-Agent': luisUserAgent,
44+
'Ocp-Apim-Subscription-Key': this.subscriptionKey,
45+
}
46+
3447
// new luis api client
48+
const options = {
49+
userAgent: luisUserAgent
50+
}
51+
3552
const creds = new CognitiveServicesCredentials(subscriptionKey)
36-
this.client = new LUISAuthoringClient(creds, endpoint)
53+
this.client = new LUISAuthoringClient(creds, endpoint, options)
3754
}
3855

3956
public async getApplicationList() {
@@ -93,17 +110,13 @@ export class LuBuildCore {
93110

94111
const name = `?appName=${currentApp.name}`
95112
const url = this.endpoint + '/luis/authoring/v3.0-preview/apps/import' + name
96-
const headers = {
97-
'Content-Type': 'application/json',
98-
'Ocp-Apim-Subscription-Key': this.subscriptionKey
99-
}
100113

101114
let messageData
102115
let retryCount = this.retryCount + 1
103116
let error: any
104117
while (retryCount > 0) {
105118
if (error === undefined || error.code === rateLimitErrorCode.toString()) {
106-
let response = await fetch(url, {method: 'POST', headers, body: JSON.stringify(currentApp)})
119+
let response = await fetch(url, {method: 'POST', headers: this.headers, body: JSON.stringify(currentApp)})
107120
messageData = await response.json()
108121

109122
if (messageData.error === undefined) break
@@ -125,18 +138,14 @@ export class LuBuildCore {
125138

126139
public async exportApplication(appId: string, versionId: string) {
127140
const url = this.endpoint + '/luis/authoring/v3.0-preview/apps/' + appId + '/versions/' + versionId + '/export?format=json'
128-
const headers = {
129-
'Content-Type': 'application/json',
130-
'Ocp-Apim-Subscription-Key': this.subscriptionKey
131-
}
132141

133142
let messageData
134143
let retryCount = this.retryCount + 1
135144
let error
136145
while (retryCount > 0) {
137146
if (error === undefined || error.statusCode === rateLimitErrorCode) {
138147
try {
139-
const response = await fetch(url, {method: 'GET', headers})
148+
const response = await fetch(url, {method: 'GET', headers: this.headers})
140149
messageData = await response.json()
141150
break
142151
} catch (e) {
@@ -215,17 +224,13 @@ export class LuBuildCore {
215224

216225
const versionId = `?versionId=${options.versionId}`
217226
let url = this.endpoint + '/luis/authoring/v3.0-preview/apps/' + appId + '/versions/import' + versionId
218-
const headers = {
219-
'Content-Type': 'application/json',
220-
'Ocp-Apim-Subscription-Key': this.subscriptionKey
221-
}
222227

223228
let messageData
224229
let retryCount = this.retryCount + 1
225230
let error: any
226231
while (retryCount > 0) {
227232
if (error === undefined || error.code === rateLimitErrorCode.toString()) {
228-
let response = await fetch(url, {method: 'POST', headers, body: JSON.stringify(app)})
233+
let response = await fetch(url, {method: 'POST', headers: this.headers, body: JSON.stringify(app)})
229234
messageData = await response.json()
230235

231236
if (messageData.error === undefined) break
@@ -428,4 +433,12 @@ export class LuBuildCore {
428433
return aValue < bValue ? -1 : aValue > bValue ? 1 : 0
429434
})
430435
}
436+
437+
private getUserAgent() {
438+
const packageUserAgent = `${packageJSON.name}/${packageJSON.version}`
439+
const platformUserAgent = `(${os.arch()}-${os.type()}-${os.release()}; Node.js,Version=${process.version})`
440+
const userAgent = `${packageUserAgent} ${platformUserAgent}`
441+
442+
return userAgent
443+
}
431444
}

packages/lu/src/parser/qnabuild/serviceBase.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ServiceBase {
6666
commonHeaders(subscriptionKey) {
6767
return {
6868
'Content-Type': 'application/json',
69-
'User-Agent': this.getUserAgent(),
69+
'User-Agent': process.env['QNA_USER_AGENT'] || this.getUserAgent(),
7070
'Ocp-Apim-Subscription-Key': subscriptionKey
7171
}
7272
}

0 commit comments

Comments
 (0)