Reorganiza service to make use of dynamic config
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Wed, 25 Jul 2018 14:56:43 +0000 (16:56 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Wed, 25 Jul 2018 14:56:43 +0000 (16:56 +0200)
Feature #13819

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

.env
src/common/api/server-api.ts
src/common/config.ts
src/services/auth-service/auth-service.ts
src/services/services.ts
src/store/auth/auth-reducer.test.ts
src/store/details-panel/details-panel-action.ts

diff --git a/.env b/.env
index a523865a6ae43a5b4e8bc670cd28029bfee3870e..ed397c5cc0952badd931be8ccfdd7ec60d95a6d1 100644 (file)
--- a/.env
+++ b/.env
@@ -3,4 +3,5 @@
 # SPDX-License-Identifier: AGPL-3.0
 
 REACT_APP_ARVADOS_CONFIG_URL=/config.json
 # SPDX-License-Identifier: AGPL-3.0
 
 REACT_APP_ARVADOS_CONFIG_URL=/config.json
-REACT_APP_ARVADOS_API_HOST=https://qr1hi.arvadosapi.com
\ No newline at end of file
+REACT_APP_ARVADOS_API_HOST=qr1hi.arvadosapi.com
+HTTPS=true
\ No newline at end of file
index 5beecd48ee7dafabfb34b5e2c1984af964f08498..bcd2f65985a6bee294e4079b33142818f7f51414 100644 (file)
@@ -6,19 +6,24 @@ import Axios, { AxiosInstance } from "axios";
 
 export const API_HOST = process.env.REACT_APP_ARVADOS_API_HOST;
 
 
 export const API_HOST = process.env.REACT_APP_ARVADOS_API_HOST;
 
-export const serverApi: AxiosInstance = Axios.create({
-    baseURL: API_HOST + '/arvados/v1'
-});
+export const authClient: AxiosInstance = Axios.create();
+export const apiClient: AxiosInstance = Axios.create();
 
 export function setServerApiAuthorizationHeader(token: string) {
 
 export function setServerApiAuthorizationHeader(token: string) {
-    serverApi.defaults.headers.common = {
-        'Authorization': `OAuth2 ${token}`
-    };}
+    [authClient, apiClient].forEach(client => {
+        client.defaults.headers.common = {
+            Authorization: `OAuth2 ${token}`
+        };
+    });
+}
 
 export function removeServerApiAuthorizationHeader() {
 
 export function removeServerApiAuthorizationHeader() {
-    delete serverApi.defaults.headers.common.Authorization;
+    [authClient, apiClient].forEach(client => {
+        delete client.defaults.headers.common.Authorization;
+    });
 }
 
 export const setBaseUrl = (url: string) => {
 }
 
 export const setBaseUrl = (url: string) => {
-    serverApi.defaults.baseURL = url + "/arvados/v1";
+    authClient.defaults.baseURL = url;
+    apiClient.defaults.baseURL = url + "/arvados/v1";
 };
 };
index 4b4a52a3ea4169db5bebbca79e578c8c6230b01b..775b11452ac370450c89976c5cb88f2fd0883afd 100644 (file)
@@ -10,14 +10,21 @@ export interface Config {
     API_HOST: string;
 }
 
     API_HOST: string;
 }
 
-const defaultConfig: Config = {
-    API_HOST: process.env.REACT_APP_ARVADOS_API_HOST || ""
-};
-
 export const fetchConfig = () => {
     return Axios
         .get<Config>(CONFIG_URL + "?nocache=" + (new Date()).getTime())
         .then(response => response.data)
 export const fetchConfig = () => {
     return Axios
         .get<Config>(CONFIG_URL + "?nocache=" + (new Date()).getTime())
         .then(response => response.data)
-        .catch(() => Promise.resolve(defaultConfig));
+        .catch(() => Promise.resolve(getDefaultConfig()))
+        .then(mapConfig);
 };
 
 };
 
+const mapConfig = (config: Config): Config => ({
+    ...config,
+    API_HOST: addProtocol(config.API_HOST)
+});
+
+const getDefaultConfig = (): Config => ({
+    API_HOST: process.env.REACT_APP_ARVADOS_API_HOST || ""
+});
+
+const addProtocol = (url: string) => `${window.location.protocol}//${url}`;
index 1879e6a0abd1b5e2fedb75e3690329407aea4280..551d435f25d208d07a835270b4602a97b228460a 100644 (file)
@@ -24,7 +24,9 @@ export interface UserDetailsResponse {
 
 export class AuthService {
 
 
 export class AuthService {
 
-    constructor(protected serverApi: AxiosInstance) { }
+    constructor(
+        protected authClient: AxiosInstance,
+        protected apiClient: AxiosInstance) { }
 
     public saveApiToken(token: string) {
         localStorage.setItem(API_TOKEN_KEY, token);
 
     public saveApiToken(token: string) {
         localStorage.setItem(API_TOKEN_KEY, token);
@@ -76,16 +78,16 @@ export class AuthService {
 
     public login() {
         const currentUrl = `${window.location.protocol}//${window.location.host}/token`;
 
     public login() {
         const currentUrl = `${window.location.protocol}//${window.location.host}/token`;
-        window.location.assign(`${API_HOST}/login?return_to=${currentUrl}`);
+        window.location.assign(`${this.authClient.defaults.baseURL || ""}/login?return_to=${currentUrl}`);
     }
 
     public logout() {
         const currentUrl = `${window.location.protocol}//${window.location.host}`;
     }
 
     public logout() {
         const currentUrl = `${window.location.protocol}//${window.location.host}`;
-        window.location.assign(`${API_HOST}/logout?return_to=${currentUrl}`);
+        window.location.assign(`${this.authClient.defaults.baseURL || ""}/logout?return_to=${currentUrl}`);
     }
 
     public getUserDetails = (): Promise<User> => {
     }
 
     public getUserDetails = (): Promise<User> => {
-        return this.serverApi
+        return this.apiClient
             .get<UserDetailsResponse>('/users/current')
             .then(resp => ({
                 email: resp.data.email,
             .get<UserDetailsResponse>('/users/current')
             .then(resp => ({
                 email: resp.data.email,
index f0afd76fc82f7158f3e85aa9ddcb95b0d1c2df65..a08ed3cb3de4bbb0a89caa858cd5c5b3163c3551 100644 (file)
@@ -4,13 +4,13 @@
 
 import { AuthService } from "./auth-service/auth-service";
 import { GroupsService } from "./groups-service/groups-service";
 
 import { AuthService } from "./auth-service/auth-service";
 import { GroupsService } from "./groups-service/groups-service";
-import { serverApi } from "../common/api/server-api";
+import { authClient, apiClient } from "../common/api/server-api";
 import { ProjectService } from "./project-service/project-service";
 import { LinkService } from "./link-service/link-service";
 import { FavoriteService } from "./favorite-service/favorite-service";
 
 import { ProjectService } from "./project-service/project-service";
 import { LinkService } from "./link-service/link-service";
 import { FavoriteService } from "./favorite-service/favorite-service";
 
-export const authService = new AuthService(serverApi);
-export const groupsService = new GroupsService(serverApi);
-export const projectService = new ProjectService(serverApi);
-export const linkService = new LinkService(serverApi);
+export const authService = new AuthService(authClient, apiClient);
+export const groupsService = new GroupsService(apiClient);
+export const projectService = new ProjectService(apiClient);
+export const linkService = new LinkService(apiClient);
 export const favoriteService = new FavoriteService(linkService, groupsService);
 export const favoriteService = new FavoriteService(linkService, groupsService);
index ea08e589838414d6bb1b876a21ce45422041ac35..778b500d364b87fe5478b939833af69ff24ca628 100644 (file)
@@ -12,7 +12,6 @@ import {
     USER_OWNER_UUID_KEY,
     USER_UUID_KEY
 } from "../../services/auth-service/auth-service";
     USER_OWNER_UUID_KEY,
     USER_UUID_KEY
 } from "../../services/auth-service/auth-service";
-import { API_HOST } from "../../common/api/server-api";
 
 import 'jest-localstorage-mock';
 
 
 import 'jest-localstorage-mock';
 
@@ -96,7 +95,7 @@ describe('auth-reducer', () => {
         window.location.assign = jest.fn();
         authReducer(initialState, authActions.LOGIN());
         expect(window.location.assign).toBeCalledWith(
         window.location.assign = jest.fn();
         authReducer(initialState, authActions.LOGIN());
         expect(window.location.assign).toBeCalledWith(
-            `${API_HOST}/login?return_to=${window.location.protocol}//${window.location.host}/token`
+            `/login?return_to=${window.location.protocol}//${window.location.host}/token`
         );
     });
 
         );
     });
 
@@ -105,7 +104,7 @@ describe('auth-reducer', () => {
         window.location.assign = jest.fn();
         authReducer(initialState, authActions.LOGOUT());
         expect(window.location.assign).toBeCalledWith(
         window.location.assign = jest.fn();
         authReducer(initialState, authActions.LOGOUT());
         expect(window.location.assign).toBeCalledWith(
-            `${API_HOST}/logout?return_to=${location.protocol}//${location.host}`
+            `/logout?return_to=${location.protocol}//${location.host}`
         );
     });
 });
         );
     });
 });
index ba330f2e417dde4b07bd64914591258e255f084d..974346e6e0c0cc79aacf7af242f0cfc925257c48 100644 (file)
@@ -5,7 +5,7 @@
 import { unionize, ofType, UnionOf } from "unionize";
 import { CommonResourceService } from "../../common/api/common-resource-service";
 import { Dispatch } from "redux";
 import { unionize, ofType, UnionOf } from "unionize";
 import { CommonResourceService } from "../../common/api/common-resource-service";
 import { Dispatch } from "redux";
-import { serverApi } from "../../common/api/server-api";
+import { apiClient } from "../../common/api/server-api";
 import { Resource, ResourceKind } from "../../models/resource";
 
 export const detailsPanelActions = unionize({
 import { Resource, ResourceKind } from "../../models/resource";
 
 export const detailsPanelActions = unionize({
@@ -29,11 +29,11 @@ export const loadDetails = (uuid: string, kind: ResourceKind) =>
 const getService = (kind: ResourceKind) => {
     switch (kind) {
         case ResourceKind.Project:
 const getService = (kind: ResourceKind) => {
     switch (kind) {
         case ResourceKind.Project:
-            return new CommonResourceService(serverApi, "groups");
+            return new CommonResourceService(apiClient, "groups");
         case ResourceKind.Collection:
         case ResourceKind.Collection:
-            return new CommonResourceService(serverApi, "collections");
+            return new CommonResourceService(apiClient, "collections");
         default:
         default:
-            return new CommonResourceService(serverApi, "");
+            return new CommonResourceService(apiClient, "");
     }
 };
 
     }
 };