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 = {}); |
Murali-P | 72d62fb | 2018-03-29 17:46:39 +0530 | [diff] [blame] | 44 | if (options.isAnonymous) { |
| 45 | return; |
| 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 authToken = localStorage.getItem(STORAGE_AUTH_KEY); |
| 49 | if (authToken) { |
| 50 | headers[AUTHORIZATION_HEADER] = authToken; |
| 51 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 52 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 53 | let catalogApiHeaders = Configuration.get('CatalogApiHeaders'), |
| 54 | catalogUidHeader = catalogApiHeaders && catalogApiHeaders.userId; |
| 55 | if (catalogUidHeader) { |
| 56 | headers[catalogUidHeader.name] = catalogUidHeader.value; |
| 57 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 58 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 59 | headers[REQUEST_ID_HEADER] = uuid.create().toString(); |
| 60 | if (options.md5) { |
| 61 | let headers = options.headers; |
| 62 | headers[CONTENT_MD5_HEADER] = window.btoa( |
| 63 | md5(JSON.stringify(data)).toLowerCase() |
| 64 | ); |
| 65 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 66 | } |
| 67 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 68 | function handleSuccess(responseHeaders, requestHeaders) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 69 | let authToken = responseHeaders[AUTHORIZATION_HEADER]; |
| 70 | let prevToken = requestHeaders && requestHeaders[AUTHORIZATION_HEADER]; |
| 71 | if (authToken && authToken !== prevToken) { |
| 72 | if (authToken === 'null') { |
| 73 | localStorage.removeItem(STORAGE_AUTH_KEY); |
| 74 | } else { |
| 75 | localStorage.setItem(STORAGE_AUTH_KEY, authToken); |
| 76 | } |
| 77 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 78 | } |
| 79 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 80 | class RestAPIUtil { |
| 81 | handleRequest(url, type, options = {}, data) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 82 | applySecurity(options, data); |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 83 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 84 | // TODO see ig necessary or in transformrequest funtion |
| 85 | if (type === POST || type === PUT) { |
| 86 | if (data instanceof FormData) { |
| 87 | options.headers.contentType = MULTIPART_FORM_DATA; |
| 88 | } else { |
| 89 | options.headers.contentType = APPLICATION_JSON; |
| 90 | // config.data = JSON.stringify(data); |
| 91 | } |
| 92 | } else { |
| 93 | data = null; |
| 94 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 95 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 96 | let config = { |
| 97 | method: type, |
| 98 | url: url, |
| 99 | headers: options.headers, |
| 100 | data: data |
| 101 | }; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 102 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 103 | store.dispatch({ type: LoaderConstants.SEND_REQUEST, url: url }); |
| 104 | if (options.dataType === BINARY) { |
| 105 | config.responseType = 'arraybuffer'; |
| 106 | return axios(config) |
| 107 | .then(result => { |
| 108 | store.dispatch({ |
| 109 | type: LoaderConstants.RECEIVE_RESPONSE, |
| 110 | url: result.config.url |
| 111 | }); |
| 112 | return { |
| 113 | blob: new Blob([result.data]), |
| 114 | headers: result.headers |
| 115 | }; |
| 116 | }) |
| 117 | .catch(error => { |
| 118 | store.dispatch({ |
| 119 | type: LoaderConstants.RECEIVE_RESPONSE, |
| 120 | url: error.config.url |
| 121 | }); |
| 122 | errorResponseHandler(error.response); |
| 123 | }); |
| 124 | } else { |
| 125 | return axios(config) |
| 126 | .then(result => { |
| 127 | store.dispatch({ |
| 128 | type: LoaderConstants.RECEIVE_RESPONSE, |
| 129 | url: result.config.url |
| 130 | }); |
| 131 | handleSuccess(result.headers, result.config.headers); |
| 132 | return result.data; |
| 133 | }) |
| 134 | .catch(error => { |
| 135 | store.dispatch({ |
| 136 | type: LoaderConstants.RECEIVE_RESPONSE, |
| 137 | url: error.config.url |
| 138 | }); |
| 139 | errorResponseHandler(error.response); |
| 140 | return Promise.reject({ |
| 141 | responseJSON: error.response.data |
| 142 | }); |
| 143 | }); |
| 144 | } |
| 145 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 146 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 147 | fetch(url, options) { |
| 148 | return this.handleRequest(url, GET, options); |
| 149 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 150 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 151 | get(url, options) { |
| 152 | return this.fetch(url, options); |
| 153 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 154 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 155 | post(url, data, options) { |
| 156 | return this.handleRequest(url, POST, options, data); |
| 157 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 158 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 159 | put(url, data, options) { |
| 160 | return this.handleRequest(url, PUT, options, data); |
| 161 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 162 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 163 | destroy(url, options) { |
| 164 | return this.handleRequest(url, DELETE, options); |
| 165 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 166 | } |
| 167 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 168 | const instance = new RestAPIUtil(); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 169 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 170 | export default instance; |