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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
18 import { ApplicationStore } from "../store/applicationStore";
19 import { ReplaceAction } from "../actions/navigationActions";
21 const baseUri = `${ window.location.origin }`;
22 const absUrlPattern = /^https?:\/\//;
23 let applicationStore: ApplicationStore | null = null;
25 export const startRestService = (store: ApplicationStore) => {
26 applicationStore = store;
29 export const formEncode = (params: { [key: string]: string | number }) => Object.keys(params).map((key) => {
30 return encodeURIComponent(key) + '=' + encodeURIComponent(params[key].toString());
33 /** Sends a rest request to the given path.
34 * @returns The data, or null it there was any error
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) {
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.
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 } = {
52 const isAbsUrl = absUrlPattern.test(path);
53 const uri = isAbsUrl ? path : isResource ? path.replace(/\/{2,}/i, '/') : (baseUri) + ('/' + path).replace(/\/{2,}/i, '/');
58 'Content-Type': 'application/json',
59 'Accept': 'application/json',
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) {
69 message: "User is not valid or not logged in."
74 'Authorization': `${user.tokenType} ${user.token}`
75 //'Authorization': 'Basic YWRtaW46YWRtaW4='
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}`));
84 message: "Authentication requested by server."
87 const contentType = fetchResult.headers.get("Content-Type") || fetchResult.headers.get("content-type");
88 const isJson = contentType && contentType.toLowerCase().startsWith("application/json");
90 const data = (isJson ? await fetchResult.json() : await fetchResult.text()) as TData;
93 status: fetchResult.status,
94 message: fetchResult.statusText,
100 status: fetchResult.status,
101 message: error && error.message || String(error),