17119: add more unit tests.
[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 { NodeService } from '~/services/node-service/node-service';
33 import { FileViewersConfigService } from '~/services/file-viewers-config-service/file-viewers-config-service';
34 import { LinkAccountService } from "./link-account-service/link-account-service";
35 import parse from "parse-duration";
36
37 export type ServiceRepository = ReturnType<typeof createServices>;
38
39 export function setAuthorizationHeader(services: ServiceRepository, token: string) {
40     services.apiClient.defaults.headers.common = {
41         Authorization: `Bearer ${token}`
42     };
43     services.webdavClient.defaults.headers = {
44         Authorization: `Bearer ${token}`
45     };
46 }
47
48 export function removeAuthorizationHeader(services: ServiceRepository) {
49     delete services.apiClient.defaults.headers.common;
50     delete services.webdavClient.defaults.headers.common;
51 }
52
53 export const createServices = (config: Config, actions: ApiActions, useApiClient?: AxiosInstance) => {
54     // Need to give empty 'headers' object or it will create an
55     // instance with a reference to the global default headers object,
56     // which is very bad because that means setAuthorizationHeader
57     // would update the global default instead of the instance default.
58     const apiClient = useApiClient || Axios.create({ headers: {} });
59     apiClient.defaults.baseURL = config.baseUrl;
60
61     const webdavClient = new WebDAV();
62     webdavClient.defaults.baseURL = config.keepWebServiceUrl;
63
64     const apiClientAuthorizationService = new ApiClientAuthorizationService(apiClient, actions);
65     const authorizedKeysService = new AuthorizedKeysService(apiClient, actions);
66     const containerRequestService = new ContainerRequestService(apiClient, actions);
67     const containerService = new ContainerService(apiClient, actions);
68     const groupsService = new GroupsService(apiClient, actions);
69     const keepService = new KeepService(apiClient, actions);
70     const linkService = new LinkService(apiClient, actions);
71     const logService = new LogService(apiClient, actions);
72     const nodeService = new NodeService(apiClient, actions);
73     const permissionService = new PermissionService(apiClient, actions);
74     const projectService = new ProjectService(apiClient, actions);
75     const repositoriesService = new RepositoriesService(apiClient, actions);
76     const userService = new UserService(apiClient, actions);
77     const virtualMachineService = new VirtualMachinesService(apiClient, actions);
78     const workflowService = new WorkflowService(apiClient, actions);
79     const linkAccountService = new LinkAccountService(apiClient, actions);
80
81     const ancestorsService = new AncestorService(groupsService, userService);
82
83     const idleTimeout = config && config.clusterConfig && config.clusterConfig.Workbench.IdleTimeout || '0s';
84     const authService = new AuthService(apiClient, config.rootUrl, actions,
85         (parse(idleTimeout, 's') || 0) > 0);
86
87     const collectionService = new CollectionService(apiClient, webdavClient, authService, actions);
88     const favoriteService = new FavoriteService(linkService, groupsService);
89     const tagService = new TagService(linkService);
90     const searchService = new SearchService();
91     const vocabularyService = new VocabularyService(config.vocabularyUrl);
92     const fileViewersConfig = new FileViewersConfigService(config.fileViewersConfigUrl);
93
94     return {
95         ancestorsService,
96         apiClient,
97         apiClientAuthorizationService,
98         authService,
99         authorizedKeysService,
100         collectionService,
101         containerRequestService,
102         containerService,
103         favoriteService,
104         fileViewersConfig,
105         groupsService,
106         keepService,
107         linkService,
108         logService,
109         nodeService,
110         permissionService,
111         projectService,
112         repositoriesService,
113         searchService,
114         tagService,
115         userService,
116         virtualMachineService,
117         webdavClient,
118         workflowService,
119         vocabularyService,
120         linkAccountService
121     };
122 };
123
124 export const getResourceService = (kind?: ResourceKind) => (serviceRepository: ServiceRepository) => {
125     switch (kind) {
126         case ResourceKind.USER:
127             return serviceRepository.userService;
128         case ResourceKind.GROUP:
129             return serviceRepository.groupsService;
130         case ResourceKind.COLLECTION:
131             return serviceRepository.collectionService;
132         default:
133             return undefined;
134     }
135 };