Client for categories API implemented

Jens Pelzetter 2020-07-26 11:28:43 +02:00
parent c96a47990f
commit d05a72e9af
1 changed files with 222 additions and 3 deletions

View File

@ -395,7 +395,7 @@ export class CategorizationApiClient {
const response: ApiResponse = await this.#apiClient.get( const response: ApiResponse = await this.#apiClient.get(
`${ `${
this.#CATEGORIES_API_PREFIX this.#CATEGORIES_API_PREFIX
}/$domain/${path}/objects?limit=${limit}&offset=${offset}` }/$domain/${path}/@objects?limit=${limit}&offset=${offset}`
); );
if (response.ok) { if (response.ok) {
@ -412,8 +412,8 @@ export class CategorizationApiClient {
list: objects, list: objects,
count: result.count as number, count: result.count as number,
limit: result.limit as number, limit: result.limit as number,
offset: result.offset as number offset: result.offset as number,
} };
} else { } else {
throw `Failed to get objects in category ${path} of throw `Failed to get objects in category ${path} of
domain ${domain}: ${response.status} ${response.statusText}`; domain ${domain}: ${response.status} ${response.statusText}`;
@ -423,4 +423,223 @@ export class CategorizationApiClient {
domain ${domain}: ${err}`; domain ${domain}: ${err}`;
} }
} }
async getObjectsInCategoryWithId(
categoryId: number,
limit = 20,
offset = 0
): Promise<ListView<Categorization>> {
try {
const response: ApiResponse = await this.#apiClient.get(
`${
this.#CATEGORIES_API_PREFIX
}/ID-${categoryId}/@objects?limit=${limit}&offset=${offset}`
);
if (response.ok) {
const result: Record<string, unknown> = await response.json();
const list: Record<string, unknown>[] = result.list as Record<
string,
unknown
>[];
const objects: Categorization[] = list.map((record) =>
buildCategorizationFromRecord(record)
);
return {
list: objects,
count: result.count as number,
limit: result.limit as number,
offset: result.offset as number,
};
} else {
throw `Failed to get objects in category with
ID ${categoryId}: ${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to get objects in category with
Id ${categoryId}: ${err}`;
}
}
async getObjectsInCategoryWithUuid(
uuid: string,
limit = 20,
offset = 0
): Promise<ListView<Categorization>> {
try {
const response: ApiResponse = await this.#apiClient.get(
`${
this.#CATEGORIES_API_PREFIX
}/UUID-${uuid}/@objects?limit=${limit}&offset=${offset}`
);
if (response.ok) {
const result: Record<string, unknown> = await response.json();
const list: Record<string, unknown>[] = result.list as Record<
string,
unknown
>[];
const objects: Categorization[] = list.map((record) =>
buildCategorizationFromRecord(record)
);
return {
list: objects,
count: result.count as number,
limit: result.limit as number,
offset: result.offset as number,
};
} else {
throw `Failed to get objects in category with
UUID ${uuid}: ${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to get objects in category with
UUId ${uuid}: ${err}`;
}
}
async addObjectToCategoryOfDomain(
domain: string,
path: string,
categorization: Categorization
): Promise<string> {
try {
const response: ApiResponse = await this.#apiClient.post(
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`,
JSON.stringify(categorization)
);
if (response.ok) {
const result: string = await response.text();
return result;
} else {
throw `Failed to add object to category ${path} of
domain ${domain}: ${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to add object to category ${path} of
domain ${domain}: ${err}`;
}
}
async addObjectToCategoryWithId(
categoryId: number,
categorization: Categorization
): Promise<string> {
try {
const response: ApiResponse = await this.#apiClient.post(
`${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`,
JSON.stringify(categorization)
);
if (response.ok) {
const result: string = await response.text();
return result;
} else {
throw `Failed to add object to category with
ID ${categoryId}: ${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to add object to category with
ID ${categoryId}: ${err}`;
}
}
async addObjectToCategoryWithUuid(
uuid: string,
categorization: Categorization
): Promise<string> {
try {
const response: ApiResponse = await this.#apiClient.post(
`${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`,
JSON.stringify(categorization)
);
if (response.ok) {
const result: string = await response.text();
return result;
} else {
throw `Failed to add object to category with
UUID ${uuid}: ${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to add object to category with
UUID ${uuid}: ${err}`;
}
}
async removeObjectFromCategoryOfDomain(
domain: string,
path: string,
objectIdentifier: string
): Promise<void> {
try {
const response: ApiResponse = await this.#apiClient.delete(
`${
this.#CATEGORIES_API_PREFIX
}/${domain}/${path}/@objects/${objectIdentifier}`
);
if (response.ok) {
return;
} else {
throw `Failed to remove object ${objectIdentifier} from
category ${path} of domain ${domain}:
${response.status} ${response.statusText}.`;
}
} catch (err) {
throw `Failed to remove object ${objectIdentifier} from
category ${path} of domain ${domain}: ${err}`;
}
}
async removeObjectFromCategoryWithId(
categoryId: number,
objectIdentifier: string
): Promise<void> {
try {
const response: ApiResponse = await this.#apiClient.delete(
`${
this.#CATEGORIES_API_PREFIX
}/ID-${categoryId}/@objects/${objectIdentifier}`
);
if (response.ok) {
return;
} else {
throw `Failed to remove object ${objectIdentifier} from
category with ID ${categoryId}:
${response.status} ${response.statusText}.`;
}
} catch (err) {
throw `Failed to remove object ${objectIdentifier} from
category with ID ${categoryId}: ${err}`;
}
}
async removeObjectFromCategoryWithUuid(
uuid: string,
objectIdentifier: string
): Promise<void> {
try {
const response: ApiResponse = await this.#apiClient.delete(
`${
this.#CATEGORIES_API_PREFIX
}/UUID-${uuid}/@objects/${objectIdentifier}`
);
if (response.ok) {
return;
} else {
throw `Failed to remove object ${objectIdentifier} from
category with UUID ${uuid}:
${response.status} ${response.statusText}.`;
}
} catch (err) {
throw `Failed to remove object ${objectIdentifier} from
category with UUID ${uuid}: ${err}`;
}
}
} }