function getApis()

in core/routemgmt/common/apigw-utils.js [290:355]


function getApis(gwInfo, spaceGuid, bpOrApiName, limit, skip) {
  var qsBasepath = { 'basePath' : bpOrApiName };
  var qsApiName = { 'title' : 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+'/'+encodeURIComponent(spaceGuid)+'/apis?limit='+limit+'&skip='+skip,
    headers: {
      'Accept': 'application/json',
      'User-Agent': UserAgent
    },
    json: true
  };
  if (qs) {
    options.qs = qs;
  }
  if (gwInfo.gwAuth) {
    options.headers.Authorization = 'Bearer ' + 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: '+makeJsonString(error));
      if (response && response.headers) console.log('getApis: response headers: '+makeJsonString(response.headers));
      console.log('getApis: body type = '+typeof body);
      if (body) console.log('getApis: response JSON.stringify(body): '+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: '+makeJsonString(error));
      } else if (statusCode != 200) {
        console.error('getApis: failure: response code: '+statusCode);
        if (body) {
          var errMsg = makeJsonString(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) {
          if (Array.isArray(body)) {
            resolve(body);
          } else {
            console.error('getApis: Invalid API GW response body; a JSON array was not returned');
            resolve( [] );
          }
        } else {
          console.log('getApis: No APIs found');
          resolve( [] );
        }
      }
    });
  });
}