blob: 6be5db765cb78944602542135a9abc0b21fa6bbc [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';
30
31// content-types
32const APPLICATION_JSON = 'application/json';
33const MULTIPART_FORM_DATA = 'multipart/form-data';
34
35const BINARY = 'binary';
36
Michael Landoefa037d2017-02-19 12:57:33 +020037const AUTHORIZATION_HEADER = 'X-AUTH-TOKEN';
38const STORAGE_AUTH_KEY = 'sdc-auth-token';
39const REQUEST_ID_HEADER = 'X-ECOMP-RequestID';
40const CONTENT_MD5_HEADER = 'Content-MD5';
Michael Landoefa037d2017-02-19 12:57:33 +020041
ilanap1965d162018-01-04 11:34:59 +020042function applySecurity(options, data) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020043 let headers = options.headers || (options.headers = {});
Murali-P72d62fb2018-03-29 17:46:39 +053044 if (options.isAnonymous) {
45 return;
46 }
AviZi280f8012017-06-09 02:39:56 +030047
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020048 let authToken = localStorage.getItem(STORAGE_AUTH_KEY);
49 if (authToken) {
50 headers[AUTHORIZATION_HEADER] = authToken;
51 }
AviZi280f8012017-06-09 02:39:56 +030052
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020053 let catalogApiHeaders = Configuration.get('CatalogApiHeaders'),
54 catalogUidHeader = catalogApiHeaders && catalogApiHeaders.userId;
55 if (catalogUidHeader) {
56 headers[catalogUidHeader.name] = catalogUidHeader.value;
57 }
ilanap1965d162018-01-04 11:34:59 +020058
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020059 headers[REQUEST_ID_HEADER] = uuid.create().toString();
60 if (options.md5) {
61 let headers = options.headers;
62 headers[CONTENT_MD5_HEADER] = window.btoa(
63 md5(JSON.stringify(data)).toLowerCase()
64 );
65 }
AviZi280f8012017-06-09 02:39:56 +030066}
67
ilanap1965d162018-01-04 11:34:59 +020068function handleSuccess(responseHeaders, requestHeaders) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020069 let authToken = responseHeaders[AUTHORIZATION_HEADER];
70 let prevToken = requestHeaders && requestHeaders[AUTHORIZATION_HEADER];
71 if (authToken && authToken !== prevToken) {
72 if (authToken === 'null') {
73 localStorage.removeItem(STORAGE_AUTH_KEY);
74 } else {
75 localStorage.setItem(STORAGE_AUTH_KEY, authToken);
76 }
77 }
AviZi280f8012017-06-09 02:39:56 +030078}
79
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020080class RestAPIUtil {
81 handleRequest(url, type, options = {}, data) {
82 if (DEBUG) {
83 console.log('axios --> Making REST call (' + type + '): ' + url);
84 }
ilanap1965d162018-01-04 11:34:59 +020085
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020086 applySecurity(options, data);
ilanap1965d162018-01-04 11:34:59 +020087
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020088 // TODO see ig necessary or in transformrequest funtion
89 if (type === POST || type === PUT) {
90 if (data instanceof FormData) {
91 options.headers.contentType = MULTIPART_FORM_DATA;
92 } else {
93 options.headers.contentType = APPLICATION_JSON;
94 // config.data = JSON.stringify(data);
95 }
96 } else {
97 data = null;
98 }
ilanap1965d162018-01-04 11:34:59 +020099
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200100 let config = {
101 method: type,
102 url: url,
103 headers: options.headers,
104 data: data
105 };
ilanap1965d162018-01-04 11:34:59 +0200106
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200107 store.dispatch({ type: LoaderConstants.SEND_REQUEST, url: url });
108 if (options.dataType === BINARY) {
109 config.responseType = 'arraybuffer';
110 return axios(config)
111 .then(result => {
112 store.dispatch({
113 type: LoaderConstants.RECEIVE_RESPONSE,
114 url: result.config.url
115 });
116 return {
117 blob: new Blob([result.data]),
118 headers: result.headers
119 };
120 })
121 .catch(error => {
122 store.dispatch({
123 type: LoaderConstants.RECEIVE_RESPONSE,
124 url: error.config.url
125 });
126 errorResponseHandler(error.response);
127 });
128 } else {
129 return axios(config)
130 .then(result => {
131 store.dispatch({
132 type: LoaderConstants.RECEIVE_RESPONSE,
133 url: result.config.url
134 });
135 handleSuccess(result.headers, result.config.headers);
136 return result.data;
137 })
138 .catch(error => {
139 store.dispatch({
140 type: LoaderConstants.RECEIVE_RESPONSE,
141 url: error.config.url
142 });
143 errorResponseHandler(error.response);
144 return Promise.reject({
145 responseJSON: error.response.data
146 });
147 });
148 }
149 }
AviZi280f8012017-06-09 02:39:56 +0300150
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200151 fetch(url, options) {
152 return this.handleRequest(url, GET, options);
153 }
ilanap1965d162018-01-04 11:34:59 +0200154
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200155 get(url, options) {
156 return this.fetch(url, options);
157 }
AviZi280f8012017-06-09 02:39:56 +0300158
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200159 post(url, data, options) {
160 return this.handleRequest(url, POST, options, data);
161 }
ilanap1965d162018-01-04 11:34:59 +0200162
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200163 put(url, data, options) {
164 return this.handleRequest(url, PUT, options, data);
165 }
ilanap1965d162018-01-04 11:34:59 +0200166
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200167 destroy(url, options) {
168 return this.handleRequest(url, DELETE, options);
169 }
AviZi280f8012017-06-09 02:39:56 +0300170}
171
ilanap1965d162018-01-04 11:34:59 +0200172const instance = new RestAPIUtil();
AviZi280f8012017-06-09 02:39:56 +0300173
AviZi280f8012017-06-09 02:39:56 +0300174export default instance;