Promise based Advent22.api_get

This commit is contained in:
Jörn-Michael Miehe 2023-09-07 15:12:46 +00:00
parent a6e6d8eb19
commit 1866833d3d
3 changed files with 43 additions and 71 deletions

View file

@ -40,16 +40,13 @@ export default class extends Vue {
}; };
public mounted() { public mounted() {
this.$advent22.api_get_string("days/date", (date: string) => { this.$advent22
this.date = date; .api_get_string("days/date")
}); .then((date: string) => (this.date = date));
this.$advent22.api_get_number( this.$advent22
"days/visible_days", .api_get_number("days/visible_days")
(visible_days: number) => { .then((visible_days: number) => (this.visible_days = visible_days));
this.visible_days = visible_days;
},
);
} }
} }
</script> </script>

View file

@ -24,10 +24,10 @@ export default class extends Vue {
public on_click() { public on_click() {
this.$emit("doorClick"); this.$emit("doorClick");
this.$advent22.api_get_blob( this.$advent22
`days/image/${this.door.day}`, .api_get_blob(`days/image/${this.door.day}`)
(data) => this.$emit("doorSuccess", data), .then((data) => this.$emit("doorSuccess", data))
(_, reason) => { .catch(([reason]) => {
let msg = "Unbekannter Fehler, bitte wiederholen!"; let msg = "Unbekannter Fehler, bitte wiederholen!";
if ( if (
@ -39,8 +39,7 @@ export default class extends Vue {
} }
this.$emit("doorFailure", msg); this.$emit("doorFailure", msg);
}, });
);
} }
} }
</script> </script>

View file

@ -38,76 +38,52 @@ export class Advent22 {
this.api_auth = { username: username, password: password }; this.api_auth = { username: username, password: password };
} }
private fail(name: string, reason: unknown): void { private api_get<T>(endpoint: string): Promise<T>;
console.warn("Failed to query", name, "-", reason); private api_get<T>(endpoint: string, responseType: ResponseType): Promise<T>;
}
private api_get<T>( private api_get<T>(
endpoint: string, endpoint: string,
on_success: (data: T) => void,
on_failure: (name: string, reason: unknown) => void,
responseType: ResponseType = "json", responseType: ResponseType = "json",
) { ): Promise<T> {
const req_data = { const req_data = {
auth: this.api_auth, auth: this.api_auth,
responseType: responseType, responseType: responseType,
}; };
return new Promise<T>((resolve, reject) => {
this.axios this.axios
.get<T>(this.api_url(endpoint), req_data) .get<T>(this.api_url(endpoint), req_data)
.then((response) => on_success(response.data)) .then((response) => resolve(response.data))
.catch((reason) => on_failure(endpoint, reason)); .catch((reason) => {
console.error(`Failed to query ${endpoint}: ${reason}`);
reject([reason, endpoint]);
});
});
} }
public api_get_blob( public api_get_blob(endpoint: string): Promise<string> {
endpoint: string, return new Promise<string>((resolve, reject) => {
on_success: (data: string) => void, this.api_get<Blob>(endpoint, "blob")
on_failure: (name: string, reason: unknown) => void = this.fail, .then((data: Blob) => {
) {
this.api_get<Blob>(
endpoint,
(data: Blob) => {
const reader = new FileReader(); const reader = new FileReader();
reader.readAsDataURL(data); reader.readAsDataURL(data);
reader.onloadend = () => { reader.onloadend = () => {
if (typeof reader.result === "string") { if (typeof reader.result === "string") {
on_success(reader.result); resolve(reader.result);
} else { } else {
on_failure(endpoint, "failed data url"); reject([endpoint, "failed data url"]);
} }
}; };
}, })
on_failure, .catch(reject);
"blob", });
);
} }
public api_get_string( public api_get_string(endpoint: string): Promise<string> {
endpoint: string, return this.api_get<string>(endpoint);
on_success: (data: string) => void,
on_failure: (name: string, reason: unknown) => void = this.fail,
) {
this.api_get<string>(
endpoint,
(data: string) => {
on_success(data);
},
on_failure,
);
} }
public api_get_number( public api_get_number(endpoint: string): Promise<number> {
endpoint: string, return this.api_get<number>(endpoint);
on_success: (data: number) => void,
on_failure: (name: string, reason: unknown) => void = this.fail,
) {
this.api_get<number>(
endpoint,
(data: number) => {
on_success(data);
},
on_failure,
);
} }
} }