15803: Display loading screen during "get user info" and "self activate"
[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 { ApiClientAuthorizationService } from '~/services/api-client-authorization-service/api-client-authorization-service';
7 import { AuthService } from "./auth-service/auth-service";
8 import { GroupsService } from "./groups-service/groups-service";
9 import { ProjectService } from "./project-service/project-service";
10 import { LinkService } from "./link-service/link-service";
11 import { FavoriteService } from "./favorite-service/favorite-service";
12 import { CollectionService } from "./collection-service/collection-service";
13 import { TagService } from "./tag-service/tag-service";
14 import { CollectionFilesService } from "./collection-files-service/collection-files-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
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.webdavClient.defaults.headers = {
43         Authorization: `Bearer ${token}`
44     };
45 }
46
47 export function removeAuthorizationHeader(services: ServiceRepository) {
48     delete services.apiClient.defaults.headers.common;
49     delete services.webdavClient.defaults.headers.common;
50 }
51
52 export const createServices = (config: Config, actions: ApiActions) => {
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 = Axios.create({ headers: {} });
58     apiClient.defaults.baseURL = config.baseUrl;
59
60     const webdavClient = new WebDAV();
61     webdavClient.defaults.baseURL = config.keepWebServiceUrl;
62
63     const apiClientAuthorizationService = new ApiClientAuthorizationService(apiClient, actions);
64     const authorizedKeysService = new AuthorizedKeysService(apiClient, actions);
65     const containerRequestService = new ContainerRequestService(apiClient, actions);
66     const containerService = new ContainerService(apiClient, actions);
67     const groupsService = new GroupsService(apiClient, actions);
68     const keepService = new KeepService(apiClient, actions);
69     const linkService = new LinkService(apiClient, actions);
70     const logService = new LogService(apiClient, actions);
71     const nodeService = new NodeService(apiClient, actions);
72     const permissionService = new PermissionService(apiClient, actions);
73     const projectService = new ProjectService(apiClient, actions);
74     const repositoriesService = new RepositoriesService(apiClient, actions);
75     const userService = new UserService(apiClient, actions);
76     const virtualMachineService = new VirtualMachinesService(apiClient, actions);
77     const workflowService = new WorkflowService(apiClient, actions);
78     const linkAccountService = new LinkAccountService(apiClient, actions);
79
80     const ancestorsService = new AncestorService(groupsService, userService);
81     const authService = new AuthService(apiClient, config.rootUrl, actions);
82     const collectionService = new CollectionService(apiClient, webdavClient, authService, actions);
83     const collectionFilesService = new CollectionFilesService(collectionService);
84     const favoriteService = new FavoriteService(linkService, groupsService);
85     const tagService = new TagService(linkService);
86     const searchService = new SearchService();
87     const vocabularyService = new VocabularyService(config.vocabularyUrl);
88     const fileViewersConfig = new FileViewersConfigService(config.fileViewersConfigUrl);
89
90     return {
91         ancestorsService,
92         apiClient,
93         apiClientAuthorizationService,
94         authService,
95         authorizedKeysService,
96         collectionFilesService,
97         collectionService,
98         containerRequestService,
99         containerService,
100         favoriteService,
101         fileViewersConfig,
102         groupsService,
103         keepService,
104         linkService,
105         logService,
106         nodeService,
107         permissionService,
108         projectService,
109         repositoriesService,
110         searchService,
111         tagService,
112         userService,
113         virtualMachineService,
114         webdavClient,
115         workflowService,
116         vocabularyService,
117         linkAccountService
118     };
119 };
120
121 export const getResourceService = (kind?: ResourceKind) => (serviceRepository: ServiceRepository) => {
122     switch (kind) {
123         case ResourceKind.USER:
124             return serviceRepository.userService;
125         case ResourceKind.GROUP:
126             return serviceRepository.groupsService;
127         case ResourceKind.COLLECTION:
128             return serviceRepository.collectionService;
129         default:
130             return undefined;
131     }
132 };