ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016-2017 European Support Limited |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 3 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 | * |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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, |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 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. |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 15 | */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | import uuid from 'uuid-js'; |
| 17 | import md5 from 'md5'; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 18 | import axios from 'axios'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 19 | |
| 20 | import store from 'sdc-app/AppStore.js'; |
| 21 | import {actionTypes as LoaderConstants} from 'nfvo-components/loader/LoaderConstants.js'; |
| 22 | import Configuration from 'sdc-app/config/Configuration.js'; |
| 23 | import errorResponseHandler from './ErrorResponseHandler.js'; |
| 24 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 25 | //methods |
| 26 | const GET = 'GET'; |
| 27 | const POST = 'POST'; |
| 28 | const PUT = 'PUT'; |
| 29 | const DELETE = 'DELETE'; |
| 30 | |
| 31 | // content-types |
| 32 | const APPLICATION_JSON = 'application/json'; |
| 33 | const MULTIPART_FORM_DATA = 'multipart/form-data'; |
| 34 | |
| 35 | const BINARY = 'binary'; |
| 36 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 37 | const AUTHORIZATION_HEADER = 'X-AUTH-TOKEN'; |
| 38 | const STORAGE_AUTH_KEY = 'sdc-auth-token'; |
| 39 | const REQUEST_ID_HEADER = 'X-ECOMP-RequestID'; |
| 40 | const CONTENT_MD5_HEADER = 'Content-MD5'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 41 | |
| 42 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 43 | function applySecurity(options, data) { |
| 44 | let headers = options.headers || (options.headers = {}); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 45 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 46 | let authToken = localStorage.getItem(STORAGE_AUTH_KEY); |
| 47 | if (authToken) { |
| 48 | headers[AUTHORIZATION_HEADER] = authToken; |
| 49 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 50 | |
ilanap | 785dc1e | 2018-01-08 15:50:18 +0200 | [diff] [blame] | 51 | let catalogApiHeaders = Configuration.get('CatalogApiHeaders'), |
| 52 | catalogUidHeader = catalogApiHeaders && catalogApiHeaders.userId; |
| 53 | if (catalogUidHeader) { |
| 54 | headers[catalogUidHeader.name] = catalogUidHeader.value; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | headers[REQUEST_ID_HEADER] = uuid.create().toString(); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 58 | if (options.md5) { |
| 59 | let headers = options.headers; |
| 60 | headers[CONTENT_MD5_HEADER] = window.btoa(md5(JSON.stringify(data)).toLowerCase()); |
| 61 | } |
| 62 | } |
| 63 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 64 | |
| 65 | function handleSuccess(responseHeaders, requestHeaders) { |
| 66 | let authToken = responseHeaders[AUTHORIZATION_HEADER]; |
| 67 | let prevToken = requestHeaders && requestHeaders[AUTHORIZATION_HEADER]; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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 | } |
| 75 | } |
| 76 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 77 | class RestAPIUtil { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 78 | handleRequest(url, type, options = {}, data){ |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 79 | 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; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 89 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 90 | 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 104 | }; |
| 105 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 106 | 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); |
| 128 | throw {responseJSON: error.response.data}; |
| 129 | }); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 130 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 131 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 132 | } |
| 133 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 134 | 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 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 156 | } |
| 157 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 158 | const instance = new RestAPIUtil(); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 159 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 160 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 161 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 162 | export default instance; |