Skip to content

Commit

Permalink
Google api pagination (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJuanAndOnly99 authored Jun 17, 2024
1 parent 6394c82 commit ce01968
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions scripts/googleapi2events.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,29 @@ function getApiOptions(options) {
// If results hit the limit of 2500 entries, an error is thrown
// Accepts additional options to pass to the API
async function getGoogleEvents(queryName, dynamicOptions) {
const itemsAsync = await calendar.events.list(getApiOptions(dynamicOptions));
const items = itemsAsync.data.items;

if (items.length == 0) {
throw new Error(queryName + 'No events returned!');
} else if (items.length == 2500) {
throw new Error(
queryName + ' Events retrieved reached API limit!',
items.length
);
} else {
console.log(queryName + ' Events retrieved:', items.length);
}
return items;
let allItems = [];
let nextPageToken = null;

// Loop to fetch events until there are no more pages
do {
// If nextPageToken is not null, set it in dynamicOptions to fetch the next page
if (nextPageToken) {
dynamicOptions.set('pageToken', nextPageToken);
}

const itemsAsync = await calendar.events.list(getApiOptions(dynamicOptions));
const items = itemsAsync.data.items;
allItems = allItems.concat(items);
// set page token for the next iteration
nextPageToken = itemsAsync.data.nextPageToken;
} while (nextPageToken);

if (allItems.length == 0) {
throw new Error(queryName + ' No events returned!');
} else {
console.log(queryName + ' Events retrieved:', allItems.length);
}
return allItems;
}

// Fetches events from Google API and transforms them
Expand Down

0 comments on commit ce01968

Please sign in to comment.