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

View file

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