blob: 03908d82038e490d2e4939ed2bcb80c68cac1c19 [file] [log] [blame]
ilanap1965d162018-01-04 11:34:59 +02001/*
Maleke6c3c722018-10-16 16:44:41 +03002* Copyright © 2018 European Support Limited
3*
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
7*
8* http: //www.apache.org/licenses/LICENSE-2.0
9*
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.
15*/
16
Michael Landoefa037d2017-02-19 12:57:33 +020017import uuid from 'uuid-js';
18import md5 from 'md5';
ilanap1965d162018-01-04 11:34:59 +020019import axios from 'axios';
Michael Landoefa037d2017-02-19 12:57:33 +020020
21import store from 'sdc-app/AppStore.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020022import { actionTypes as LoaderConstants } from 'nfvo-components/loader/LoaderConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020023import Configuration from 'sdc-app/config/Configuration.js';
24import errorResponseHandler from './ErrorResponseHandler.js';
25
ilanap1965d162018-01-04 11:34:59 +020026//methods
27const GET = 'GET';
28const POST = 'POST';
29const PUT = 'PUT';
30const DELETE = 'DELETE';
ilanap1965d162018-01-04 11:34:59 +020031const BINARY = 'binary';
32
Michael Landoefa037d2017-02-19 12:57:33 +020033const AUTHORIZATION_HEADER = 'X-AUTH-TOKEN';
34const STORAGE_AUTH_KEY = 'sdc-auth-token';
35const REQUEST_ID_HEADER = 'X-ECOMP-RequestID';
36const CONTENT_MD5_HEADER = 'Content-MD5';
Michael Landoefa037d2017-02-19 12:57:33 +020037
Maleke6c3c722018-10-16 16:44:41 +030038export function applySecurity(options, data) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020039 let headers = options.headers || (options.headers = {});
Murali-P72d62fb2018-03-29 17:46:39 +053040 if (options.isAnonymous) {
41 return;
42 }
AviZi280f8012017-06-09 02:39:56 +030043
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020044 let authToken = localStorage.getItem(STORAGE_AUTH_KEY);
45 if (authToken) {
46 headers[AUTHORIZATION_HEADER] = authToken;
47 }
AviZi280f8012017-06-09 02:39:56 +030048
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020049 let catalogApiHeaders = Configuration.get('CatalogApiHeaders'),
50 catalogUidHeader = catalogApiHeaders && catalogApiHeaders.userId;
51 if (catalogUidHeader) {
52 headers[catalogUidHeader.name] = catalogUidHeader.value;
53 }
ilanap1965d162018-01-04 11:34:59 +020054
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020055 headers[REQUEST_ID_HEADER] = uuid.create().toString();
56 if (options.md5) {
57 let headers = options.headers;
58 headers[CONTENT_MD5_HEADER] = window.btoa(
59 md5(JSON.stringify(data)).toLowerCase()
60 );
61 }
AviZi280f8012017-06-09 02:39:56 +030062}
63
ilanap1965d162018-01-04 11:34:59 +020064function handleSuccess(responseHeaders, requestHeaders) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020065 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);
70 } else {
71 localStorage.setItem(STORAGE_AUTH_KEY, authToken);
72 }
73 }
AviZi280f8012017-06-09 02:39:56 +030074}
75
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020076class RestAPIUtil {
Yarin Dekelbc205342018-10-16 10:42:49 +030077 handleRequest(url, type, options = {}, data = {}) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020078 applySecurity(options, data);
ilanap1965d162018-01-04 11:34:59 +020079
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020080 let config = {
81 method: type,
82 url: url,
83 headers: options.headers,
84 data: data
85 };
ilanap1965d162018-01-04 11:34:59 +020086
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020087 store.dispatch({ type: LoaderConstants.SEND_REQUEST, url: url });
88 if (options.dataType === BINARY) {
89 config.responseType = 'arraybuffer';
90 return axios(config)
91 .then(result => {
92 store.dispatch({
93 type: LoaderConstants.RECEIVE_RESPONSE,
94 url: result.config.url
95 });
96 return {
97 blob: new Blob([result.data]),
98 headers: result.headers
99 };
100 })
101 .catch(error => {
102 store.dispatch({
103 type: LoaderConstants.RECEIVE_RESPONSE,
104 url: error.config.url
105 });
106 errorResponseHandler(error.response);
107 });
108 } else {
109 return axios(config)
110 .then(result => {
111 store.dispatch({
112 type: LoaderConstants.RECEIVE_RESPONSE,
113 url: result.config.url
114 });
115 handleSuccess(result.headers, result.config.headers);
116 return result.data;
117 })
118 .catch(error => {
119 store.dispatch({
120 type: LoaderConstants.RECEIVE_RESPONSE,
121 url: error.config.url
122 });
123 errorResponseHandler(error.response);
124 return Promise.reject({
125 responseJSON: error.response.data
126 });
127 });
128 }
129 }
AviZi280f8012017-06-09 02:39:56 +0300130
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200131 fetch(url, options) {
132 return this.handleRequest(url, GET, options);
133 }
ilanap1965d162018-01-04 11:34:59 +0200134
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200135 get(url, options) {
136 return this.fetch(url, options);
137 }
AviZi280f8012017-06-09 02:39:56 +0300138
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200139 post(url, data, options) {
140 return this.handleRequest(url, POST, options, data);
141 }
ilanap1965d162018-01-04 11:34:59 +0200142
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200143 put(url, data, options) {
144 return this.handleRequest(url, PUT, options, data);
145 }
ilanap1965d162018-01-04 11:34:59 +0200146
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200147 destroy(url, options) {
148 return this.handleRequest(url, DELETE, options);
149 }
AviZi280f8012017-06-09 02:39:56 +0300150}
151
ilanap1965d162018-01-04 11:34:59 +0200152const instance = new RestAPIUtil();
AviZi280f8012017-06-09 02:39:56 +0300153
AviZi280f8012017-06-09 02:39:56 +0300154export default instance;