Skip to content

Commit

Permalink
Move getAllUsagePlans to a "shared" file
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Chew committed Jun 26, 2019
1 parent 5bf6b1c commit 6cdc771
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 21 deletions.
24 changes: 3 additions & 21 deletions lambdas/catalog-updater/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ let AWS = require('aws-sdk'),
bucketName = '',
hash = require('object-hash')

const { getAllUsagePlans } = require('./shared/get-all-usage-plans')

/**
* Takes in an s3 listObjectsV2 object and returns whether it's a "swagger file" (one ending in .JSON, .YAML, or .YML),
* and whether it's in the catalog folder (S3 Key starts with "catalog/").
Expand Down Expand Up @@ -176,26 +178,6 @@ function copyAnyMethod(api) {
return api
}

/** Fetches all usage plans, combining all pages into a single array. */
async function getAllUsagePlans() {
// The maximum allowed value of `limit` is 500 according to
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/APIGateway.html#getUsagePlans-property
const defaultParams = {limit: 500}

console.log('Fetching first page of usage plans')
let response = await exports.gateway.getUsagePlans(defaultParams).promise()
const usagePlans = response.items

while (response.position) {
console.log(`Fetching next page of usage plans, at position=[${response.position}]`)
const nextParams = {...defaultParams, position: response.position}
response = await exports.gateway.getUsagePlans(nextParams).promise()
usagePlans.push(...response.items)
}

return usagePlans
}

function buildCatalog(swaggerFiles, sdkGeneration) {
console.log(`results: ${JSON.stringify(swaggerFiles, null, 4)}`)
console.log(sdkGeneration)
Expand All @@ -205,7 +187,7 @@ function buildCatalog(swaggerFiles, sdkGeneration) {
generic: []
}

return getAllUsagePlans()
return getAllUsagePlans(exports.gateway)
.then(usagePlans => {
console.log(`usagePlans: ${JSON.stringify(usagePlans, null, 4)}`)
for (let i = 0; i < usagePlans.length; i++) {
Expand Down
33 changes: 33 additions & 0 deletions lambdas/catalog-updater/shared/get-all-usage-plans.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Fetches all usage plans, combining all pages into a single array.
*
* @param apiGateway
* an instance of `AWS.APIGateway` to use for API calls
*
* @param paramOverrides
* a parameter object passed in calls to `APIGateway.getUsagePlans`
*
* @returns
* a Promise resolving with an array of items returned from
* `APIGateway.getUsagePlans` calls
*/
async function getAllUsagePlans(apiGateway, paramOverrides = {}) {
// The maximum allowed value of `limit` is 500 according to
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/APIGateway.html#getUsagePlans-property
const defaultParams = {limit: 500, ...paramOverrides}

console.log('Fetching first page of usage plans')
let response = await apiGateway.getUsagePlans(defaultParams).promise()
const usagePlans = response.items

while (response.position) {
console.log(`Fetching next page of usage plans, at position=[${response.position}]`)
const nextParams = {...defaultParams, position: response.position}
response = await apiGateway.getUsagePlans(nextParams).promise()
usagePlans.push(...response.items)
}

return usagePlans
}

module.exports = { getAllUsagePlans }
33 changes: 33 additions & 0 deletions lambdas/shared/get-all-usage-plans.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Fetches all usage plans, combining all pages into a single array.
*
* @param apiGateway
* an instance of `AWS.APIGateway` to use for API calls
*
* @param paramOverrides
* a parameter object passed in calls to `APIGateway.getUsagePlans`
*
* @returns
* a Promise resolving with an array of items returned from
* `APIGateway.getUsagePlans` calls
*/
async function getAllUsagePlans(apiGateway, paramOverrides = {}) {
// The maximum allowed value of `limit` is 500 according to
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/APIGateway.html#getUsagePlans-property
const defaultParams = {limit: 500, ...paramOverrides}

console.log('Fetching first page of usage plans')
let response = await apiGateway.getUsagePlans(defaultParams).promise()
const usagePlans = response.items

while (response.position) {
console.log(`Fetching next page of usage plans, at position=[${response.position}]`)
const nextParams = {...defaultParams, position: response.position}
response = await apiGateway.getUsagePlans(nextParams).promise()
usagePlans.push(...response.items)
}

return usagePlans
}

module.exports = { getAllUsagePlans }

0 comments on commit 6cdc771

Please sign in to comment.