From 06d1e5b07cd7ceee4acf030d73307b45637bf4c9 Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Fri, 24 Jul 2020 17:40:33 +0200 Subject: [PATCH] Clients will be put into separate dir --- .../src/main/typescript/ccm-core-apiclient.ts | 192 +----------------- .../typescript/clients/categorization-api.ts | 191 +++++++++++++++++ 2 files changed, 193 insertions(+), 190 deletions(-) create mode 100644 ccm-core-apiclient/src/main/typescript/clients/categorization-api.ts diff --git a/ccm-core-apiclient/src/main/typescript/ccm-core-apiclient.ts b/ccm-core-apiclient/src/main/typescript/ccm-core-apiclient.ts index c4e36d68a..41bbafea8 100644 --- a/ccm-core-apiclient/src/main/typescript/ccm-core-apiclient.ts +++ b/ccm-core-apiclient/src/main/typescript/ccm-core-apiclient.ts @@ -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 { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 }; diff --git a/ccm-core-apiclient/src/main/typescript/clients/categorization-api.ts b/ccm-core-apiclient/src/main/typescript/clients/categorization-api.ts new file mode 100644 index 000000000..1d4138324 --- /dev/null +++ b/ccm-core-apiclient/src/main/typescript/clients/categorization-api.ts @@ -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 { + 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 { + 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 { + 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 { + 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 { + 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 { + 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 { + 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 { + 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 { + 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}`; + } + } +}