1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as _ from "lodash";
6 import { AxiosInstance, AxiosPromise } from "axios";
7 import { Resource } from "src/models/resource";
8 import * as uuid from "uuid/v4";
9 import { ApiActions } from "~/services/api/api-actions";
11 export interface ListArguments {
21 export interface ListResults<T> {
26 itemsAvailable: number;
29 export interface Errors {
34 export enum CommonResourceServiceError {
35 UNIQUE_VIOLATION = 'UniqueViolation',
36 OWNERSHIP_CYCLE = 'OwnershipCycle',
37 MODIFYING_CONTAINER_REQUEST_FINAL_STATE = 'ModifyingContainerRequestFinalState',
42 export class CommonResourceService<T extends Resource> {
44 static mapResponseKeys = (response: { data: any }) =>
45 CommonResourceService.mapKeys(_.camelCase)(response.data)
47 static mapKeys = (mapFn: (key: string) => string) =>
48 (value: any): any => {
50 case _.isPlainObject(value):
53 .map(key => [key, mapFn(key)])
54 .reduce((newValue, [key, newKey]) => ({
56 [newKey]: CommonResourceService.mapKeys(mapFn)(value[key])
58 case _.isArray(value):
59 return value.map(CommonResourceService.mapKeys(mapFn));
65 static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions): Promise<R> {
67 actions.progressFn(reqId, true);
70 actions.progressFn(reqId, false);
73 .then(CommonResourceService.mapResponseKeys)
74 .catch(({ response }) => {
75 actions.progressFn(reqId, false);
76 const errors = CommonResourceService.mapResponseKeys(response) as Errors;
77 actions.errorFn(reqId, errors);
82 static customResponse<R>(promise: AxiosPromise<R>, actions: ApiActions): Promise<R> {
84 actions.progressFn(reqId, true);
87 actions.progressFn(reqId, false);
90 .then((response: { data: any }) => response.data);
94 protected serverApi: AxiosInstance;
95 protected resourceType: string;
96 protected actions: ApiActions;
98 constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions) {
99 this.serverApi = serverApi;
100 this.resourceType = '/' + resourceType + '/';
101 this.actions = actions;
104 create(data?: Partial<T>) {
105 return CommonResourceService.defaultResponse(
107 .post<T>(this.resourceType, data && CommonResourceService.mapKeys(_.snakeCase)(data)),
112 delete(uuid: string): Promise<T> {
113 return CommonResourceService.defaultResponse(
115 .delete(this.resourceType + uuid),
121 return CommonResourceService.defaultResponse(
123 .get<T>(this.resourceType + uuid),
128 list(args: ListArguments = {}): Promise<ListResults<T>> {
129 const { filters, order, ...other } = args;
132 filters: filters ? `[${filters}]` : undefined,
133 order: order ? order : undefined
135 return CommonResourceService.defaultResponse(
137 .get(this.resourceType, {
138 params: CommonResourceService.mapKeys(_.snakeCase)(params)
144 update(uuid: string, data: Partial<T>) {
145 return CommonResourceService.defaultResponse(
147 .put<T>(this.resourceType + uuid, data && CommonResourceService.mapKeys(_.snakeCase)(data)),
153 export const getCommonResourceServiceError = (errorResponse: any) => {
154 if ('errors' in errorResponse && 'errorToken' in errorResponse) {
155 const error = errorResponse.errors.join('');
157 case /UniqueViolation/.test(error):
158 return CommonResourceServiceError.UNIQUE_VIOLATION;
159 case /ownership cycle/.test(error):
160 return CommonResourceServiceError.OWNERSHIP_CYCLE;
161 case /Mounts cannot be modified in state 'Final'/.test(error):
162 return CommonResourceServiceError.MODIFYING_CONTAINER_REQUEST_FINAL_STATE;
164 return CommonResourceServiceError.UNKNOWN;
167 return CommonResourceServiceError.NONE;