First part of API client for managing application instances

Jens Pelzetter 2020-08-04 18:08:37 +02:00
parent 4b3126c56a
commit 102b984b5f
4 changed files with 135 additions and 3 deletions

View File

@ -0,0 +1,132 @@
import {
ApiResponse,
LibreCcmApiClient,
ListView,
ApiError,
} from "@libreccm/ccm-apiclient-commons";
import { buildIdentifierParam } from "@libreccm/ccm-apiclient-commons";
import * as Constants from "../constants";
import {
CcmApplication,
buildCcmApplicationFromRecord,
} from "../entities/applications";
/**
* Client for managing application instances using the RESTful API of LibreCCM
*/
export class WebApiClient {
#apiClient: LibreCcmApiClient;
readonly #APPS_API_PREFIX = `${Constants.ADMIN_API_PREFIX}/applications`;
constructor(apiClient: LibreCcmApiClient) {
this.#apiClient = apiClient;
}
async getAllApplications(
limit: number,
offset: number
): Promise<ListView<CcmApplication>> {
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#APPS_API_PREFIX}`,
{
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 apps: CcmApplication[] = list.map((record) =>
buildCcmApplicationFromRecord(record)
);
return {
list: apps,
count: result.count as number,
limit: result.limit as number,
offset: result.offset as number,
};
} else {
throw `Failed to get application instances:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to get application instances: ${err}`;
}
}
async getApplication(
appIdentifier: string | number
): Promise<CcmApplication> {
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#APPS_API_PREFIX}/${buildIdentifierParam(
appIdentifier
)}`
);
if (response.ok) {
return buildCcmApplicationFromRecord(
(await response.json()) as Record<string, unknown>
);
} else {
throw `Failed to get application instance ${appIdentifier}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to get application instance ${appIdentifier}: ${err}`;
}
}
async createApplication(application: CcmApplication): Promise<void> {
try {
const response: ApiResponse = await this.#apiClient.post(
`${this.#APPS_API_PREFIX}`,
JSON.stringify(application)
);
if (response.ok) {
return;
} else {
throw `Failed to create new application instance of
type ${application.applicationType}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to create new application instance of
type ${application.applicationType}: ${err}`;
}
}
async updateApplication(
appIdentifier: string,
application: CcmApplication): Promise<void> {
try {
const response: ApiResponse = await this.#apiClient.put(
`${this.#APPS_API_PREFIX}/${buildIdentifierParam(appIdentifier)}`,
JSON.stringify(application)
);
if (response.ok) {
return;
} else {
throw `Failed to update pplication instance ${appIdentifier}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to update pplication instance ${appIdentifier}:
${application.applicationType}: ${err}`;
}
}
}

View File

@ -12,7 +12,7 @@ import { Site, buildSiteFromRecord } from "../entities/site";
import {
CcmApplicationId,
buildCcmApplicationIdFromRecord,
} from "../entities/web";
} from "../entities/applications";
/**
* Client for managing sites using the RESTful API

View File

@ -135,11 +135,11 @@ export class UsersApiClient {
if (response.ok) {
return;
} else {
throw `Failed to delete user ${name}:
throw `Failed to delete user ${userIdentifier}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to delete user ${name}: ${err}`;
throw `Failed to delete user ${userIdentifier}: ${err}`;
}
}