macareno.marco
macareno.marco

Reputation: 143

How to get root path "/" Resource from AWS Api Gateway?

I've been trying to retrieve the Resource with the path "/" (the root) from AWS Api Gateway using the Nodejs AWS SDK. I know the naïve solution would be to do it this way:

var AWS = require('aws-sdk');
var __ = require('lodash');
var Promise = require('bluebird');

var resources = [];
var apiGateway = Promise.promisifyAll(new AWS.APIGateway({apiVersion: '2015-07-09', region: 'us-west-2'}));

var _finishRetrievingResources = function (resources) {
  var orderedResources = __.sortBy(resources, function (res) {
    return res.path.split('/').length;
  });
  var firstResource = orderedResources[0];
};

var _retrieveNextPage = function (resp) {
  resources = resources.concat(resp.data.items);
  if (resp.hasNextPage()) {
    resp.nextPage().on('success', _retrieveNextPage).send();
  } else {
    _finishRetrievingResources(resources);
  }
};

var foo = apiGateway.getResources({restApiId: 'mah_rest_api_id'}).on('success', _retrieveNextPage).send();

However, does anybody know of an alternate method? I'd prefer to know that I'll alway have to do at most one call than having to do multiple.

PS: I know there are several optimizations that could be made (e.g. check for root path on every response), I really want to know if there's a single SDK Call that could fix this.

Upvotes: 1

Views: 2209

Answers (1)

Andrew Templeton
Andrew Templeton

Reputation: 1696

There is not a single call, though it can be if you have less than 500 resources. As a consolation prize, this is the best-practice, using position to prevent accidental misses if there are over 500 resources. If there are less than 500 resources, this will work with one call:

https://github.com/andrew-templeton/cfn-api-gateway-restapi/blob/bd964408bcb4bc6fc8ec91b5e1f0387c8f11691a/index.js#L77-L102

Upvotes: 3

Related Questions