diff --git a/packages/app/main/src/services/luisApiService.spec.ts b/packages/app/main/src/services/luisApiService.spec.ts index 1fbea8645..a9cbeec7f 100644 --- a/packages/app/main/src/services/luisApiService.spec.ts +++ b/packages/app/main/src/services/luisApiService.spec.ts @@ -34,9 +34,6 @@ import '../fetchProxy'; import { LuisApi } from './luisApiService'; const mockArmToken = 'bm90aGluZw.eyJ1cG4iOiJnbGFzZ293QHNjb3RsYW5kLmNvbSJ9.7gjdshgfdsk98458205jfds9843fjds'; -const mockReq: RequestInit = { - headers: { Authorization: `Bearer ${mockArmToken}` }, -}; const mockResponses = [ 'hfdjg459846gjfdhgfdshjg', { error: { statusCode: 401, message: 'Oh Noes!' } }, @@ -129,12 +126,12 @@ describe('The LuisApiService class', () => { }); expect(mockArgsPassedToFetch[1]).toEqual({ - url: 'https://australiaeast.api.cognitive.microsoft.com/luis/api/v2.0/apps/', + url: 'https://api.eu.luis.ai/api/v2.0/bots/programmatickey', headers: jasmine.any(Object), }); expect(mockArgsPassedToFetch[2]).toEqual({ - url: 'https://westeurope.api.cognitive.microsoft.com/luis/api/v2.0/apps/', + url: 'https://api.au.luis.ai/api/v2.0/bots/programmatickey', headers: jasmine.any(Object), }); diff --git a/packages/app/main/src/services/luisApiService.ts b/packages/app/main/src/services/luisApiService.ts index ec74c5ffc..0a1862877 100644 --- a/packages/app/main/src/services/luisApiService.ts +++ b/packages/app/main/src/services/luisApiService.ts @@ -43,13 +43,17 @@ export class LuisApi { const req: RequestInit = { headers: { Authorization: `Bearer ${armToken}` }, }; - let authoringKey: string; + let authoringKeys: string[]; try { yield { label: 'Retrieving key from LUIS…', progress: 25 }; - const url = 'https://api.luis.ai/api/v2.0/bots/programmatickey'; - const authoringKeyResponse = yield fetch(url, req); - authoringKey = yield authoringKeyResponse.text(); - authoringKey = authoringKey.replace(/["]/g, ''); + const urls = [ + 'https://api.luis.ai/api/v2.0/bots/programmatickey', + 'https://api.eu.luis.ai/api/v2.0/bots/programmatickey', + 'https://api.au.luis.ai/api/v2.0/bots/programmatickey', + ]; + const requests = urls.map(url => fetch(url, req)); + const authoringKeyResponses: Response[] = yield Promise.all(requests); + authoringKeys = yield Promise.all(authoringKeyResponses.map(async response => await response.json())); } catch (e) { payload.code = ServiceCodes.AccountNotFound; return payload; @@ -61,7 +65,10 @@ export class LuisApi { const regions: LuisRegion[] = ['westus', 'westeurope', 'australiaeast']; let i = regions.length; while (i--) { - luisApiPromises.push(LuisApi.getApplicationsForRegion(regions[i], authoringKey)); + if (typeof authoringKeys[i] === 'object') { + continue; + } + luisApiPromises.push(LuisApi.getApplicationsForRegion(regions[i], authoringKeys[i])); } const results = yield Promise.all(luisApiPromises); // 3. @@ -73,16 +80,19 @@ export class LuisApi { // 4. // Mutate the list into an array of ILuisService[] payload.services = luisModels.map( - (luisModel: LuisModel): ILuisService => ({ - authoringKey, - appId: luisModel.id, - id: luisModel.id, - name: luisModel.name, - subscriptionKey: authoringKey, - type: luisModel.activeVersion === 'Dispatch' ? ServiceTypes.Dispatch : ServiceTypes.Luis, - version: luisModel.activeVersion, - region: luisModel.region, - }) + (luisModel: LuisModel): ILuisService => { + const authoringKey = authoringKeys[regions.indexOf(luisModel.region)]; + return { + authoringKey, + appId: luisModel.id, + id: luisModel.id, + name: luisModel.name, + subscriptionKey: authoringKey, + type: luisModel.activeVersion === 'Dispatch' ? ServiceTypes.Dispatch : ServiceTypes.Luis, + version: luisModel.activeVersion, + region: luisModel.region, + }; + } ) as ILuisService[]; return payload;