implement recovery from changed token
use exponential backoff for token retrieval
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type z from "zod";
|
||||
import z from "zod";
|
||||
import { logger } from "@/logger";
|
||||
import { NanoKVMClient } from "./nano-kvm-client";
|
||||
import {
|
||||
type GpioSchema,
|
||||
@@ -26,7 +27,20 @@ export class NanoKVMService {
|
||||
async getInfo(): Promise<z.output<typeof InfoSchema>> {
|
||||
const data = await (await this.client.fetch("/api/vm/info", "GET")).json();
|
||||
|
||||
return InfoSuccess.parse(data).data;
|
||||
const result = InfoSuccess.safeParse(data);
|
||||
|
||||
if (!result.success) {
|
||||
logger.error(data);
|
||||
logger.fatal(`Failed getting info: `, z.prettifyError(result.error));
|
||||
|
||||
if (data === "unauthorized") {
|
||||
await this.client.initAuth();
|
||||
}
|
||||
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
return result.data.data;
|
||||
}
|
||||
|
||||
async getGpio(): Promise<z.output<typeof GpioSchema>> {
|
||||
@@ -34,7 +48,20 @@ export class NanoKVMService {
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
return GpioSuccess.parse(data).data;
|
||||
const result = GpioSuccess.safeParse(data);
|
||||
|
||||
if (!result.success) {
|
||||
logger.error(data);
|
||||
logger.fatal(`Failed getting gpio: `, z.prettifyError(result.error));
|
||||
|
||||
if (data === "unauthorized") {
|
||||
await this.client.initAuth();
|
||||
}
|
||||
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
return result.data.data;
|
||||
}
|
||||
|
||||
async triggerPower(input: z.input<typeof TriggerPowerInput>) {
|
||||
|
||||
Reference in New Issue
Block a user