summaryrefslogtreecommitdiffstats
path: root/src/utils/CommonAPIService.js
blob: c7aea455ebf3330865f19672c572f458b477a305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

import axios from 'axios';
import {GlobalExtConstants} from './GlobalExtConstants.js';

const commonApi = (settings, path, httpMethodType, reqPayload, stubPath, overrideDomain, specialCase, additionalHeaders, noProxy) => {
  let BASE_URL = GlobalExtConstants.BASE_URL;
  const proxyConfig = {
  //enter proxy details here for local
    proxy : {
      host: '',
      port: ''
    }
  };

  let SWITCH_URL = '';
  let APERTURE_SERVICE = JSON.parse(sessionStorage.getItem(GlobalExtConstants.ENVIRONMENT + 'APERTURE_SERVICE'));
  if(overrideDomain){
    SWITCH_URL = overrideDomain + '/';
    if(settings.ISTABULAR){
      SWITCH_URL+= settings.TABULAR + '/'
      + settings.PREFIX + '/'
      + settings.APERTURE + '/'
      + settings.TABULARVERSION + '/'
      + path;
    }else{
      SWITCH_URL+= path;
    }
  }else if(APERTURE_SERVICE && (settings.ISAPERTURE !== undefined)){
    let baseURL = (settings.NODESERVER) ? 'https://'+ settings.NODESERVER : BASE_URL;
    SWITCH_URL = baseURL + '/';
    if(!noProxy){
        SWITCH_URL += settings.PROXY + '/';
    }
    SWITCH_URL += settings.PREFIX + '/'
    + settings.APERTURE + '/';
    if(settings.ISAPERTURE && settings.APERTURE_SERVICENAME !== undefined){
      SWITCH_URL += settings.VERSION + '/'
      + settings.APERTURE_SERVICENAME;
    }else if(settings.ISAPERTURE){
      SWITCH_URL += settings.VERSION + '/';
    }
    SWITCH_URL += path;    
  }else if(settings.NODESERVER){
    SWITCH_URL = 'https://'
      + settings.NODESERVER + '/';
      if(!noProxy){
          SWITCH_URL += settings.PROXY + '/';
      }
      SWITCH_URL += settings.PREFIX + '/';
      if(specialCase){
         SWITCH_URL += specialCase + '/';
      }
     SWITCH_URL += settings.VERSION + '/'
     + path;
  }else{
    SWITCH_URL = BASE_URL + '/';
      if(!noProxy){
          SWITCH_URL += settings.PROXY + '/';
      }
      SWITCH_URL += settings.PREFIX + '/';
    if(specialCase){
       SWITCH_URL += specialCase + '/';
    }
    SWITCH_URL += settings.VERSION + '/'
         + path;
  }
  console.log('Making call to the backend >>>>>>>>>>>', SWITCH_URL);

  var headers = {'Content-Type' : 'application/json','Access-Control-Allow-Origin' : '*','X-FromAppId':'AAI-UI', 'X-TransactionId' : 'AAI-UI', 'Accept':'application/json'};
  if(additionalHeaders){
    for(var i = 0; i < additionalHeaders.length; i++){
        if(additionalHeaders[i].name && additionalHeaders[i].value){
            headers[additionalHeaders[i].name] = additionalHeaders[i].value;
        }else{
            console.log("CommonAPIService :: Additional headers passed in are not in teh proper format: "+ JSON.stringify(additionalHeaders));
        }
    }
  }
  console.log("HEADER VALUES: "+ headers);
  if(settings.USESTUBS){
    return new Promise((resolve, reject) => {
          var responseObj = {};
          responseObj.data = require('app/assets/stubs/' + stubPath + '.json');
          responseObj.status = 200;
          responseObj.headers = [];
          if(responseObj.data && responseObj.data.results){
            responseObj.headers['total-results'] =  Object.keys(responseObj.data.results).length;
            resolve(responseObj);
          }else if(responseObj.data && ['BYOQPersonalQueries','BYOQCommunityQueries','BYOQPublicQueries','ConvertQueryToTree','SingleTransactionEdit'].indexOf(stubPath) > -1 ){
            resolve(responseObj);
          }else if(responseObj.data){
            responseObj.headers['total-results'] =  0;
            reject(responseObj.data);
          }else{
            reject('Error');
          }
      })
  }else if (['PUT','PATCH','DELETE','POST'].indexOf(httpMethodType) > -1 && reqPayload !== null) {
    return axios({ method: httpMethodType,
      url: SWITCH_URL,
      data: reqPayload,
      headers: headers });
  }else{
    if(settings.NODESERVER){
        return axios.get(SWITCH_URL, proxyConfig);
    }else{
        return axios.get(SWITCH_URL);
    }
  }
};

export default commonApi;