blob: a8b40d32a69477f7d21d714289ab2055853f6216 [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) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020082 applySecurity(options, data);
ilanap1965d162018-01-04 11:34:59 +020083
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020084 // TODO see ig necessary or in transformrequest funtion
85 if (type === POST || type === PUT) {
86 if (data instanceof FormData) {
87 options.headers.contentType = MULTIPART_FORM_DATA;
88 } else {
89 options.headers.contentType = APPLICATION_JSON;
90 // config.data = JSON.stringify(data);
91 }
92 } else {
93 data = null;
94 }
ilanap1965d162018-01-04 11:34:59 +020095
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020096 let config = {
97 method: type,
98 url: url,
99 headers: options.headers,
100 data: data
101 };
ilanap1965d162018-01-04 11:34:59 +0200102
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200103 store.dispatch({ type: LoaderConstants.SEND_REQUEST, url: url });
104 if (options.dataType === BINARY) {
105 config.responseType = 'arraybuffer';
106 return axios(config)
107 .then(result => {
108 store.dispatch({
109 type: LoaderConstants.RECEIVE_RESPONSE,
110 url: result.config.url
111 });
112 return {
113 blob: new Blob([result.data]),
114 headers: result.headers
115 };
116 })
117 .catch(error => {
118 store.dispatch({
119 type: LoaderConstants.RECEIVE_RESPONSE,
120 url: error.config.url
121 });
122 errorResponseHandler(error.response);
123 });
124 } else {
125 return axios(config)
126 .then(result => {
127 store.dispatch({
128 type: LoaderConstants.RECEIVE_RESPONSE,
129 url: result.config.url
130 });
131 handleSuccess(result.headers, result.config.headers);
132 return result.data;
133 })
134 .catch(error => {
135 store.dispatch({
136 type: LoaderConstants.RECEIVE_RESPONSE,
137 url: error.config.url
138 });
139 errorResponseHandler(error.response);
140 return Promise.reject({
141 responseJSON: error.response.data
142 });
143 });
144 }
145 }
AviZi280f8012017-06-09 02:39:56 +0300146
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200147 fetch(url, options) {
148 return this.handleRequest(url, GET, options);
149 }
ilanap1965d162018-01-04 11:34:59 +0200150
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200151 get(url, options) {
152 return this.fetch(url, options);
153 }
AviZi280f8012017-06-09 02:39:56 +0300154
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200155 post(url, data, options) {
156 return this.handleRequest(url, POST, options, data);
157 }
ilanap1965d162018-01-04 11:34:59 +0200158
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200159 put(url, data, options) {
160 return this.handleRequest(url, PUT, options, data);
161 }
ilanap1965d162018-01-04 11:34:59 +0200162
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200163 destroy(url, options) {
164 return this.handleRequest(url, DELETE, options);
165 }
AviZi280f8012017-06-09 02:39:56 +0300166}
167
ilanap1965d162018-01-04 11:34:59 +0200168const instance = new RestAPIUtil();
AviZi280f8012017-06-09 02:39:56 +0300169
AviZi280f8012017-06-09 02:39:56 +0300170export default instance;