21128: project favorite in place Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa.knox...
[arvados.git] / src / services / services.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import Axios from "axios";
6 import { AxiosInstance } from "axios";
7 import { ApiClientAuthorizationService } from 'services/api-client-authorization-service/api-client-authorization-service';
8 import { AuthService } from "./auth-service/auth-service";
9 import { GroupsService } from "./groups-service/groups-service";
10 import { ProjectService } from "./project-service/project-service";
11 import { LinkService } from "./link-service/link-service";
12 import { FavoriteService } from "./favorite-service/favorite-service";
13 import { CollectionService } from "./collection-service/collection-service";
14 import { TagService } from "./tag-service/tag-service";
15 import { KeepService } from "./keep-service/keep-service";
16 import { WebDAV } from "common/webdav";
17 import { Config } from "common/config";
18 import { UserService } from './user-service/user-service';
19 import { AncestorService } from "services/ancestors-service/ancestors-service";
20 import { ResourceKind } from "models/resource";
21 import { ContainerRequestService } from './container-request-service/container-request-service';
22 import { ContainerService } from './container-service/container-service';
23 import { LogService } from './log-service/log-service';
24 import { ApiActions } from "services/api/api-actions";
25 import { WorkflowService } from "services/workflow-service/workflow-service";
26 import { SearchService } from 'services/search-service/search-service';
27 import { PermissionService } from "services/permission-service/permission-service";
28 import { VirtualMachinesService } from "services/virtual-machines-service/virtual-machines-service";
29 import { RepositoriesService } from 'services/repositories-service/repositories-service';
30 import { AuthorizedKeysService } from 'services/authorized-keys-service/authorized-keys-service';
31 import { VocabularyService } from 'services/vocabulary-service/vocabulary-service';
32 import { FileViewersConfigService } from 'services/file-viewers-config-service/file-viewers-config-service';
33 import { LinkAccountService } from "./link-account-service/link-account-service";
34 import parse from "parse-duration";
35
36 export type ServiceRepository = ReturnType<typeof createServices>;
37
38 export function setAuthorizationHeader(services: ServiceRepository, token: string) {
39     services.apiClient.defaults.headers.common = {
40         Authorization: `Bearer ${token}`
41     };
42     services.keepWebdavClient.setAuthorization(`Bearer ${token}`);
43     services.apiWebdavClient.setAuthorization(`Bearer ${token}`);
44 }
45
46 export function removeAuthorizationHeader(services: ServiceRepository) {
47     delete services.apiClient.defaults.headers.common;
48     services.keepWebdavClient.setAuthorization(undefined);
49     services.apiWebdavClient.setAuthorization(undefined);
50 }
51
52 export const createServices = (config: Config, actions: ApiActions, useApiClient?: AxiosInstance) => {
53     // Need to give empty 'headers' object or it will create an
54     // instance with a reference to the global default headers object,
55     // which is very bad because that means setAuthorizationHeader
56     // would update the global default instead of the instance default.
57     const apiClient = useApiClient || Axios.create({ headers: {} });
58     apiClient.defaults.baseURL = config.baseUrl;
59
60     const keepWebdavClient = new WebDAV({
61         baseURL: config.keepWebServiceUrl
62     });
63
64     const apiWebdavClient = new WebDAV({
65         baseURL: config.baseUrl
66     });
67
68     const apiClientAuthorizationService = new ApiClientAuthorizationService(apiClient, actions);
69     const authorizedKeysService = new AuthorizedKeysService(apiClient, actions);
70     const containerRequestService = new ContainerRequestService(apiClient, actions);
71     const containerService = new ContainerService(apiClient, actions);
72     const groupsService = new GroupsService(apiClient, actions);
73     const keepService = new KeepService(apiClient, actions);
74     const linkService = new LinkService(apiClient, actions);
75     const logService = new LogService(apiClient, apiWebdavClient, actions);
76     const permissionService = new PermissionService(apiClient, actions);
77     const projectService = new ProjectService(apiClient, actions);
78     const repositoriesService = new RepositoriesService(apiClient, actions);
79     const userService = new UserService(apiClient, actions);
80     const virtualMachineService = new VirtualMachinesService(apiClient, actions);
81     const workflowService = new WorkflowService(apiClient, actions);
82     const linkAccountService = new LinkAccountService(apiClient, actions);
83
84     const idleTimeout = (config && config.clusterConfig && config.clusterConfig.Workbench.IdleTimeout) || '0s';
85     const authService = new AuthService(apiClient, config.rootUrl, actions,
86         (parse(idleTimeout, 's') || 0) > 0);
87
88     const collectionService = new CollectionService(apiClient, keepWebdavClient, authService, actions);
89     const ancestorsService = new AncestorService(groupsService, userService, collectionService);
90     const favoriteService = new FavoriteService(linkService, groupsService);
91     const tagService = new TagService(linkService);
92     const searchService = new SearchService();
93     const vocabularyService = new VocabularyService(config.vocabularyUrl);
94     const fileViewersConfig = new FileViewersConfigService(config.fileViewersConfigUrl);
95
96     return {
97         ancestorsService,
98         apiClient,
99         apiClientAuthorizationService,
100         authService,
101         authorizedKeysService,
102         collectionService,
103         containerRequestService,
104         containerService,
105         favoriteService,
106         fileViewersConfig,
107         groupsService,
108         keepService,
109         linkService,
110         logService,
111         permissionService,
112         projectService,
113         repositoriesService,
114         searchService,
115         tagService,
116         userService,
117         virtualMachineService,
118         keepWebdavClient,
119         apiWebdavClient,
120         workflowService,
121         vocabularyService,
122         linkAccountService
123     };
124 };
125
126 export const getResourceService = (kind?: ResourceKind) => (serviceRepository: ServiceRepository) => {
127     switch (kind) {
128         case ResourceKind.USER:
129             return serviceRepository.userService;
130         case ResourceKind.GROUP:
131             return serviceRepository.groupsService;
132         case ResourceKind.COLLECTION:
133             return serviceRepository.collectionService;
134         default:
135             return undefined;
136     }
137 };