ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 1 | /* |
Einav Weiss Keidar | d2f5794 | 2018-02-14 14:00:07 +0200 | [diff] [blame] | 2 | * Copyright © 2016-2018 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'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 21 | import { actionTypes as LoaderConstants } from 'nfvo-components/loader/LoaderConstants.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 42 | function applySecurity(options, data) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 43 | let headers = options.headers || (options.headers = {}); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 44 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 45 | let authToken = localStorage.getItem(STORAGE_AUTH_KEY); |
| 46 | if (authToken) { |
| 47 | headers[AUTHORIZATION_HEADER] = authToken; |
| 48 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 49 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 50 | let catalogApiHeaders = Configuration.get('CatalogApiHeaders'), |
| 51 | catalogUidHeader = catalogApiHeaders && catalogApiHeaders.userId; |
| 52 | if (catalogUidHeader) { |
| 53 | headers[catalogUidHeader.name] = catalogUidHeader.value; |
| 54 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 55 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 56 | headers[REQUEST_ID_HEADER] = uuid.create().toString(); |
| 57 | if (options.md5) { |
| 58 | let headers = options.headers; |
| 59 | headers[CONTENT_MD5_HEADER] = window.btoa( |
| 60 | md5(JSON.stringify(data)).toLowerCase() |
| 61 | ); |
| 62 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 63 | } |
| 64 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 65 | function handleSuccess(responseHeaders, requestHeaders) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 66 | let authToken = responseHeaders[AUTHORIZATION_HEADER]; |
| 67 | let prevToken = requestHeaders && requestHeaders[AUTHORIZATION_HEADER]; |
| 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 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 75 | } |
| 76 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 77 | class RestAPIUtil { |
| 78 | handleRequest(url, type, options = {}, data) { |
| 79 | if (DEBUG) { |
| 80 | console.log('axios --> Making REST call (' + type + '): ' + url); |
| 81 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 82 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 83 | applySecurity(options, data); |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 84 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 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; |
| 89 | } else { |
| 90 | options.headers.contentType = APPLICATION_JSON; |
| 91 | // config.data = JSON.stringify(data); |
| 92 | } |
| 93 | } else { |
| 94 | data = null; |
| 95 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 96 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 97 | let config = { |
| 98 | method: type, |
| 99 | url: url, |
| 100 | headers: options.headers, |
| 101 | data: data |
| 102 | }; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 103 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 104 | store.dispatch({ type: LoaderConstants.SEND_REQUEST, url: url }); |
| 105 | if (options.dataType === BINARY) { |
| 106 | config.responseType = 'arraybuffer'; |
| 107 | return axios(config) |
| 108 | .then(result => { |
| 109 | store.dispatch({ |
| 110 | type: LoaderConstants.RECEIVE_RESPONSE, |
| 111 | url: result.config.url |
| 112 | }); |
| 113 | return { |
| 114 | blob: new Blob([result.data]), |
| 115 | headers: result.headers |
| 116 | }; |
| 117 | }) |
| 118 | .catch(error => { |
| 119 | store.dispatch({ |
| 120 | type: LoaderConstants.RECEIVE_RESPONSE, |
| 121 | url: error.config.url |
| 122 | }); |
| 123 | errorResponseHandler(error.response); |
| 124 | }); |
| 125 | } else { |
| 126 | return axios(config) |
| 127 | .then(result => { |
| 128 | store.dispatch({ |
| 129 | type: LoaderConstants.RECEIVE_RESPONSE, |
| 130 | url: result.config.url |
| 131 | }); |
| 132 | handleSuccess(result.headers, result.config.headers); |
| 133 | return result.data; |
| 134 | }) |
| 135 | .catch(error => { |
| 136 | store.dispatch({ |
| 137 | type: LoaderConstants.RECEIVE_RESPONSE, |
| 138 | url: error.config.url |
| 139 | }); |
| 140 | errorResponseHandler(error.response); |
| 141 | return Promise.reject({ |
| 142 | responseJSON: error.response.data |
| 143 | }); |
| 144 | }); |
| 145 | } |
| 146 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 147 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 148 | fetch(url, options) { |
| 149 | return this.handleRequest(url, GET, options); |
| 150 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 151 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 152 | get(url, options) { |
| 153 | return this.fetch(url, options); |
| 154 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 155 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 156 | post(url, data, options) { |
| 157 | return this.handleRequest(url, POST, options, data); |
| 158 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 159 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 160 | put(url, data, options) { |
| 161 | return this.handleRequest(url, PUT, options, data); |
| 162 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 163 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 164 | destroy(url, options) { |
| 165 | return this.handleRequest(url, DELETE, options); |
| 166 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 167 | } |
| 168 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 169 | const instance = new RestAPIUtil(); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 170 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 171 | export default instance; |