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'; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 30 | const BINARY = 'binary'; |
| 31 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 32 | const AUTHORIZATION_HEADER = 'X-AUTH-TOKEN'; |
| 33 | const STORAGE_AUTH_KEY = 'sdc-auth-token'; |
| 34 | const REQUEST_ID_HEADER = 'X-ECOMP-RequestID'; |
| 35 | const CONTENT_MD5_HEADER = 'Content-MD5'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 36 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 37 | function applySecurity(options, data) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 38 | let headers = options.headers || (options.headers = {}); |
Murali-P | 72d62fb | 2018-03-29 17:46:39 +0530 | [diff] [blame] | 39 | if (options.isAnonymous) { |
| 40 | return; |
| 41 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 42 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 43 | let authToken = localStorage.getItem(STORAGE_AUTH_KEY); |
| 44 | if (authToken) { |
| 45 | headers[AUTHORIZATION_HEADER] = authToken; |
| 46 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 47 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 48 | let catalogApiHeaders = Configuration.get('CatalogApiHeaders'), |
| 49 | catalogUidHeader = catalogApiHeaders && catalogApiHeaders.userId; |
| 50 | if (catalogUidHeader) { |
| 51 | headers[catalogUidHeader.name] = catalogUidHeader.value; |
| 52 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 53 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 54 | headers[REQUEST_ID_HEADER] = uuid.create().toString(); |
| 55 | if (options.md5) { |
| 56 | let headers = options.headers; |
| 57 | headers[CONTENT_MD5_HEADER] = window.btoa( |
| 58 | md5(JSON.stringify(data)).toLowerCase() |
| 59 | ); |
| 60 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 61 | } |
| 62 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 63 | function handleSuccess(responseHeaders, requestHeaders) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 64 | let authToken = responseHeaders[AUTHORIZATION_HEADER]; |
| 65 | let prevToken = requestHeaders && requestHeaders[AUTHORIZATION_HEADER]; |
| 66 | if (authToken && authToken !== prevToken) { |
| 67 | if (authToken === 'null') { |
| 68 | localStorage.removeItem(STORAGE_AUTH_KEY); |
| 69 | } else { |
| 70 | localStorage.setItem(STORAGE_AUTH_KEY, authToken); |
| 71 | } |
| 72 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 73 | } |
| 74 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 75 | class RestAPIUtil { |
Yarin Dekel | bc20534 | 2018-10-16 10:42:49 +0300 | [diff] [blame] | 76 | handleRequest(url, type, options = {}, data = {}) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 77 | applySecurity(options, data); |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 78 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 79 | let config = { |
| 80 | method: type, |
| 81 | url: url, |
| 82 | headers: options.headers, |
| 83 | data: data |
| 84 | }; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 85 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 86 | store.dispatch({ type: LoaderConstants.SEND_REQUEST, url: url }); |
| 87 | if (options.dataType === BINARY) { |
| 88 | config.responseType = 'arraybuffer'; |
| 89 | return axios(config) |
| 90 | .then(result => { |
| 91 | store.dispatch({ |
| 92 | type: LoaderConstants.RECEIVE_RESPONSE, |
| 93 | url: result.config.url |
| 94 | }); |
| 95 | return { |
| 96 | blob: new Blob([result.data]), |
| 97 | headers: result.headers |
| 98 | }; |
| 99 | }) |
| 100 | .catch(error => { |
| 101 | store.dispatch({ |
| 102 | type: LoaderConstants.RECEIVE_RESPONSE, |
| 103 | url: error.config.url |
| 104 | }); |
| 105 | errorResponseHandler(error.response); |
| 106 | }); |
| 107 | } else { |
| 108 | return axios(config) |
| 109 | .then(result => { |
| 110 | store.dispatch({ |
| 111 | type: LoaderConstants.RECEIVE_RESPONSE, |
| 112 | url: result.config.url |
| 113 | }); |
| 114 | handleSuccess(result.headers, result.config.headers); |
| 115 | return result.data; |
| 116 | }) |
| 117 | .catch(error => { |
| 118 | store.dispatch({ |
| 119 | type: LoaderConstants.RECEIVE_RESPONSE, |
| 120 | url: error.config.url |
| 121 | }); |
| 122 | errorResponseHandler(error.response); |
| 123 | return Promise.reject({ |
| 124 | responseJSON: error.response.data |
| 125 | }); |
| 126 | }); |
| 127 | } |
| 128 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 129 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 130 | fetch(url, options) { |
| 131 | return this.handleRequest(url, GET, options); |
| 132 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 133 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 134 | get(url, options) { |
| 135 | return this.fetch(url, options); |
| 136 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 137 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 138 | post(url, data, options) { |
| 139 | return this.handleRequest(url, POST, options, data); |
| 140 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 141 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 142 | put(url, data, options) { |
| 143 | return this.handleRequest(url, PUT, options, data); |
| 144 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 145 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 146 | destroy(url, options) { |
| 147 | return this.handleRequest(url, DELETE, options); |
| 148 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 149 | } |
| 150 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 151 | const instance = new RestAPIUtil(); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 152 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 153 | export default instance; |