Skip to content

Commit

Permalink
Merge 69d9af0 into d29ccb1
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Wilaby committed Mar 7, 2019
2 parents d29ccb1 + 69d9af0 commit 7178039
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
7 changes: 2 additions & 5 deletions packages/app/main/src/services/luisApiService.spec.ts
Expand Up @@ -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!' } },
Expand Down Expand Up @@ -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),
});

Expand Down
42 changes: 26 additions & 16 deletions packages/app/main/src/services/luisApiService.ts
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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;
Expand Down

0 comments on commit 7178039

Please sign in to comment.