Client for RESTful API for managing application instances

Former-commit-id: 96256d60ff
restapi
Jens Pelzetter 2020-08-05 16:41:38 +02:00
parent d30cbc5ec7
commit ac27d5ac61
1 changed files with 128 additions and 4 deletions

View File

@ -13,6 +13,10 @@ import {
CcmApplication, CcmApplication,
buildCcmApplicationFromRecord, buildCcmApplicationFromRecord,
} from "../entities/applications"; } from "../entities/applications";
import {
DomainOwnership,
buildDomainOwnershipFromRecord,
} from "../entities/categorization";
/** /**
* Client for managing application instances using the RESTful API of LibreCCM * Client for managing application instances using the RESTful API of LibreCCM
@ -110,11 +114,14 @@ export class WebApiClient {
} }
async updateApplication( async updateApplication(
appIdentifier: string, appIdentifier: string | number,
application: CcmApplication): Promise<void> { application: CcmApplication
): Promise<void> {
try { try {
const response: ApiResponse = await this.#apiClient.put( const response: ApiResponse = await this.#apiClient.put(
`${this.#APPS_API_PREFIX}/${buildIdentifierParam(appIdentifier)}`, `${this.#APPS_API_PREFIX}/${buildIdentifierParam(
appIdentifier
)}`,
JSON.stringify(application) JSON.stringify(application)
); );
@ -125,8 +132,125 @@ export class WebApiClient {
${response.status} ${response.statusText}`; ${response.status} ${response.statusText}`;
} }
} catch (err) { } catch (err) {
throw `Failed to update pplication instance ${appIdentifier}: throw `Failed to update application instance ${appIdentifier}:
${application.applicationType}: ${err}`; ${application.applicationType}: ${err}`;
} }
} }
async deleteApplication(appIdentifier: string | number): Promise<void> {
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<ListView<DomainOwnership>> {
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<string, unknown>;
const list: Record<string, unknown>[] = 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<void> {
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<void> {
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}`;
}
}
} }