function getApis()

in core/routemgmt/common/utils.js [278:344]


function getApis(gwInfo, tenantId, bpOrApiName) {
  var qsBasepath = { 'filter[where][basePath]' : bpOrApiName };
  var qsApiName = { 'filter[where][name]' : bpOrApiName };
  var qs;
  if (bpOrApiName) {
    if (bpOrApiName.indexOf('/') !== 0) {
      console.log('getApis: querying APIs based on api name');
      qs = qsApiName;
    } else {
      console.log('getApis: querying APIs based on basepath');
      qs = qsBasepath;
    }
  }
  var options = {
    followAllRedirects: true,
    url: gwInfo.gwUrl+'/tenants/'+tenantId+'/apis',
    headers: {
      'Accept': 'application/json'
    },
  };
  if (qs) {
    options.qs = qs;
  }
  if (gwInfo.gwAuth) {
    options.headers.Authorization = 'Basic ' + gwInfo.gwAuth;
  }
  console.log('getApis: request: '+JSON.stringify(options));

  return new Promise(function(resolve, reject) {
    request.get(options, function(error, response, body) {
      var statusCode = response ? response.statusCode : undefined;
      console.log('getApis: response status: '+ statusCode);
      if (error) console.error('Warning: getApis request failed: '+utils2.makeJsonString(error));
      if (body) console.log('getApis: response body: '+utils2.makeJsonString(body));
      if (error) {
        console.error('getApis: Unable to obtain API(s) from the API Gateway');
        reject('Unable to obtain API(s) from the API Gateway: '+utils2.makeJsonString(error));
      } else if (statusCode != 200) {
        if (body) {
          var errMsg = JSON.stringify(body);
          if (body.error && body.error.message) errMsg = body.error.message;
          reject('Unable to obtain API(s) from the API Gateway (status code '+statusCode+'): '+ errMsg);
        } else {
          reject('Unable to obtain API(s) from the API Gateway: Response failure code: '+statusCode);
        }
      } else {
        if (body) {
          try {
            var bodyJson = JSON.parse(body);
            if (Array.isArray(bodyJson)) {
              resolve(bodyJson);
            } else {
              console.error('getApis: Invalid API GW response body; a JSON array was not returned');
              resolve( [] );
            }
          } catch(e) {
            console.error('getApis: Invalid API GW response body; JSON.parse() failure: '+e);
            reject('Invalid API Gateway response: '+e);
          }
        } else {
          console.log('getApis: No APIs found');
          resolve( [] );
        }
      }
    });
  });
}