query_simple -> get

This commit is contained in:
Jörn-Michael Miehe 2022-09-16 22:02:23 +00:00
parent 208af9d55b
commit 800cc87d51
2 changed files with 58 additions and 46 deletions

View file

@ -42,6 +42,7 @@
<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { EventData } from "./components/calendar/EventModel";
import { CalendarData } from "./components/calendar/CalendarModel";
import TitleBar from "./components/title/TitleBar.vue";
@ -94,21 +95,18 @@ export default class App extends Vue {
below: string;
};
this.$ovdashboard.api_query_simple_object<LogoConfig>(
"misc/config/logo",
(data) => {
this.logo_above = data.above;
this.logo_below = data.below;
}
);
this.$ovdashboard.api_get_object<LogoConfig>("misc/config/logo", (data) => {
this.logo_above = data.above;
this.logo_below = data.below;
});
// Update Title
this.$ovdashboard.api_query_simple_string("text/get/html/title", (data) => {
this.$ovdashboard.api_get_string("text/get/html/title", (data) => {
this.title_html = data;
});
// Update Images
this.$ovdashboard.api_query_simple_list("image/list", (data) => {
this.$ovdashboard.api_get_list("image/list", (data) => {
this.image_urls = data.map((name: string) =>
this.$ovdashboard.api_url("image/get/" + name)
);
@ -121,25 +119,22 @@ export default class App extends Vue {
speed: number;
};
this.$ovdashboard.api_query_simple_object<ImageConfig>(
"image/config",
(data) => {
this.image_height = data.height;
this.image_contain = data.contain;
this.image_speed = data.speed;
}
);
this.$ovdashboard.api_get_object<ImageConfig>("image/config", (data) => {
this.image_height = data.height;
this.image_contain = data.contain;
this.image_speed = data.speed;
});
// Update Message
this.$ovdashboard.api_query_simple_string(
this.$ovdashboard.api_get_string(
"text/get/html/message",
(data) => (this.message_html = data)
);
// Update Calendar Aggregates
this.$ovdashboard.api_query_simple_list("aggregate/list", (data) => {
this.$ovdashboard.api_get_list("aggregate/list", (data) => {
let promises = data.map((name: string) =>
this.$axios.get(this.$ovdashboard.api_url("aggregate/get/" + name))
this.$ovdashboard.api_get_prepare<EventData[]>("aggregate/get/" + name)
);
Promise.all(promises)
@ -161,7 +156,7 @@ export default class App extends Vue {
speed: number;
};
this.$ovdashboard.api_query_simple_object<CalendarConfig>(
this.$ovdashboard.api_get_object<CalendarConfig>(
"calendar/config",
(data) => {
this.calendar_speed = data.speed;
@ -174,7 +169,7 @@ export default class App extends Vue {
name: string;
};
this.$ovdashboard.api_query_simple_object<ServerConfig>(
this.$ovdashboard.api_get_object<ServerConfig>(
"misc/config/server",
(data) => {
this.server_host = data.host;
@ -183,17 +178,17 @@ export default class App extends Vue {
);
// Update Version
this.$ovdashboard.api_query_simple_string("misc/version", (data) => {
this.$ovdashboard.api_get_string("misc/version", (data) => {
this.dashboard_version = data;
});
// Update IP
this.$ovdashboard.api_query_simple_string("misc/lanip", (data) => {
this.$ovdashboard.api_get_string("misc/lanip", (data) => {
this.dashboard_ip = data;
});
// Update Ticker
this.$ovdashboard.api_query_simple_string("ticker/html", (data) => {
this.$ovdashboard.api_get_string("ticker/html", (data) => {
this.ticker_html = data;
});
@ -202,12 +197,9 @@ export default class App extends Vue {
color: string;
};
this.$ovdashboard.api_query_simple_object<TickerConfig>(
"ticker/config",
(data) => {
this.ticker_color = data.color;
}
);
this.$ovdashboard.api_get_object<TickerConfig>("ticker/config", (data) => {
this.ticker_color = data.color;
});
}
public created(): void {

View file

@ -1,4 +1,4 @@
import axios, { AxiosInstance } from 'axios';
import axios, { AxiosInstance, AxiosPromise } from 'axios';
import Vue from 'vue';
export class OVDashboardPlugin {
@ -50,46 +50,66 @@ export class OVDashboardPlugin {
console.warn("Failed to query", name, "-", reason);
}
public api_query_simple(
public api_get_prepare<T>(endpoint: string): AxiosPromise<T> {
return this.axios.get<T>(this.api_url(endpoint));
}
public api_get_generic(
endpoint: string,
on_success: (data: unknown) => void
): void {
this.axios
.get(this.api_url(endpoint))
this.api_get_prepare(endpoint)
.then((response) => on_success(response.data))
.catch(this.fail(endpoint));
}
public api_query_simple_string(
public api_get_string(
endpoint: string,
on_success: (data: string) => void
): void {
this.api_query_simple(endpoint, (data) => {
if (typeof data !== "string") return;
this.api_get_generic(endpoint, (data) => {
if (typeof data !== "string") {
console.error("Endpoint", endpoint, "did not return a string:", data);
return;
}
on_success(data);
});
}
public api_query_simple_list(
public api_get_list(
endpoint: string,
on_success: (data: string[]) => void
): void {
this.api_query_simple(endpoint, (data) => {
if (!Array.isArray(data)) return;
if (!data.every((value) => typeof value === "string")) return;
this.api_get_generic(endpoint, (data) => {
if (!Array.isArray(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);
return;
}
on_success(data);
});
}
public api_query_simple_object<Type extends object>(
public api_get_object<Type extends object>(
endpoint: string,
on_success: (data: Type) => void
): void {
this.api_query_simple(endpoint, (data) => {
if (typeof data !== "object") return;
if (data === null) return;
this.api_get_generic(endpoint, (data) => {
if (typeof data !== "object") {
console.error("Endpoint", endpoint, "did not return an Object:", data);
return;
}
if (data === null) {
console.error("Endpoint", endpoint, "returned NULL!");
return;
}
on_success(data as Type);
});