Client for RESTful for managing the configuration of LibreCCM

Jens Pelzetter 2020-07-28 20:24:43 +02:00
parent f63b048826
commit 9b044a7e4a
1 changed files with 91 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import {
} from "../entities/configuration";
import * as Constants from "../constants";
import { RequestBody } from "@libreccm/ccm-apiclient-commons/dist/ApiClient";
export class ConfigurationApiClient {
#apiClient: LibreCcmApiClient;
@ -44,4 +45,94 @@ export class ConfigurationApiClient {
throw `Failed to retrieve configuration info: ${err}`;
}
}
async getConfiguration(confName: string): Promise<ConfigurationInfo> {
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#CONFIGURATION_API_PREFIX}/${confName}`
);
if (response.ok) {
const result: Record<
string,
unknown
> = (await response.json()) as Record<string, unknown>;
return buildConfigurationInfoFromRecord(result);
} else {
throw `Failed to get configuration info for
configuration ${confName}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to get configuration info for
configuration ${confName}: ${err}`;
}
}
async getSettings(confName: string): Promise<Record<string, unknown>> {
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#CONFIGURATION_API_PREFIX}/${confName}/settings`
);
if (response.ok) {
return (await response.json()) as Record<string, unknown>;
} else {
throw `Failed to get settings of
configuration ${confName}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to get settings of
configuration ${confName}: ${err}`;
}
}
async getSetting(confName: string, setting: string): Promise<unknown> {
try {
const response: ApiResponse = await this.#apiClient.get(
`${
this.#CONFIGURATION_API_PREFIX
}/${confName}/settings/${setting}`
);
if (response.ok) {
return await response.json();
} else {
throw `Failed to get setting ${setting} of
configuration ${confName}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to get setting ${setting} of
configuration ${confName}: ${err}`;
}
}
async updateSetting(
confName: string,
setting: string,
value: unknown
): Promise<void> {
try {
const response: ApiResponse = await this.#apiClient.put(
`${
this.#CONFIGURATION_API_PREFIX
}/${confName}/settings/${setting}`,
value as RequestBody
);
if (response.ok) {
return;
} else {
throw `Failed to update setting ${setting} of
configuration ${confName}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to update setting ${setting} of
configuration ${confName}: ${err}`;
}
}
}