Updated Sparky to add ECOMP functionality Browse, Specialized Search, BYOQ, and the...
[aai/sparky-fe.git] / src / utils / CommonAPIService.js
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import axios from 'axios';
22 import {GlobalExtConstants} from './GlobalExtConstants.js';
23
24 const commonApi = (settings, path, httpMethodType, reqPayload, stubPath, overrideDomain, specialCase, additionalHeaders, noProxy) => {
25   let BASE_URL = GlobalExtConstants.BASE_URL;
26   const proxyConfig = {
27   //enter proxy details here for local
28     proxy : {
29       host: '',
30       port: ''
31     }
32   };
33
34   let SWITCH_URL = '';
35   let APERTURE_SERVICE = JSON.parse(sessionStorage.getItem(GlobalExtConstants.ENVIRONMENT + 'APERTURE_SERVICE'));
36   if(overrideDomain){
37     SWITCH_URL = overrideDomain + '/';
38     if(settings.ISTABULAR){
39       SWITCH_URL+= settings.TABULAR + '/'
40       + settings.PREFIX + '/'
41       + settings.APERTURE + '/'
42       + settings.TABULARVERSION + '/'
43       + path;
44     }else{
45       SWITCH_URL+= path;
46     }
47   }else if(APERTURE_SERVICE && (settings.ISAPERTURE !== undefined)){
48     let baseURL = (settings.NODESERVER) ? 'https://'+ settings.NODESERVER : BASE_URL;
49     SWITCH_URL = baseURL + '/';
50     if(!noProxy){
51         SWITCH_URL += settings.PROXY + '/';
52     }
53     SWITCH_URL += settings.PREFIX + '/'
54     + settings.APERTURE + '/';
55     if(settings.ISAPERTURE && settings.APERTURE_SERVICENAME !== undefined){
56       SWITCH_URL += settings.VERSION + '/'
57       + settings.APERTURE_SERVICENAME;
58     }else if(settings.ISAPERTURE){
59       SWITCH_URL += settings.VERSION + '/';
60     }
61     SWITCH_URL += path;    
62   }else if(settings.NODESERVER){
63     SWITCH_URL = 'https://'
64       + settings.NODESERVER + '/';
65       if(!noProxy){
66           SWITCH_URL += settings.PROXY + '/';
67       }
68       SWITCH_URL += settings.PREFIX + '/';
69       if(specialCase){
70          SWITCH_URL += specialCase + '/';
71       }
72      SWITCH_URL += settings.VERSION + '/'
73      + path;
74   }else{
75     SWITCH_URL = BASE_URL + '/';
76       if(!noProxy){
77           SWITCH_URL += settings.PROXY + '/';
78       }
79       SWITCH_URL += settings.PREFIX + '/';
80     if(specialCase){
81        SWITCH_URL += specialCase + '/';
82     }
83     SWITCH_URL += settings.VERSION + '/'
84          + path;
85   }
86   console.log('Making call to the backend >>>>>>>>>>>', SWITCH_URL);
87
88   var headers = {'Content-Type' : 'application/json','Access-Control-Allow-Origin' : '*','X-FromAppId':'AAI-UI', 'X-TransactionId' : 'AAI-UI', 'Accept':'application/json'};
89   if(additionalHeaders){
90     for(var i = 0; i < additionalHeaders.length; i++){
91         if(additionalHeaders[i].name && additionalHeaders[i].value){
92             headers[additionalHeaders[i].name] = additionalHeaders[i].value;
93         }else{
94             console.log("CommonAPIService :: Additional headers passed in are not in teh proper format: "+ JSON.stringify(additionalHeaders));
95         }
96     }
97   }
98   console.log("HEADER VALUES: "+ headers);
99   if(settings.USESTUBS){
100     return new Promise((resolve, reject) => {
101           var responseObj = {};
102           responseObj.data = require('app/assets/stubs/' + stubPath + '.json');
103           responseObj.status = 200;
104           responseObj.headers = [];
105           if(responseObj.data && responseObj.data.results){
106             responseObj.headers['total-results'] =  Object.keys(responseObj.data.results).length;
107             resolve(responseObj);
108           }else if(responseObj.data && ['BYOQPersonalQueries','BYOQCommunityQueries','BYOQPublicQueries','ConvertQueryToTree','SingleTransactionEdit'].indexOf(stubPath) > -1 ){
109             resolve(responseObj);
110           }else if(responseObj.data){
111             responseObj.headers['total-results'] =  0;
112             reject(responseObj.data);
113           }else{
114             reject('Error');
115           }
116       })
117   }else if (['PUT','PATCH','DELETE','POST'].indexOf(httpMethodType) > -1 && reqPayload !== null) {
118     return axios({ method: httpMethodType,
119       url: SWITCH_URL,
120       data: reqPayload,
121       headers: headers });
122   }else{
123     if(settings.NODESERVER){
124         return axios.get(SWITCH_URL, proxyConfig);
125     }else{
126         return axios.get(SWITCH_URL);
127     }
128   }
129 };
130
131 export default commonApi;