2 * Copyright © 2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http: //www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 import uuid from 'uuid-js';
18 import md5 from 'md5';
19 import axios from 'axios';
21 import store from 'sdc-app/AppStore.js';
22 import { actionTypes as LoaderConstants } from 'nfvo-components/loader/LoaderConstants.js';
23 import Configuration from 'sdc-app/config/Configuration.js';
24 import errorResponseHandler from './ErrorResponseHandler.js';
30 const DELETE = 'DELETE';
31 const BINARY = 'binary';
33 const AUTHORIZATION_HEADER = 'X-AUTH-TOKEN';
34 const STORAGE_AUTH_KEY = 'sdc-auth-token';
35 const REQUEST_ID_HEADER = 'X-ECOMP-RequestID';
36 const CONTENT_MD5_HEADER = 'Content-MD5';
38 export function applySecurity(options, data) {
39 let headers = options.headers || (options.headers = {});
40 if (options.isAnonymous) {
44 let authToken = localStorage.getItem(STORAGE_AUTH_KEY);
46 headers[AUTHORIZATION_HEADER] = authToken;
49 let catalogApiHeaders = Configuration.get('CatalogApiHeaders'),
50 catalogUidHeader = catalogApiHeaders && catalogApiHeaders.userId;
51 if (catalogUidHeader) {
52 headers[catalogUidHeader.name] = catalogUidHeader.value;
55 headers[REQUEST_ID_HEADER] = uuid.create().toString();
57 let headers = options.headers;
58 headers[CONTENT_MD5_HEADER] = window.btoa(
59 md5(JSON.stringify(data)).toLowerCase()
64 function handleSuccess(responseHeaders, requestHeaders) {
65 let authToken = responseHeaders[AUTHORIZATION_HEADER];
66 let prevToken = requestHeaders && requestHeaders[AUTHORIZATION_HEADER];
67 if (authToken && authToken !== prevToken) {
68 if (authToken === 'null') {
69 localStorage.removeItem(STORAGE_AUTH_KEY);
71 localStorage.setItem(STORAGE_AUTH_KEY, authToken);
77 handleRequest(url, type, options = {}, data = {}) {
78 applySecurity(options, data);
83 headers: options.headers,
87 if (options.validateStatus) {
88 config.validateStatus = options.validateStatus;
91 if (options.onUploadProgress) {
92 config.onUploadProgress = options.onUploadProgress;
95 if (!options.noLoading) {
96 store.dispatch({ type: LoaderConstants.SEND_REQUEST, url: url });
98 if (options.dataType === BINARY) {
99 config.responseType = 'arraybuffer';
102 if (!options.noLoading) {
104 type: LoaderConstants.RECEIVE_RESPONSE,
105 url: result.config.url
110 blob: new Blob([result.data]),
111 headers: result.headers
115 if (!options.noLoading) {
117 type: LoaderConstants.RECEIVE_RESPONSE,
118 url: error.config.url
121 errorResponseHandler(error.response);
127 type: LoaderConstants.RECEIVE_RESPONSE,
128 url: result.config.url
130 handleSuccess(result.headers, result.config.headers);
135 type: LoaderConstants.RECEIVE_RESPONSE,
136 url: error.config.url
138 errorResponseHandler(error.response);
139 return Promise.reject({
140 responseJSON: error.response.data
145 fetch(url, options) {
146 return this.handleRequest(url, GET, options);
150 return this.fetch(url, options);
153 post(url, data, options) {
154 return this.handleRequest(url, POST, options, data);
157 put(url, data, options) {
158 return this.handleRequest(url, PUT, options, data);
161 destroy(url, options) {
162 return this.handleRequest(url, DELETE, options);
166 const instance = new RestAPIUtil();
168 export default instance;