Client for RESTful for Im/Export
parent
9b044a7e4a
commit
1b8f00175c
|
|
@ -27,7 +27,7 @@ export interface LibreCcmApiClient {
|
|||
*/
|
||||
post(
|
||||
endpoint: string,
|
||||
body: RequestBody,
|
||||
body?: RequestBody,
|
||||
searchParams?: Record<string, string>
|
||||
): Promise<ApiResponse>;
|
||||
/**
|
||||
|
|
@ -277,7 +277,7 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
|
|||
|
||||
async post(
|
||||
endpoint: string,
|
||||
body: RequestBody,
|
||||
body?: RequestBody,
|
||||
searchParams?: Record<string, string>
|
||||
): Promise<ApiResponse> {
|
||||
const url = buildUrl(this.#baseUrl, endpoint, searchParams);
|
||||
|
|
@ -562,7 +562,7 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
|
|||
|
||||
post(
|
||||
endpoint: string,
|
||||
body: ArrayBuffer,
|
||||
body?: ArrayBuffer,
|
||||
searchParams?: Record<string, string>
|
||||
): Promise<ApiResponse> {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
import {
|
||||
ApiResponse,
|
||||
LibreCcmApiClient,
|
||||
ListView,
|
||||
} from "@libreccm/ccm-apiclient-commons";
|
||||
|
||||
import {
|
||||
ConfigurationInfo,
|
||||
SettingInfo,
|
||||
buildConfigurationInfoFromRecord,
|
||||
} from "../entities/configuration";
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,85 @@
|
|||
import {
|
||||
ApiResponse,
|
||||
LibreCcmApiClient,
|
||||
} from "@libreccm/ccm-apiclient-commons";
|
||||
|
||||
import {
|
||||
ImportManifest,
|
||||
buildImportManifestFromRecord,
|
||||
} from "../entities/imexport";
|
||||
|
||||
import * as Constants from "../constants";
|
||||
|
||||
export class ImExportClient {
|
||||
#apiClient: LibreCcmApiClient;
|
||||
|
||||
readonly #IMEXPORT_API_PREFIX = `${Constants.ADMIN_API_PREFIX}/imexport`;
|
||||
|
||||
constructor(apiClient: LibreCcmApiClient) {
|
||||
this.#apiClient = apiClient;
|
||||
}
|
||||
|
||||
async listImports(): Promise<ImportManifest[]> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.get(
|
||||
`${this.#IMEXPORT_API_PREFIX}/imports`
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
const records: Record<
|
||||
string,
|
||||
unknown
|
||||
>[] = (await response.json()) as Record<string, unknown>[];
|
||||
|
||||
return records.map((record) =>
|
||||
buildImportManifestFromRecord(record)
|
||||
);
|
||||
} else {
|
||||
throw `Failed to list available import archives:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to list available import archives: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async importArchive(importName: string): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.post(
|
||||
`${this.#IMEXPORT_API_PREFIX}/imports/${importName}`
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to import archive ${importName}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to import archive ${importName}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async exportEntities(
|
||||
exportName: string,
|
||||
entityIds: string[]
|
||||
): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.post(
|
||||
`${this.#IMEXPORT_API_PREFIX}/exports/${exportName}`,
|
||||
JSON.stringify(entityIds)
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to export entities
|
||||
${entityIds.join(", ")} as ${exportName}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to export entities
|
||||
${entityIds.join(", ")} as ${exportName}: ${err}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
import { assertProperties } from "@libreccm/ccm-apiclient-commons";
|
||||
|
||||
export interface ImportManifest {
|
||||
created: Date;
|
||||
onServer: string;
|
||||
types: string[];
|
||||
}
|
||||
|
||||
export function buildImportManifestFromRecord(
|
||||
record: Record<string, unknown>
|
||||
): ImportManifest {
|
||||
assertProperties(record, ["created", "onServer", "types"]);
|
||||
|
||||
return {
|
||||
created: new Date(record.created as string),
|
||||
onServer: record.onServer as string,
|
||||
types: record.types as string[]
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue