blob: 66d7c6d4fdd0a3a0605802b6866d3020997b0013 [file] [log] [blame]
ilanap1965d162018-01-04 11:34:59 +02001/*
Einav Weiss Keidard2f57942018-02-14 14:00:07 +02002 * Copyright © 2016-2018 European Support Limited
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
7 *
ilanap1965d162018-01-04 11:34:59 +02008 * http://www.apache.org/licenses/LICENSE-2.0
Michael Landoefa037d2017-02-19 12:57:33 +02009 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
ilanap1965d162018-01-04 11:34:59 +020012 * 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.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import uuid from 'uuid-js';
17import md5 from 'md5';
ilanap1965d162018-01-04 11:34:59 +020018import axios from 'axios';
Michael Landoefa037d2017-02-19 12:57:33 +020019
20import store from 'sdc-app/AppStore.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020021import { actionTypes as LoaderConstants } from 'nfvo-components/loader/LoaderConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020022import Configuration from 'sdc-app/config/Configuration.js';
23import errorResponseHandler from './ErrorResponseHandler.js';
24
ilanap1965d162018-01-04 11:34:59 +020025//methods
26const GET = 'GET';
27const POST = 'POST';
28const PUT = 'PUT';
29const DELETE = 'DELETE';
ilanap1965d162018-01-04 11:34:59 +020030const BINARY = 'binary';
31
Michael Landoefa037d2017-02-19 12:57:33 +020032const AUTHORIZATION_HEADER = 'X-AUTH-TOKEN';
33const STORAGE_AUTH_KEY = 'sdc-auth-token';
34const REQUEST_ID_HEADER = 'X-ECOMP-RequestID';
35const CONTENT_MD5_HEADER = 'Content-MD5';
Michael Landoefa037d2017-02-19 12:57:33 +020036
ilanap1965d162018-01-04 11:34:59 +020037function applySecurity(options, data) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020038 let headers = options.headers || (options.headers = {});
Murali-P72d62fb2018-03-29 17:46:39 +053039 if (options.isAnonymous) {
40 return;
41 }
AviZi280f8012017-06-09 02:39:56 +030042
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020043 let authToken = localStorage.getItem(STORAGE_AUTH_KEY);
44 if (authToken) {
45 headers[AUTHORIZATION_HEADER] = authToken;
46 }
AviZi280f8012017-06-09 02:39:56 +030047
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020048 let catalogApiHeaders = Configuration.get('CatalogApiHeaders'),
49 catalogUidHeader = catalogApiHeaders && catalogApiHeaders.userId;
50 if (catalogUidHeader) {
51 headers[catalogUidHeader.name] = catalogUidHeader.value;
52 }
ilanap1965d162018-01-04 11:34:59 +020053
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020054 headers[REQUEST_ID_HEADER] = uuid.create().toString();
55 if (options.md5) {
56 let headers = options.headers;
57 headers[CONTENT_MD5_HEADER] = window.btoa(
58 md5(JSON.stringify(data)).toLowerCase()
59 );
60 }
AviZi280f8012017-06-09 02:39:56 +030061}
62
ilanap1965d162018-01-04 11:34:59 +020063function handleSuccess(responseHeaders, requestHeaders) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020064 let authToken = responseHeaders[AUTHORIZATION_HEADER];
65 let prevToken = requestHeaders && requestHeaders[AUTHORIZATION_HEADER];
66 if (authToken && authToken !== prevToken) {
67 if (authToken === 'null') {
68 localStorage.removeItem(STORAGE_AUTH_KEY);
69 } else {
70 localStorage.setItem(STORAGE_AUTH_KEY, authToken);
71 }
72 }
AviZi280f8012017-06-09 02:39:56 +030073}
74
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020075class RestAPIUtil {
Yarin Dekelbc205342018-10-16 10:42:49 +030076 handleRequest(url, type, options = {}, data = {}) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020077 applySecurity(options, data);
ilanap1965d162018-01-04 11:34:59 +020078
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020079 let config = {
80 method: type,
81 url: url,
82 headers: options.headers,
83 data: data
84 };
ilanap1965d162018-01-04 11:34:59 +020085
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020086 store.dispatch({ type: LoaderConstants.SEND_REQUEST, url: url });
87 if (options.dataType === BINARY) {
88 config.responseType = 'arraybuffer';
89 return axios(config)
90 .then(result => {
91 store.dispatch({
92 type: LoaderConstants.RECEIVE_RESPONSE,
93 url: result.config.url
94 });
95 return {
96 blob: new Blob([result.data]),
97 headers: result.headers
98 };
99 })
100 .catch(error => {
101 store.dispatch({
102 type: LoaderConstants.RECEIVE_RESPONSE,
103 url: error.config.url
104 });
105 errorResponseHandler(error.response);
106 });
107 } else {
108 return axios(config)
109 .then(result => {
110 store.dispatch({
111 type: LoaderConstants.RECEIVE_RESPONSE,
112 url: result.config.url
113 });
114 handleSuccess(result.headers, result.config.headers);
115 return result.data;
116 })
117 .catch(error => {
118 store.dispatch({
119 type: LoaderConstants.RECEIVE_RESPONSE,
120 url: error.config.url
121 });
122 errorResponseHandler(error.response);
123 return Promise.reject({
124 responseJSON: error.response.data
125 });
126 });
127 }
128 }
AviZi280f8012017-06-09 02:39:56 +0300129
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200130 fetch(url, options) {
131 return this.handleRequest(url, GET, options);
132 }
ilanap1965d162018-01-04 11:34:59 +0200133
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200134 get(url, options) {
135 return this.fetch(url, options);
136 }
AviZi280f8012017-06-09 02:39:56 +0300137
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200138 post(url, data, options) {
139 return this.handleRequest(url, POST, options, data);
140 }
ilanap1965d162018-01-04 11:34:59 +0200141
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200142 put(url, data, options) {
143 return this.handleRequest(url, PUT, options, data);
144 }
ilanap1965d162018-01-04 11:34:59 +0200145
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200146 destroy(url, options) {
147 return this.handleRequest(url, DELETE, options);
148 }
AviZi280f8012017-06-09 02:39:56 +0300149}
150
ilanap1965d162018-01-04 11:34:59 +0200151const instance = new RestAPIUtil();
AviZi280f8012017-06-09 02:39:56 +0300152
AviZi280f8012017-06-09 02:39:56 +0300153export default instance;