parent
77f7a76cd6
commit
78d9f512f6
|
|
@ -1,191 +1,3 @@
|
|||
import {
|
||||
ApiResponse,
|
||||
LibreCcmApiClient,
|
||||
} from "@libreccm/ccm-apiclient-commons";
|
||||
import { CategorizationApiClient } from "./clients/categorization-api";
|
||||
|
||||
import { Category, buildCategoryFromRecord } from "./entities/categorization";
|
||||
import * as Constants from "./constants";
|
||||
|
||||
export class CategorizationApiClient {
|
||||
#apiClient: LibreCcmApiClient;
|
||||
|
||||
readonly #CATEGORIES_API_PREFIX = `${Constants.ADMIN_API_PREFIX}/categories`;
|
||||
|
||||
constructor(apiClient: LibreCcmApiClient) {
|
||||
this.#apiClient = apiClient;
|
||||
}
|
||||
|
||||
async getCategoryByDomainAndPath(
|
||||
domain: string,
|
||||
path: string
|
||||
): Promise<Category> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.get(
|
||||
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return buildCategoryFromRecord(await response.json());
|
||||
} else {
|
||||
throw `Failed to get category ${path} of domain ${domain}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to get category ${path} of domain ${domain}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async getCategoryById(categoryId: number): Promise<Category> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.get(
|
||||
`${this.#CATEGORIES_API_PREFIX}/categories/ID-${categoryId}`
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return buildCategoryFromRecord(await response.json());
|
||||
} else {
|
||||
throw `Failed to get category with ID ${categoryId}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to get category with Id ${categoryId}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async getCategoryByUuid(uuid: string): Promise<Category> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.get(
|
||||
`${this.#CATEGORIES_API_PREFIX}/categories/UUID-${uuid}`
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return buildCategoryFromRecord(await response.json());
|
||||
} else {
|
||||
throw `Failed to get category with UUID ${uuid}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to get category with UUID ${uuid}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async updateCategoryByDomainAndPath(
|
||||
domain: string,
|
||||
path: string,
|
||||
category: Category
|
||||
): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.put(
|
||||
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`,
|
||||
JSON.stringify(category)
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to update category ${path} of domain ${domain}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to update category ${path} of domain ${domain}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async updateCategoryById(
|
||||
categoryId: number,
|
||||
category: Category
|
||||
): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.put(
|
||||
`${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`,
|
||||
JSON.stringify(category)
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to update category with ID ${categoryId}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to update category ${categoryId}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async updateCategoryByUuid(
|
||||
uuid: string,
|
||||
category: Category
|
||||
): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.put(
|
||||
`${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`,
|
||||
JSON.stringify(category)
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to update category with UUID ${uuid}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to update category with UUID ${uuid}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteCategoryByDomainAndPath(
|
||||
domain: string,
|
||||
path: string
|
||||
): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.delete(
|
||||
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to update category ${path} of domain ${domain}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch(err) {
|
||||
throw `Failed to delete category ${path} of domain ${domain}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteCategoryById(categoryId: number): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.delete(
|
||||
`${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to delete category with ID ${categoryId}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to delete category ${categoryId}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteCategoryByUuid(
|
||||
uuid: string,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.delete(
|
||||
`${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`,
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to delete category with UUID ${uuid}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to delete category with UUID ${uuid}: ${err}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
export { CategorizationApiClient };
|
||||
|
|
|
|||
|
|
@ -0,0 +1,191 @@
|
|||
import {
|
||||
ApiResponse,
|
||||
LibreCcmApiClient,
|
||||
} from "@libreccm/ccm-apiclient-commons";
|
||||
|
||||
import { Category, buildCategoryFromRecord } from "../entities/categorization";
|
||||
import * as Constants from "../constants";
|
||||
|
||||
export class CategorizationApiClient {
|
||||
#apiClient: LibreCcmApiClient;
|
||||
|
||||
readonly #CATEGORIES_API_PREFIX = `${Constants.ADMIN_API_PREFIX}/categories`;
|
||||
|
||||
constructor(apiClient: LibreCcmApiClient) {
|
||||
this.#apiClient = apiClient;
|
||||
}
|
||||
|
||||
async getCategoryByDomainAndPath(
|
||||
domain: string,
|
||||
path: string
|
||||
): Promise<Category> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.get(
|
||||
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return buildCategoryFromRecord(await response.json());
|
||||
} else {
|
||||
throw `Failed to get category ${path} of domain ${domain}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to get category ${path} of domain ${domain}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async getCategoryById(categoryId: number): Promise<Category> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.get(
|
||||
`${this.#CATEGORIES_API_PREFIX}/categories/ID-${categoryId}`
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return buildCategoryFromRecord(await response.json());
|
||||
} else {
|
||||
throw `Failed to get category with ID ${categoryId}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to get category with Id ${categoryId}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async getCategoryByUuid(uuid: string): Promise<Category> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.get(
|
||||
`${this.#CATEGORIES_API_PREFIX}/categories/UUID-${uuid}`
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return buildCategoryFromRecord(await response.json());
|
||||
} else {
|
||||
throw `Failed to get category with UUID ${uuid}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to get category with UUID ${uuid}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async updateCategoryByDomainAndPath(
|
||||
domain: string,
|
||||
path: string,
|
||||
category: Category
|
||||
): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.put(
|
||||
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`,
|
||||
JSON.stringify(category)
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to update category ${path} of domain ${domain}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to update category ${path} of domain ${domain}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async updateCategoryById(
|
||||
categoryId: number,
|
||||
category: Category
|
||||
): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.put(
|
||||
`${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`,
|
||||
JSON.stringify(category)
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to update category with ID ${categoryId}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to update category ${categoryId}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async updateCategoryByUuid(
|
||||
uuid: string,
|
||||
category: Category
|
||||
): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.put(
|
||||
`${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`,
|
||||
JSON.stringify(category)
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to update category with UUID ${uuid}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to update category with UUID ${uuid}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteCategoryByDomainAndPath(
|
||||
domain: string,
|
||||
path: string
|
||||
): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.delete(
|
||||
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to update category ${path} of domain ${domain}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch(err) {
|
||||
throw `Failed to delete category ${path} of domain ${domain}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteCategoryById(categoryId: number): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.delete(
|
||||
`${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to delete category with ID ${categoryId}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to delete category ${categoryId}: ${err}`;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteCategoryByUuid(
|
||||
uuid: string,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.delete(
|
||||
`${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`,
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to delete category with UUID ${uuid}:
|
||||
${response.status} ${response.statusText}`;
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to delete category with UUID ${uuid}: ${err}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue