From 96256d60ffe39267a220b4b0754d5172af25ab62 Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Wed, 5 Aug 2020 16:41:38 +0200 Subject: [PATCH] Client for RESTful API for managing application instances --- .../typescript/clients/applications-api.ts | 132 +++++++++++++++++- 1 file changed, 128 insertions(+), 4 deletions(-) diff --git a/ccm-core-apiclient/src/main/typescript/clients/applications-api.ts b/ccm-core-apiclient/src/main/typescript/clients/applications-api.ts index f35b5a613..78eee4f44 100644 --- a/ccm-core-apiclient/src/main/typescript/clients/applications-api.ts +++ b/ccm-core-apiclient/src/main/typescript/clients/applications-api.ts @@ -13,6 +13,10 @@ import { CcmApplication, buildCcmApplicationFromRecord, } from "../entities/applications"; +import { + DomainOwnership, + buildDomainOwnershipFromRecord, +} from "../entities/categorization"; /** * Client for managing application instances using the RESTful API of LibreCCM @@ -110,11 +114,14 @@ export class WebApiClient { } async updateApplication( - appIdentifier: string, - application: CcmApplication): Promise { + appIdentifier: string | number, + application: CcmApplication + ): Promise { try { const response: ApiResponse = await this.#apiClient.put( - `${this.#APPS_API_PREFIX}/${buildIdentifierParam(appIdentifier)}`, + `${this.#APPS_API_PREFIX}/${buildIdentifierParam( + appIdentifier + )}`, JSON.stringify(application) ); @@ -125,8 +132,125 @@ export class WebApiClient { ${response.status} ${response.statusText}`; } } catch (err) { - throw `Failed to update pplication instance ${appIdentifier}: + throw `Failed to update application instance ${appIdentifier}: ${application.applicationType}: ${err}`; } } + + async deleteApplication(appIdentifier: string | number): Promise { + try { + const response: ApiResponse = await this.#apiClient.delete( + `${this.#APPS_API_PREFIX}/${buildIdentifierParam( + appIdentifier + )}` + ); + + if (response.ok) { + return; + } else { + throw `Failed to delete application + instance ${appIdentifier}: + ${response.status} ${response.statusText}`; + } + } catch (err) { + throw `Failed to delete application + instance ${appIdentifier}: ${err}`; + } + } + + async getDomains( + appIdentifier: string | number, + limit: number, + offset: number + ): Promise> { + try { + const response: ApiResponse = await this.#apiClient.get( + `${this.#APPS_API_PREFIX}/${buildIdentifierParam( + appIdentifier + )}/domains`, + { + limit: limit.toString(), + offset: offset.toString(), + } + ); + + if (response.ok) { + const result: Record< + string, + unknown + > = (await response.json()) as Record; + const list: Record[] = result.list as Record< + string, + unknown + >[]; + const domains: DomainOwnership[] = list.map((record) => + buildDomainOwnershipFromRecord(record) + ); + + return { + list: domains, + count: result.count as number, + limit: result.limit as number, + offset: result.limit as number, + }; + } else { + throw `Failed to get domains of application + instance ${appIdentifier}: + ${response.status} ${response.statusText}`; + } + } catch (err) { + throw `Failed to get domains of application + instance ${appIdentifier}: ${err}`; + } + } + + async addDomin( + appIdentifier: string | number, + domainIdenfifier: string | number + ): Promise { + try { + const appParam = buildIdentifierParam(appIdentifier); + const domainParam = buildIdentifierParam(domainIdenfifier); + + const response: ApiResponse = await this.#apiClient.put( + `${this.#APPS_API_PREFIX}/${appParam}/domains/${domainParam}` + ); + + if (response.ok) { + return; + } else { + throw `Failed to add domain ${domainIdenfifier} to + application ${appIdentifier}: + ${response.status} ${response.statusText}`; + } + } catch (err) { + throw `Failed to add domain ${domainIdenfifier} to + application ${appIdentifier}: ${err}`; + } + } + + async removeDomain( + appIdentifier: string | number, + domainIdenfifier: string | number + ): Promise { + try { + const appParam = buildIdentifierParam(appIdentifier); + const domainParam = buildIdentifierParam(domainIdenfifier); + + const response: ApiResponse = await this.#apiClient.delete( + `${this.#APPS_API_PREFIX}/${appParam}/domains/${domainParam}` + ); + + if (response.ok) { + return; + } else { + throw `Failed to remove domain ${domainIdenfifier} from + application ${appIdentifier}: + ${response.status} ${response.statusText}`; + } + } catch (err) { + throw `Failed to remove domain ${domainIdenfifier} from + application ${appIdentifier}: ${err}`; + } + } }