blob: 5b079863907d90c70c1d37226abfc819c3356bd0 [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';
21import {actionTypes as LoaderConstants} from 'nfvo-components/loader/LoaderConstants.js';
22import 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
42
ilanap1965d162018-01-04 11:34:59 +020043function applySecurity(options, data) {
44 let headers = options.headers || (options.headers = {});
AviZi280f8012017-06-09 02:39:56 +030045
ilanap1965d162018-01-04 11:34:59 +020046 let authToken = localStorage.getItem(STORAGE_AUTH_KEY);
47 if (authToken) {
48 headers[AUTHORIZATION_HEADER] = authToken;
49 }
AviZi280f8012017-06-09 02:39:56 +030050
ilanap785dc1e2018-01-08 15:50:18 +020051 let catalogApiHeaders = Configuration.get('CatalogApiHeaders'),
52 catalogUidHeader = catalogApiHeaders && catalogApiHeaders.userId;
53 if (catalogUidHeader) {
54 headers[catalogUidHeader.name] = catalogUidHeader.value;
ilanap1965d162018-01-04 11:34:59 +020055 }
56
57 headers[REQUEST_ID_HEADER] = uuid.create().toString();
AviZi280f8012017-06-09 02:39:56 +030058 if (options.md5) {
59 let headers = options.headers;
60 headers[CONTENT_MD5_HEADER] = window.btoa(md5(JSON.stringify(data)).toLowerCase());
61 }
62}
63
ilanap1965d162018-01-04 11:34:59 +020064
65function handleSuccess(responseHeaders, requestHeaders) {
66 let authToken = responseHeaders[AUTHORIZATION_HEADER];
67 let prevToken = requestHeaders && requestHeaders[AUTHORIZATION_HEADER];
AviZi280f8012017-06-09 02:39:56 +030068 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 }
75}
76
ilanap1965d162018-01-04 11:34:59 +020077class RestAPIUtil {
AviZi280f8012017-06-09 02:39:56 +030078 handleRequest(url, type, options = {}, data){
ilanap1965d162018-01-04 11:34:59 +020079 if (DEBUG) {
80 console.log('axios --> Making REST call (' + type + '): ' + url);
81 }
82
83 applySecurity(options, data);
84
85 // 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;
AviZi280f8012017-06-09 02:39:56 +030089 }
ilanap1965d162018-01-04 11:34:59 +020090 else {
91 options.headers.contentType = APPLICATION_JSON;
92// config.data = JSON.stringify(data);
93 }
94
95 } else {
96 data = null;
97 }
98
99 let config = {
100 method: type,
101 url: url,
102 headers : options.headers,
103 data : data
AviZi280f8012017-06-09 02:39:56 +0300104 };
105
ilanap1965d162018-01-04 11:34:59 +0200106 store.dispatch({type: LoaderConstants.SEND_REQUEST, url: url});
107 if (options.dataType === BINARY) {
108 config.responseType = 'arraybuffer';
109 return axios(config).
110 then(result => {
111 store.dispatch({type: LoaderConstants.RECEIVE_RESPONSE, url : result.config.url});
112 return ({
113 blob : new Blob([result.data] ),
114 headers : result.headers
115 });
116 }).catch(error => {
117 store.dispatch({type: LoaderConstants.RECEIVE_RESPONSE, url : error.config.url});
118 errorResponseHandler(error.response); });
119 } else {
120 return axios(config).
121 then(result => {
122 store.dispatch({type: LoaderConstants.RECEIVE_RESPONSE, url : result.config.url});
123 handleSuccess(result.headers, result.config.headers);
124 return result.data;
125 }).catch(error => {
126 store.dispatch({type: LoaderConstants.RECEIVE_RESPONSE, url : error.config.url});
127 errorResponseHandler(error.response);
Einav Weiss Keidard2f57942018-02-14 14:00:07 +0200128 return Promise.reject({responseJSON: error.response.data});
ilanap1965d162018-01-04 11:34:59 +0200129 });
AviZi280f8012017-06-09 02:39:56 +0300130 }
ilanap1965d162018-01-04 11:34:59 +0200131
AviZi280f8012017-06-09 02:39:56 +0300132 }
133
ilanap1965d162018-01-04 11:34:59 +0200134 fetch(url, options) {
135 return this.handleRequest(url, GET, options);
136 }
137
138 get(url, options) {
139 return this.fetch(url, options);
140 }
141
142 post(url, data, options) {
143 return this.handleRequest(url, POST, options, data);
144 }
145
146 put(url, data, options) {
147 return this.handleRequest(url, PUT, options, data);
148 }
149
150 destroy(url, options) {
151 return this.handleRequest(url, DELETE, options);
152 }
153
154
155
AviZi280f8012017-06-09 02:39:56 +0300156}
157
ilanap1965d162018-01-04 11:34:59 +0200158const instance = new RestAPIUtil();
AviZi280f8012017-06-09 02:39:56 +0300159
Michael Landoefa037d2017-02-19 12:57:33 +0200160
Michael Landoefa037d2017-02-19 12:57:33 +0200161
AviZi280f8012017-06-09 02:39:56 +0300162export default instance;