blob: 1a5817de664a3a719b37644fd0448b8635856411 [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 = {});
AviZi280f8012017-06-09 02:39:56 +030044
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020045 let authToken = localStorage.getItem(STORAGE_AUTH_KEY);
46 if (authToken) {
47 headers[AUTHORIZATION_HEADER] = authToken;
48 }
AviZi280f8012017-06-09 02:39:56 +030049
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020050 let catalogApiHeaders = Configuration.get('CatalogApiHeaders'),
51 catalogUidHeader = catalogApiHeaders && catalogApiHeaders.userId;
52 if (catalogUidHeader) {
53 headers[catalogUidHeader.name] = catalogUidHeader.value;
54 }
ilanap1965d162018-01-04 11:34:59 +020055
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020056 headers[REQUEST_ID_HEADER] = uuid.create().toString();
57 if (options.md5) {
58 let headers = options.headers;
59 headers[CONTENT_MD5_HEADER] = window.btoa(
60 md5(JSON.stringify(data)).toLowerCase()
61 );
62 }
AviZi280f8012017-06-09 02:39:56 +030063}
64
ilanap1965d162018-01-04 11:34:59 +020065function handleSuccess(responseHeaders, requestHeaders) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020066 let authToken = responseHeaders[AUTHORIZATION_HEADER];
67 let prevToken = requestHeaders && requestHeaders[AUTHORIZATION_HEADER];
68 if (authToken && authToken !== prevToken) {
69 if (authToken === 'null') {
70 localStorage.removeItem(STORAGE_AUTH_KEY);
71 } else {
72 localStorage.setItem(STORAGE_AUTH_KEY, authToken);
73 }
74 }
AviZi280f8012017-06-09 02:39:56 +030075}
76
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020077class RestAPIUtil {
78 handleRequest(url, type, options = {}, data) {
79 if (DEBUG) {
80 console.log('axios --> Making REST call (' + type + '): ' + url);
81 }
ilanap1965d162018-01-04 11:34:59 +020082
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020083 applySecurity(options, data);
ilanap1965d162018-01-04 11:34:59 +020084
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020085 // TODO see ig necessary or in transformrequest funtion
86 if (type === POST || type === PUT) {
87 if (data instanceof FormData) {
88 options.headers.contentType = MULTIPART_FORM_DATA;
89 } else {
90 options.headers.contentType = APPLICATION_JSON;
91 // config.data = JSON.stringify(data);
92 }
93 } else {
94 data = null;
95 }
ilanap1965d162018-01-04 11:34:59 +020096
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020097 let config = {
98 method: type,
99 url: url,
100 headers: options.headers,
101 data: data
102 };
ilanap1965d162018-01-04 11:34:59 +0200103
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200104 store.dispatch({ type: LoaderConstants.SEND_REQUEST, url: url });
105 if (options.dataType === BINARY) {
106 config.responseType = 'arraybuffer';
107 return axios(config)
108 .then(result => {
109 store.dispatch({
110 type: LoaderConstants.RECEIVE_RESPONSE,
111 url: result.config.url
112 });
113 return {
114 blob: new Blob([result.data]),
115 headers: result.headers
116 };
117 })
118 .catch(error => {
119 store.dispatch({
120 type: LoaderConstants.RECEIVE_RESPONSE,
121 url: error.config.url
122 });
123 errorResponseHandler(error.response);
124 });
125 } else {
126 return axios(config)
127 .then(result => {
128 store.dispatch({
129 type: LoaderConstants.RECEIVE_RESPONSE,
130 url: result.config.url
131 });
132 handleSuccess(result.headers, result.config.headers);
133 return result.data;
134 })
135 .catch(error => {
136 store.dispatch({
137 type: LoaderConstants.RECEIVE_RESPONSE,
138 url: error.config.url
139 });
140 errorResponseHandler(error.response);
141 return Promise.reject({
142 responseJSON: error.response.data
143 });
144 });
145 }
146 }
AviZi280f8012017-06-09 02:39:56 +0300147
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200148 fetch(url, options) {
149 return this.handleRequest(url, GET, options);
150 }
ilanap1965d162018-01-04 11:34:59 +0200151
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200152 get(url, options) {
153 return this.fetch(url, options);
154 }
AviZi280f8012017-06-09 02:39:56 +0300155
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200156 post(url, data, options) {
157 return this.handleRequest(url, POST, options, data);
158 }
ilanap1965d162018-01-04 11:34:59 +0200159
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200160 put(url, data, options) {
161 return this.handleRequest(url, PUT, options, data);
162 }
ilanap1965d162018-01-04 11:34:59 +0200163
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200164 destroy(url, options) {
165 return this.handleRequest(url, DELETE, options);
166 }
AviZi280f8012017-06-09 02:39:56 +0300167}
168
ilanap1965d162018-01-04 11:34:59 +0200169const instance = new RestAPIUtil();
AviZi280f8012017-06-09 02:39:56 +0300170
AviZi280f8012017-06-09 02:39:56 +0300171export default instance;