mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-11-23 08:13:01 +00:00
Promise based Advent22.api_get
This commit is contained in:
parent
a6e6d8eb19
commit
1866833d3d
3 changed files with 43 additions and 71 deletions
|
@ -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>
|
||||||
|
|
|
@ -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>
|
||||||
|
|
|
@ -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,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.axios
|
return new Promise<T>((resolve, reject) => {
|
||||||
.get<T>(this.api_url(endpoint), req_data)
|
this.axios
|
||||||
.then((response) => on_success(response.data))
|
.get<T>(this.api_url(endpoint), req_data)
|
||||||
.catch((reason) => on_failure(endpoint, reason));
|
.then((response) => resolve(response.data))
|
||||||
|
.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) => {
|
||||||
) {
|
const reader = new FileReader();
|
||||||
this.api_get<Blob>(
|
reader.readAsDataURL(data);
|
||||||
endpoint,
|
reader.onloadend = () => {
|
||||||
(data: Blob) => {
|
if (typeof reader.result === "string") {
|
||||||
const reader = new FileReader();
|
resolve(reader.result);
|
||||||
reader.readAsDataURL(data);
|
} else {
|
||||||
reader.onloadend = () => {
|
reject([endpoint, "failed data url"]);
|
||||||
if (typeof reader.result === "string") {
|
}
|
||||||
on_success(reader.result);
|
};
|
||||||
} else {
|
})
|
||||||
on_failure(endpoint, "failed data url");
|
.catch(reject);
|
||||||
}
|
});
|
||||||
};
|
|
||||||
},
|
|
||||||
on_failure,
|
|
||||||
"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,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue