update odlux and featureaggregator
[ccsdk/features.git] / sdnr / wt / odlux / framework / src / services / restService.ts
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 import { ApplicationStore } from "../store/applicationStore";
19 import { ReplaceAction } from "../actions/navigationActions";
20
21 const baseUri = `${ window.location.origin }`;
22 const absUrlPattern = /^https?:\/\//;
23 let applicationStore: ApplicationStore | null = null;
24
25 export const startRestService = (store: ApplicationStore) => {
26   applicationStore = store;
27 };
28
29 export const formEncode = (params: { [key: string]: string | number }) => Object.keys(params).map((key) => {
30   return encodeURIComponent(key) + '=' + encodeURIComponent(params[key].toString());
31 }).join('&');
32
33 /** Sends a rest request to the given path. 
34  * @returns The data, or null it there was any error
35  */
36 export async function requestRest<TData>(path: string = '', init: RequestInit = {}, authenticate: boolean = true, isResource: boolean = false): Promise<TData | null> {
37   const res = await requestRestExt<TData>(path, init, authenticate, isResource);
38   if (res && res.status >= 200 && res.status < 300) {
39     return res.data;
40   }
41   return null;
42 }
43
44 /** Sends a rest request to the given path and reports the server state. 
45  *  @returns An object with the server state, a message and the data.
46  */
47 export async function requestRestExt<TData>(path: string = '', init: RequestInit = {}, authenticate: boolean = true, isResource: boolean = false): Promise<{ status: number, message?: string, data: TData | null }> {
48   const result: { status: number, message?: string, data: TData | null } = {
49     status: -1,
50     data: null,
51   };
52   const isAbsUrl = absUrlPattern.test(path);
53   const uri = isAbsUrl ? path : isResource ? path.replace(/\/{2,}/i, '/') : (baseUri) + ('/' + path).replace(/\/{2,}/i, '/');
54   init = {
55     'method': 'GET',
56     ...init,
57     headers: {
58       'Content-Type': 'application/json',
59       'Accept': 'application/json',
60       ...init.headers
61     }
62   };
63   if (!isAbsUrl && authenticate && applicationStore) {
64     const { state: { framework: { authenticationState: { user } } } } = applicationStore;
65     // do not request if the user is not valid
66     if (!user || !user.isValid) {
67       return {
68         ...result,
69         message: "User is not valid or not logged in."
70       };
71     }
72     (init.headers = {
73       ...init.headers,
74       'Authorization': `${user.tokenType} ${user.token}`
75       //'Authorization': 'Basic YWRtaW46YWRtaW4='
76     });
77   }
78   const fetchResult = await fetch(uri, init);
79   if (fetchResult.status === 401 || fetchResult.status === 403) {
80     applicationStore && applicationStore.dispatch(new ReplaceAction(`/login?returnTo=${applicationStore.state.framework.navigationState.pathname}`));
81     return {
82       ...result,
83       status: 403,
84       message: "Authentication requested by server."
85     };
86   }
87   const contentType = fetchResult.headers.get("Content-Type") || fetchResult.headers.get("content-type");
88   const isJson = contentType && contentType.toLowerCase().startsWith("application/json");
89   try {
90     const data = (isJson ? await fetchResult.json() : await fetchResult.text()) as TData;
91     return {
92       ...result,
93       status: fetchResult.status,
94       message: fetchResult.statusText,
95       data: data
96     };
97   } catch (error) {
98     return {
99       ...result,
100       status: fetchResult.status,
101       message: error && error.message || String(error),
102       data: null
103     };
104   }
105 }