api_get_object_multi method
This commit is contained in:
parent
a7eed28bf3
commit
ff6fbe218d
2 changed files with 48 additions and 25 deletions
|
@ -133,22 +133,19 @@ export default class App extends Vue {
|
||||||
|
|
||||||
// Update Calendar Aggregates
|
// Update Calendar Aggregates
|
||||||
this.$ovdashboard.api_get_list("aggregate/list", (names) => {
|
this.$ovdashboard.api_get_list("aggregate/list", (names) => {
|
||||||
let promises = names.map((name: string) =>
|
this.$ovdashboard.api_get_object_multi<EventData[]>(
|
||||||
this.$ovdashboard.api_get_prepare<EventData[]>("aggregate/get/" + name)
|
names.map((name) => "aggregate/get/" + name),
|
||||||
);
|
(calendars) => {
|
||||||
|
|
||||||
Promise.all(promises)
|
|
||||||
.then((responses) => {
|
|
||||||
this.calendar_data = [];
|
this.calendar_data = [];
|
||||||
|
|
||||||
for (let i = 0; i < names.length; i++) {
|
for (let i = 0; i < names.length; i++) {
|
||||||
this.calendar_data.push({
|
this.calendar_data.push({
|
||||||
title: names[i],
|
title: names[i],
|
||||||
events: responses[i].data,
|
events: calendars[i],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
.catch(this.$ovdashboard.fail("Calendar Aggregates"));
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Update Calendar Config
|
// Update Calendar Config
|
||||||
|
|
|
@ -45,16 +45,16 @@ export class OVDashboardPlugin {
|
||||||
return this.api_baseurl + "/" + endpoint;
|
return this.api_baseurl + "/" + endpoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
public fail(name: string): (reason: unknown) => void {
|
private fail(name: string): (reason: unknown) => void {
|
||||||
return (reason: unknown) =>
|
return (reason: unknown) =>
|
||||||
console.warn("Failed to query", name, "-", reason);
|
console.warn("Failed to query", name, "-", reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
public api_get_prepare<T>(endpoint: string): AxiosPromise<T> {
|
private api_get_prepare<T>(endpoint: string): AxiosPromise<T> {
|
||||||
return this.axios.get<T>(this.api_url(endpoint));
|
return this.axios.get<T>(this.api_url(endpoint));
|
||||||
}
|
}
|
||||||
|
|
||||||
public api_get<T>(
|
private api_get<T>(
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
on_success: (data: T) => void
|
on_success: (data: T) => void
|
||||||
): void {
|
): void {
|
||||||
|
@ -77,17 +77,19 @@ export class OVDashboardPlugin {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private check_array<T>(value: unknown): value is T[] {
|
||||||
|
if (!Array.isArray(value)) return false;
|
||||||
|
if (!value.every((entry) => typeof entry === "string")) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public api_get_list(
|
public api_get_list(
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
on_success: (data: string[]) => void
|
on_success: (data: string[]) => void
|
||||||
): void {
|
): void {
|
||||||
this.api_get<string[]>(endpoint, (data) => {
|
this.api_get(endpoint, (data) => {
|
||||||
if (!Array.isArray(data)) {
|
if (!this.check_array<string>(data)) {
|
||||||
console.error("Endpoint", endpoint, "did not return an Array:", data);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!data.every((value) => typeof value === "string")) {
|
|
||||||
console.error("Endpoint", endpoint, "did not return a string[]:", data);
|
console.error("Endpoint", endpoint, "did not return a string[]:", data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -96,24 +98,48 @@ export class OVDashboardPlugin {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private check_object(value: unknown): value is object {
|
||||||
|
if (typeof value !== "object") return false;
|
||||||
|
if (value === null) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public api_get_object<Type extends object>(
|
public api_get_object<Type extends object>(
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
on_success: (data: Type) => void
|
on_success: (data: Type) => void
|
||||||
): void {
|
): void {
|
||||||
this.api_get<Type>(endpoint, (data) => {
|
this.api_get<Type>(endpoint, (data) => {
|
||||||
if (typeof data !== "object") {
|
if (!this.check_object(data)) {
|
||||||
console.error("Endpoint", endpoint, "did not return an Object:", data);
|
console.error("Endpoint", endpoint, "did not return an Object:", data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data === null) {
|
|
||||||
console.error("Endpoint", endpoint, "returned NULL!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
on_success(data);
|
on_success(data);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public api_get_object_multi<Type extends object>(
|
||||||
|
endpoints: string[],
|
||||||
|
on_success: (data: Type[]) => void
|
||||||
|
): void {
|
||||||
|
const promises = endpoints.map((endpoint) => this.api_get_prepare<Type>(endpoint));
|
||||||
|
|
||||||
|
Promise.all(promises)
|
||||||
|
.then((responses) => {
|
||||||
|
const data = responses.map((response) => response.data);
|
||||||
|
|
||||||
|
for (let i = 0; i < data.length; i++) {
|
||||||
|
if (!this.check_object(data[i])) {
|
||||||
|
console.error("Response data was not an Object:", data[i]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
on_success(data);
|
||||||
|
})
|
||||||
|
.catch(this.fail(endpoints.join(", ")));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new OVDashboardPlugin();
|
export default new OVDashboardPlugin();
|
||||||
|
|
Loading…
Reference in a new issue