From c8f1e568d85f84454b769fe218e68c19c597519f Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Tue, 28 Jul 2020 20:24:43 +0200 Subject: [PATCH] Client for RESTful for managing the configuration of LibreCCM Former-commit-id: 9b044a7e4a94722c66000e6e2b1ef9413c4c83ea --- .../typescript/clients/configuration-api.ts | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/ccm-core-apiclient/src/main/typescript/clients/configuration-api.ts b/ccm-core-apiclient/src/main/typescript/clients/configuration-api.ts index 4c3268ecd..7e6a314e7 100644 --- a/ccm-core-apiclient/src/main/typescript/clients/configuration-api.ts +++ b/ccm-core-apiclient/src/main/typescript/clients/configuration-api.ts @@ -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 { + 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; + + 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> { + try { + const response: ApiResponse = await this.#apiClient.get( + `${this.#CONFIGURATION_API_PREFIX}/${confName}/settings` + ); + + if (response.ok) { + return (await response.json()) as Record; + } 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 { + 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 { + 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}`; + } + } }