ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 1 | /* |
Malek | e6c3c72 | 2018-10-16 16:44:41 +0300 | [diff] [blame] | 2 | * 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 17 | import uuid from 'uuid-js'; |
| 18 | import md5 from 'md5'; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 19 | import axios from 'axios'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 20 | |
| 21 | import store from 'sdc-app/AppStore.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 22 | import { actionTypes as LoaderConstants } from 'nfvo-components/loader/LoaderConstants.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 23 | import Configuration from 'sdc-app/config/Configuration.js'; |
| 24 | import errorResponseHandler from './ErrorResponseHandler.js'; |
| 25 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 26 | //methods |
| 27 | const GET = 'GET'; |
| 28 | const POST = 'POST'; |
| 29 | const PUT = 'PUT'; |
| 30 | const DELETE = 'DELETE'; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 31 | const BINARY = 'binary'; |
| 32 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 33 | const AUTHORIZATION_HEADER = 'X-AUTH-TOKEN'; |
| 34 | const STORAGE_AUTH_KEY = 'sdc-auth-token'; |
| 35 | const REQUEST_ID_HEADER = 'X-ECOMP-RequestID'; |
| 36 | const CONTENT_MD5_HEADER = 'Content-MD5'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 37 | |
Malek | e6c3c72 | 2018-10-16 16:44:41 +0300 | [diff] [blame] | 38 | export function applySecurity(options, data) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 39 | let headers = options.headers || (options.headers = {}); |
Murali-P | 72d62fb | 2018-03-29 17:46:39 +0530 | [diff] [blame] | 40 | if (options.isAnonymous) { |
| 41 | return; |
| 42 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 43 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 44 | let authToken = localStorage.getItem(STORAGE_AUTH_KEY); |
| 45 | if (authToken) { |
| 46 | headers[AUTHORIZATION_HEADER] = authToken; |
| 47 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 48 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 49 | let catalogApiHeaders = Configuration.get('CatalogApiHeaders'), |
| 50 | catalogUidHeader = catalogApiHeaders && catalogApiHeaders.userId; |
| 51 | if (catalogUidHeader) { |
| 52 | headers[catalogUidHeader.name] = catalogUidHeader.value; |
| 53 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 54 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 55 | 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 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 62 | } |
| 63 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 64 | function handleSuccess(responseHeaders, requestHeaders) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 65 | 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 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 74 | } |
| 75 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 76 | class RestAPIUtil { |
Yarin Dekel | bc20534 | 2018-10-16 10:42:49 +0300 | [diff] [blame] | 77 | handleRequest(url, type, options = {}, data = {}) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 78 | applySecurity(options, data); |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 79 | |
andre.schmid | 4594cba | 2022-01-25 19:38:32 +0000 | [diff] [blame] | 80 | const config = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 81 | method: type, |
| 82 | url: url, |
| 83 | headers: options.headers, |
| 84 | data: data |
| 85 | }; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 86 | |
andre.schmid | 4594cba | 2022-01-25 19:38:32 +0000 | [diff] [blame] | 87 | if (options.validateStatus) { |
| 88 | config.validateStatus = options.validateStatus; |
| 89 | } |
| 90 | |
| 91 | if (options.onUploadProgress) { |
| 92 | config.onUploadProgress = options.onUploadProgress; |
| 93 | } |
| 94 | |
| 95 | if (!options.noLoading) { |
| 96 | store.dispatch({ type: LoaderConstants.SEND_REQUEST, url: url }); |
| 97 | } |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 98 | if (options.dataType === BINARY) { |
| 99 | config.responseType = 'arraybuffer'; |
| 100 | return axios(config) |
| 101 | .then(result => { |
andre.schmid | 4594cba | 2022-01-25 19:38:32 +0000 | [diff] [blame] | 102 | if (!options.noLoading) { |
| 103 | store.dispatch({ |
| 104 | type: LoaderConstants.RECEIVE_RESPONSE, |
| 105 | url: result.config.url |
| 106 | }); |
| 107 | } |
| 108 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 109 | return { |
| 110 | blob: new Blob([result.data]), |
| 111 | headers: result.headers |
| 112 | }; |
| 113 | }) |
| 114 | .catch(error => { |
andre.schmid | 4594cba | 2022-01-25 19:38:32 +0000 | [diff] [blame] | 115 | if (!options.noLoading) { |
| 116 | store.dispatch({ |
| 117 | type: LoaderConstants.RECEIVE_RESPONSE, |
| 118 | url: error.config.url |
| 119 | }); |
| 120 | } |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 121 | errorResponseHandler(error.response); |
| 122 | }); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 123 | } |
andre.schmid | 4594cba | 2022-01-25 19:38:32 +0000 | [diff] [blame] | 124 | return axios(config) |
| 125 | .then(result => { |
| 126 | store.dispatch({ |
| 127 | type: LoaderConstants.RECEIVE_RESPONSE, |
| 128 | url: result.config.url |
| 129 | }); |
| 130 | handleSuccess(result.headers, result.config.headers); |
| 131 | return result.data; |
| 132 | }) |
| 133 | .catch(error => { |
| 134 | store.dispatch({ |
| 135 | type: LoaderConstants.RECEIVE_RESPONSE, |
| 136 | url: error.config.url |
| 137 | }); |
| 138 | errorResponseHandler(error.response); |
| 139 | return Promise.reject({ |
| 140 | responseJSON: error.response.data |
| 141 | }); |
| 142 | }); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 143 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 144 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 145 | fetch(url, options) { |
| 146 | return this.handleRequest(url, GET, options); |
| 147 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 148 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 149 | get(url, options) { |
| 150 | return this.fetch(url, options); |
| 151 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 152 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 153 | post(url, data, options) { |
| 154 | return this.handleRequest(url, POST, options, data); |
| 155 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 156 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 157 | put(url, data, options) { |
| 158 | return this.handleRequest(url, PUT, options, data); |
| 159 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 160 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 161 | destroy(url, options) { |
| 162 | return this.handleRequest(url, DELETE, options); |
| 163 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 164 | } |
| 165 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 166 | const instance = new RestAPIUtil(); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 167 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 168 | export default instance; |