diff options
Diffstat (limited to 'openecomp-ui/src/nfvo-utils/RestAPIUtil.js')
-rw-r--r-- | openecomp-ui/src/nfvo-utils/RestAPIUtil.js | 231 |
1 files changed, 120 insertions, 111 deletions
diff --git a/openecomp-ui/src/nfvo-utils/RestAPIUtil.js b/openecomp-ui/src/nfvo-utils/RestAPIUtil.js index 5b07986390..1a5817de66 100644 --- a/openecomp-ui/src/nfvo-utils/RestAPIUtil.js +++ b/openecomp-ui/src/nfvo-utils/RestAPIUtil.js @@ -18,7 +18,7 @@ import md5 from 'md5'; import axios from 'axios'; import store from 'sdc-app/AppStore.js'; -import {actionTypes as LoaderConstants} from 'nfvo-components/loader/LoaderConstants.js'; +import { actionTypes as LoaderConstants } from 'nfvo-components/loader/LoaderConstants.js'; import Configuration from 'sdc-app/config/Configuration.js'; import errorResponseHandler from './ErrorResponseHandler.js'; @@ -39,124 +39,133 @@ const STORAGE_AUTH_KEY = 'sdc-auth-token'; const REQUEST_ID_HEADER = 'X-ECOMP-RequestID'; const CONTENT_MD5_HEADER = 'Content-MD5'; - function applySecurity(options, data) { - let headers = options.headers || (options.headers = {}); - - let authToken = localStorage.getItem(STORAGE_AUTH_KEY); - if (authToken) { - headers[AUTHORIZATION_HEADER] = authToken; - } - - let catalogApiHeaders = Configuration.get('CatalogApiHeaders'), - catalogUidHeader = catalogApiHeaders && catalogApiHeaders.userId; - if (catalogUidHeader) { - headers[catalogUidHeader.name] = catalogUidHeader.value; - } - - headers[REQUEST_ID_HEADER] = uuid.create().toString(); - if (options.md5) { - let headers = options.headers; - headers[CONTENT_MD5_HEADER] = window.btoa(md5(JSON.stringify(data)).toLowerCase()); - } + let headers = options.headers || (options.headers = {}); + + let authToken = localStorage.getItem(STORAGE_AUTH_KEY); + if (authToken) { + headers[AUTHORIZATION_HEADER] = authToken; + } + + let catalogApiHeaders = Configuration.get('CatalogApiHeaders'), + catalogUidHeader = catalogApiHeaders && catalogApiHeaders.userId; + if (catalogUidHeader) { + headers[catalogUidHeader.name] = catalogUidHeader.value; + } + + headers[REQUEST_ID_HEADER] = uuid.create().toString(); + if (options.md5) { + let headers = options.headers; + headers[CONTENT_MD5_HEADER] = window.btoa( + md5(JSON.stringify(data)).toLowerCase() + ); + } } - function handleSuccess(responseHeaders, requestHeaders) { - let authToken = responseHeaders[AUTHORIZATION_HEADER]; - let prevToken = requestHeaders && requestHeaders[AUTHORIZATION_HEADER]; - if (authToken && authToken !== prevToken) { - if (authToken === 'null') { - localStorage.removeItem(STORAGE_AUTH_KEY); - } else { - localStorage.setItem(STORAGE_AUTH_KEY, authToken); - } - } + let authToken = responseHeaders[AUTHORIZATION_HEADER]; + let prevToken = requestHeaders && requestHeaders[AUTHORIZATION_HEADER]; + if (authToken && authToken !== prevToken) { + if (authToken === 'null') { + localStorage.removeItem(STORAGE_AUTH_KEY); + } else { + localStorage.setItem(STORAGE_AUTH_KEY, authToken); + } + } } -class RestAPIUtil { - handleRequest(url, type, options = {}, data){ - if (DEBUG) { - console.log('axios --> Making REST call (' + type + '): ' + url); - } - - applySecurity(options, data); - - // TODO see ig necessary or in transformrequest funtion - if (type === POST || type === PUT) { - if (data instanceof FormData) { - options.headers.contentType = MULTIPART_FORM_DATA; - } - else { - options.headers.contentType = APPLICATION_JSON; -// config.data = JSON.stringify(data); - } - - } else { - data = null; - } - - let config = { - method: type, - url: url, - headers : options.headers, - data : data - }; - - store.dispatch({type: LoaderConstants.SEND_REQUEST, url: url}); - if (options.dataType === BINARY) { - config.responseType = 'arraybuffer'; - return axios(config). - then(result => { - store.dispatch({type: LoaderConstants.RECEIVE_RESPONSE, url : result.config.url}); - return ({ - blob : new Blob([result.data] ), - headers : result.headers - }); - }).catch(error => { - store.dispatch({type: LoaderConstants.RECEIVE_RESPONSE, url : error.config.url}); - errorResponseHandler(error.response); }); - } else { - return axios(config). - then(result => { - store.dispatch({type: LoaderConstants.RECEIVE_RESPONSE, url : result.config.url}); - handleSuccess(result.headers, result.config.headers); - return result.data; - }).catch(error => { - store.dispatch({type: LoaderConstants.RECEIVE_RESPONSE, url : error.config.url}); - errorResponseHandler(error.response); - return Promise.reject({responseJSON: error.response.data}); - }); - } - - } - - fetch(url, options) { - return this.handleRequest(url, GET, options); - } - - get(url, options) { - return this.fetch(url, options); - } - - post(url, data, options) { - return this.handleRequest(url, POST, options, data); - } - - put(url, data, options) { - return this.handleRequest(url, PUT, options, data); - } - - destroy(url, options) { - return this.handleRequest(url, DELETE, options); - } - - - +class RestAPIUtil { + handleRequest(url, type, options = {}, data) { + if (DEBUG) { + console.log('axios --> Making REST call (' + type + '): ' + url); + } + + applySecurity(options, data); + + // TODO see ig necessary or in transformrequest funtion + if (type === POST || type === PUT) { + if (data instanceof FormData) { + options.headers.contentType = MULTIPART_FORM_DATA; + } else { + options.headers.contentType = APPLICATION_JSON; + // config.data = JSON.stringify(data); + } + } else { + data = null; + } + + let config = { + method: type, + url: url, + headers: options.headers, + data: data + }; + + store.dispatch({ type: LoaderConstants.SEND_REQUEST, url: url }); + if (options.dataType === BINARY) { + config.responseType = 'arraybuffer'; + return axios(config) + .then(result => { + store.dispatch({ + type: LoaderConstants.RECEIVE_RESPONSE, + url: result.config.url + }); + return { + blob: new Blob([result.data]), + headers: result.headers + }; + }) + .catch(error => { + store.dispatch({ + type: LoaderConstants.RECEIVE_RESPONSE, + url: error.config.url + }); + errorResponseHandler(error.response); + }); + } else { + return axios(config) + .then(result => { + store.dispatch({ + type: LoaderConstants.RECEIVE_RESPONSE, + url: result.config.url + }); + handleSuccess(result.headers, result.config.headers); + return result.data; + }) + .catch(error => { + store.dispatch({ + type: LoaderConstants.RECEIVE_RESPONSE, + url: error.config.url + }); + errorResponseHandler(error.response); + return Promise.reject({ + responseJSON: error.response.data + }); + }); + } + } + + fetch(url, options) { + return this.handleRequest(url, GET, options); + } + + get(url, options) { + return this.fetch(url, options); + } + + post(url, data, options) { + return this.handleRequest(url, POST, options, data); + } + + put(url, data, options) { + return this.handleRequest(url, PUT, options, data); + } + + destroy(url, options) { + return this.handleRequest(url, DELETE, options); + } } const instance = new RestAPIUtil(); - - export default instance; |