Skip to content

Commit 835d940

Browse files
fix some issues and improve the code
1 parent 09dd613 commit 835d940

File tree

1 file changed

+59
-78
lines changed

1 file changed

+59
-78
lines changed

src/classes/insightsConnection.ts

Lines changed: 59 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ import { ScratchpadRequestBody } from "../models/scratchpad";
4545
import { StructuredTextResults } from "../models/queryResult";
4646
import { UDARequestBody } from "../models/uda";
4747

48+
const customHeadersOctet = {
49+
Accept: "application/octet-stream",
50+
"Content-Type": "application/json",
51+
};
52+
const customHeadersJson = {
53+
"Content-Type": "application/json",
54+
Accept: "application/json",
55+
json: true,
56+
};
57+
4858
export class InsightsConnection {
4959
public connected: boolean;
5060
public connLabel: string;
@@ -179,10 +189,9 @@ export class InsightsConnection {
179189
ext.insightsServiceGatewayUrls.meta,
180190
this.node.details.server,
181191
);
182-
183192
const options = await this.getOptions();
184193

185-
if (options === undefined) {
194+
if (!options) {
186195
return undefined;
187196
}
188197

@@ -204,7 +213,6 @@ export class InsightsConnection {
204213
ext.insightsAuthUrls.apiConfigUrl,
205214
this.node.details.server,
206215
);
207-
208216
const options = await this.getOptions(
209217
true,
210218
{},
@@ -215,8 +223,8 @@ export class InsightsConnection {
215223
if (options === undefined) {
216224
return undefined;
217225
}
218-
219226
const configResponse = await axios(options);
227+
220228
this.apiConfig = configResponse.data;
221229
}
222230
}
@@ -227,7 +235,6 @@ export class InsightsConnection {
227235
ext.insightsAuthUrls.configURL,
228236
this.node.details.server,
229237
);
230-
231238
const options = await this.getOptions(
232239
false,
233240
{},
@@ -240,6 +247,7 @@ export class InsightsConnection {
240247
}
241248

242249
const configResponse = await axios(options);
250+
243251
this.config = configResponse.data;
244252
this.getInsightsVersion();
245253
this.defineEndpoints();
@@ -317,17 +325,18 @@ export class InsightsConnection {
317325
targetUrl,
318326
this.node.details.server,
319327
).toString();
320-
const customHeaders = {
321-
"Content-Type": "application/json",
322-
Accept: "application/json",
323-
};
324-
const options = await this.getOptions(false, customHeaders, "POST", body);
325-
if (options === undefined || !options.headers) {
328+
const options = await this.getOptions(
329+
false,
330+
customHeadersOctet,
331+
"POST",
332+
requestUrl.toString(),
333+
body,
334+
);
335+
336+
if (!options) {
326337
return undefined;
327338
}
328-
329339
options.responseType = "arraybuffer";
330-
331340
const results = await window.withProgress(
332341
{
333342
location: ProgressLocation.Notification,
@@ -411,19 +420,14 @@ export class InsightsConnection {
411420
}
412421

413422
const scratchpadURL = new url.URL(coreUrl!, this.node.details.server);
414-
415-
const customHeaders = {
416-
json: true,
417-
};
418-
419423
const body = {
420424
output: variableName,
421425
isTableView: false,
422426
params: queryParams,
423427
};
424428
const options = await this.getOptions(
425429
true,
426-
customHeaders,
430+
customHeadersJson,
427431
"POST",
428432
scratchpadURL.toString(),
429433
body,
@@ -497,27 +501,20 @@ export class InsightsConnection {
497501
this.connEndpoints.serviceGateway.udaBase +
498502
udaReqBody.name.split(".").slice(1).join("/");
499503
const udaURL = new url.URL(udaEndpoint, this.node.details.server);
500-
const token = await this.generateToken();
501-
if (token === undefined) {
502-
return undefined;
503-
}
504+
const options = await this.getOptions(
505+
false,
506+
customHeadersOctet,
507+
"POST",
508+
udaURL.toString(),
509+
udaReqBody.params,
510+
);
504511

505-
const headers = {
506-
Authorization: `Bearer ${token.accessToken}`,
507-
Accept: "application/octet-stream",
508-
"Content-Type": "application/json",
509-
};
512+
if (!options) {
513+
return;
514+
}
510515

511-
const body = udaReqBody.params;
516+
options.responseType = "arraybuffer";
512517

513-
const options: AxiosRequestConfig = {
514-
method: "post",
515-
url: udaURL.toString(),
516-
data: body,
517-
headers: headers,
518-
responseType: "arraybuffer",
519-
httpsAgent: getHttpsAgent(this.node.details.insecure),
520-
};
521518
const results = await window.withProgress(
522519
{
523520
location: ProgressLocation.Notification,
@@ -571,21 +568,17 @@ export class InsightsConnection {
571568
this.connEndpoints.scratchpad.importUDA,
572569
this.node.details.server,
573570
);
574-
const token = await this.generateToken();
575-
if (token === undefined) {
576-
return undefined;
577-
}
578-
const username = jwtDecode<JwtUser>(token.accessToken);
579-
if (username === undefined || username.preferred_username === "") {
580-
invalidUsernameJWT(this.connLabel);
571+
const options = await this.getOptions(
572+
true,
573+
customHeadersJson,
574+
"POST",
575+
udaURL.toString(),
576+
udaReqBody,
577+
);
578+
if (!options) {
579+
return;
581580
}
582581

583-
const headers = {
584-
Authorization: `Bearer ${token.accessToken}`,
585-
Username: username.preferred_username,
586-
"Content-Type": "application/json",
587-
};
588-
589582
const udaResponse = await window.withProgress(
590583
{
591584
location: ProgressLocation.Notification,
@@ -596,27 +589,22 @@ export class InsightsConnection {
596589
kdbOutputLog(`User cancelled the UDA execution.`, "WARNING");
597590
});
598591

599-
const udaRes = await axios
600-
.post(udaURL.toString(), udaReqBody, {
601-
headers,
602-
httpsAgent: getHttpsAgent(this.node.details.insecure),
603-
})
604-
.then((response: any) => {
605-
if (response.data.error) {
606-
return response.data;
607-
} else {
608-
kdbOutputLog(`[UDA] Status: ${response.status}`, "INFO");
609-
if (!response.data.error) {
610-
if (isTableView) {
611-
response.data = JSON.parse(
612-
response.data.data,
613-
) as StructuredTextResults;
614-
}
615-
return response.data;
592+
const udaRes = await axios(options).then((response: any) => {
593+
if (response.data.error) {
594+
return response.data;
595+
} else {
596+
kdbOutputLog(`[UDA] Status: ${response.status}`, "INFO");
597+
if (!response.data.error) {
598+
if (isTableView) {
599+
response.data = JSON.parse(
600+
response.data.data,
601+
) as StructuredTextResults;
616602
}
617603
return response.data;
618604
}
619-
});
605+
return response.data;
606+
}
607+
});
620608
return udaRes;
621609
},
622610
);
@@ -639,9 +627,6 @@ export class InsightsConnection {
639627
this.connEndpoints.scratchpad.scratchpad,
640628
this.node.details.server,
641629
);
642-
const customHeaders = {
643-
"Content-Type": "application/json",
644-
};
645630
const body: ScratchpadRequestBody = {
646631
expression: query,
647632
language: !isPython ? "q" : "python",
@@ -661,7 +646,7 @@ export class InsightsConnection {
661646

662647
const options = await this.getOptions(
663648
true,
664-
customHeaders,
649+
customHeadersJson,
665650
"POST",
666651
scratchpadURL.toString(),
667652
body,
@@ -738,19 +723,15 @@ export class InsightsConnection {
738723
this.connEndpoints.scratchpad.reset,
739724
this.node.details.server,
740725
);
741-
const customHeaders = {
742-
json: true,
743-
};
744-
745726
const options = await this.getOptions(
746727
true,
747-
customHeaders,
728+
customHeadersJson,
748729
"POST",
749730
scratchpadURL.toString(),
750731
null,
751732
);
752733

753-
if (options === undefined) {
734+
if (!options) {
754735
return;
755736
}
756737

0 commit comments

Comments
 (0)