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 }): Promise<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 actions.errorFn(reqId, response.message);
77 Promise.reject<Errors>(CommonResourceService.mapResponseKeys(response));
81 protected serverApi: AxiosInstance;
82 protected resourceType: string;
83 protected actions: ApiActions;
85 constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions) {
86 this.serverApi = serverApi;
87 this.resourceType = '/' + resourceType + '/';
88 this.actions = actions;
91 create(data?: Partial<T> | any) {
92 return CommonResourceService.defaultResponse(
94 .post<T>(this.resourceType, data && CommonResourceService.mapKeys(_.snakeCase)(data)),
99 delete(uuid: string): Promise<T> {
100 return CommonResourceService.defaultResponse(
102 .delete(this.resourceType + uuid),
108 return CommonResourceService.defaultResponse(
110 .get<T>(this.resourceType + uuid),
115 list(args: ListArguments = {}): Promise<ListResults<T>> {
116 const { filters, order, ...other } = args;
119 filters: filters ? `[${filters}]` : undefined,
120 order: order ? order : undefined
122 return CommonResourceService.defaultResponse(
124 .get(this.resourceType, {
125 params: CommonResourceService.mapKeys(_.snakeCase)(params)
131 update(uuid: string, data: Partial<T>) {
132 return CommonResourceService.defaultResponse(
134 .put<T>(this.resourceType + uuid, data && CommonResourceService.mapKeys(_.snakeCase)(data)),
140 export const getCommonResourceServiceError = (errorResponse: any) => {
141 if ('errors' in errorResponse && 'errorToken' in errorResponse) {
142 const error = errorResponse.errors.join('');
144 case /UniqueViolation/.test(error):
145 return CommonResourceServiceError.UNIQUE_VIOLATION;
146 case /ownership cycle/.test(error):
147 return CommonResourceServiceError.OWNERSHIP_CYCLE;
148 case /Mounts cannot be modified in state 'Final'/.test(error):
149 return CommonResourceServiceError.MODIFYING_CONTAINER_REQUEST_FINAL_STATE;
151 return CommonResourceServiceError.UNKNOWN;
154 return CommonResourceServiceError.NONE;