diff --git a/ccm-core-apiclient/src/main/typescript/clients/sites-api.ts b/ccm-core-apiclient/src/main/typescript/clients/sites-api.ts index d19b801d4..7fcfc6121 100644 --- a/ccm-core-apiclient/src/main/typescript/clients/sites-api.ts +++ b/ccm-core-apiclient/src/main/typescript/clients/sites-api.ts @@ -1,4 +1,6 @@ import { + ApiClientError, + ApiError, ApiResponse, LibreCcmApiClient, ListView, @@ -27,14 +29,12 @@ export class SitesApiClient { } async getSites(limit: number, offset: number): Promise> { + const url = `${this.#SITES_API_PREFIX}`; try { - const response: ApiResponse = await this.#apiClient.get( - `${this.#SITES_API_PREFIX}`, - { - limit: limit.toString(), - offset: offset.toString(), - } - ); + const response: ApiResponse = await this.#apiClient.get(url, { + limit: limit.toString(), + offset: offset.toString(), + }); if (response.ok) { const result: Record< string, @@ -55,48 +55,79 @@ export class SitesApiClient { offset: result.offset as number, }; } else { - throw `Failed to get sites: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "get", + `Failed to get sites`, + url + ); } } catch (err) { - throw `Failed to get sites: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError(`Failed to get sites: ${err}`); + } } } async getSite(siteIdentifier: string | number): Promise { try { - const response: ApiResponse = await this.#apiClient.get( - `${this.#SITES_API_PREFIX}/${buildIdentifierParam( - siteIdentifier - )}` - ); + const url = `${this.#SITES_API_PREFIX}/${buildIdentifierParam( + siteIdentifier + )}`; + const response: ApiResponse = await this.#apiClient.get(url); if (response.ok) { return buildSiteFromRecord( (await response.json()) as Record ); } else { - throw `Failed to get site ${siteIdentifier}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "get", + `Failed to get site ${siteIdentifier}`, + url + ); } } catch (err) { - throw `Failed to get site ${siteIdentifier}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to get site ${siteIdentifier}: ${err}` + ); + } } } async addSite(site: Site): Promise { + const url = `${this.#SITES_API_PREFIX}`; try { const response: ApiResponse = await this.#apiClient.post( - `${this.#SITES_API_PREFIX}`, + url, JSON.stringify(site) ); if (response.ok) { return site.domainOfSite; } else { - throw `Failed to add site ${site.domainOfSite}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "post", + `Failed to add site ${site.domainOfSite}`, + url + ); } } catch (err) { - throw `Failed to add site ${site.domainOfSite}: ${err}`; + if (err instanceof ApiClientError) { + throw err; + } else { + throw new ApiClientError( + `Failed to add site ${site.domainOfSite}: ${err}` + ); + } } } @@ -104,39 +135,61 @@ export class SitesApiClient { siteIdentifier: string | number, site: Site ): Promise { + const url = `${this.#SITES_API_PREFIX}/${buildIdentifierParam( + siteIdentifier + )}`; try { const response: ApiResponse = await this.#apiClient.put( - `${this.#SITES_API_PREFIX}/${buildIdentifierParam( - siteIdentifier - )}`, + url, JSON.stringify(site) ); if (response.ok) { return; } else { - throw `Failed to update site ${siteIdentifier}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "put", + `Failed to update site ${siteIdentifier}`, + url + ); } } catch (err) { - throw `Failed to update site ${siteIdentifier}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to update site ${siteIdentifier}: ${err}` + ); + } } } async deleteSite(siteIdentifier: string | number): Promise { + const url = `${this.#SITES_API_PREFIX}/${buildIdentifierParam( + siteIdentifier + )}`; try { - const response: ApiResponse = await this.#apiClient.delete( - `${this.#SITES_API_PREFIX}/${buildIdentifierParam( - siteIdentifier - )}` - ); + const response: ApiResponse = await this.#apiClient.delete(url); if (response.ok) { return; } else { - throw `Failed to delete site ${siteIdentifier}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "delete", + `Failed to delete site ${siteIdentifier}`, + url + ); } } catch (err) { - throw `Failed to delete site ${siteIdentifier}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to delete site ${siteIdentifier}: ${err}` + ); + } } } @@ -145,16 +198,13 @@ export class SitesApiClient { limit: number, offset: number ): Promise> { + const siteParam = buildIdentifierParam(siteIdentifier); + const url = `${this.#SITES_API_PREFIX}/${siteParam}/applications`; try { - const siteParam = buildIdentifierParam(siteIdentifier); - - const response: ApiResponse = await this.#apiClient.get( - `${this.#SITES_API_PREFIX}/${siteParam}/applications`, - { - limit: limit.toString(), - offset: offset.toString(), - } - ); + const response: ApiResponse = await this.#apiClient.get(url, { + limit: limit.toString(), + offset: offset.toString(), + }); if (response.ok) { const result: Record< string, @@ -175,13 +225,22 @@ export class SitesApiClient { offset: result.limit as number, }; } else { - throw `Failed to get applications of - site ${siteIdentifier}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "get", + `Failed to get applications of site ${siteIdentifier}`, + url + ); } } catch (err) { - throw `Failed to get applications of - site ${siteIdentifier}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to get applications of site ${siteIdentifier}: ${err}` + ); + } } } @@ -189,29 +248,34 @@ export class SitesApiClient { siteIdentifier: string | number, applicationIdentifier: string | number ): Promise { + const siteParam: string = buildIdentifierParam(siteIdentifier); + const applicationParam: string = buildIdentifierParam( + applicationIdentifier + ); + const url = `${ + this.#SITES_API_PREFIX + }/${siteParam}/groups/${applicationParam}`; try { - const siteParam: string = buildIdentifierParam(siteIdentifier); - const applicationParam: string = buildIdentifierParam( - applicationIdentifier - ); - - const response: ApiResponse = await this.#apiClient.put( - `${ - this.#SITES_API_PREFIX - }/${siteParam}/groups/${applicationParam}` - ); + const response: ApiResponse = await this.#apiClient.put(url); if (response.ok) { return; } else { - throw `Failed to add application ${applicationIdentifier} - to site - ${siteIdentifier}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "put", + `Failed to add application ${applicationIdentifier} to site ${siteIdentifier}`, + url + ); } } catch (err) { - throw `Failed to add application ${applicationIdentifier} - to site - ${siteIdentifier}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to add application ${applicationIdentifier} to site ${siteIdentifier}: ${err}` + ); + } } } @@ -219,29 +283,35 @@ export class SitesApiClient { siteIdentifier: string | number, applicationIdentifier: string | number ): Promise { - try { - const siteParam: string = buildIdentifierParam(siteIdentifier); - const applicationParam: string = buildIdentifierParam( - applicationIdentifier - ); + const siteParam: string = buildIdentifierParam(siteIdentifier); + const applicationParam: string = buildIdentifierParam( + applicationIdentifier + ); + const url = `${ + this.#SITES_API_PREFIX + }/${siteParam}/groups/${applicationParam}`; - const response: ApiResponse = await this.#apiClient.delete( - `${ - this.#SITES_API_PREFIX - }/${siteParam}/groups/${applicationParam}` - ); + try { + const response: ApiResponse = await this.#apiClient.delete(url); if (response.ok) { return; } else { - throw `Failed to remove application ${applicationIdentifier} - fromsite - ${siteIdentifier}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "delete", + `Failed to remove application ${applicationIdentifier} fromsite ${siteIdentifier}`, + url + ); } } catch (err) { - throw `Failed to remove application ${applicationIdentifier} - from site - ${siteIdentifier}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to remove application ${applicationIdentifier} from site ${siteIdentifier}: ${err}` + ); + } } } }