Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ready endpoint handling #360

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 42 additions & 34 deletions src/views/Main/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getDsHeaders } from "../../components/QueryBuilder/Operations/helpers";
import setDataSources from "../DataSources/store/setDataSources";
import { setShowDataSourceSetting } from "./setShowDataSourceSetting";


// updateDataSources:

export function updateDataSourcesWithUrl(
Expand Down Expand Up @@ -146,33 +147,35 @@ export async function checkLocalAPI(
datasource: any,
auth?: AuthParams,
isAuth?: boolean
) {
): Promise<boolean> {
let response: any = {};
let conf = getAxiosConf(datasource);
let isReady = false;
let opts: any = { ...conf };

if (auth?.username !== "" && isAuth) {
opts.auth = auth;
}

try {
let res = await getReadyResponse(url, opts, response);

response = res;
} catch (e: any) {
isReady = false;
} finally {
if (
response &&
response?.status === 200 &&
(response?.contentType === "application/json; charset=utf-8" ||
response?.contentLength === "0")
) {
isReady = true;
return new Promise(async (resolve, rej) => {
try {
let res = await getReadyResponse(url, opts, response);

response = res;
} catch (e: any) {
rej(false);
} finally {
if (
response &&
response?.status === 200 &&
(response?.contentType.includes("application/json") ||
response?.contentLength === "0")
) {
resolve(true);
} else {
rej(false);
}
}
}
return isReady;
});
}

export async function updateDataSourcesFromLocalUrl(
Expand All @@ -196,26 +199,31 @@ export async function updateDataSourcesFromLocalUrl(
}
}
}

let dsReady = false;

let isLocalReady = false;

if (logsDs?.url !== "") {

dsReady = await checkLocalAPI(logsDs.url, logsDs, auth, isBasicAuth); // add the auth in here
}
if (!dsReady) {
isLocalReady = await checkLocalAPI(location, logsDs);

if (isLocalReady && !dsReady) {
const dsCP = [...dataSources];
const prevDs = JSON.parse(JSON.stringify(dsCP));

const newDs = prevDs?.map((m: any) => ({
...m,
url: location,
}));
localStorage.setItem("dataSources", JSON.stringify(newDs));
dispatch(setDataSources(newDs));
} else if (!dsReady && !isLocalReady) {
navigate("datasources");

if (!dsReady) {
isLocalReady = await checkLocalAPI(location, logsDs);

if (isLocalReady && !dsReady) {
const dsCP = [...dataSources];
const prevDs = JSON.parse(JSON.stringify(dsCP));

const newDs = prevDs?.map((m: any) => ({
...m,
url: location,
}));
localStorage.setItem("dataSources", JSON.stringify(newDs));
dispatch(setDataSources(newDs));
} else if (!dsReady && !isLocalReady) {
navigate("datasources");
}
}
}
}
Loading