Skip to content

Instantly share code, notes, and snippets.

@TaylorAckley TaylorAckley/pager.js
Last active Dec 11, 2018

Embed
What would you like to do?
Simple Paging Example
require('dotenv').config();
const request = require('request-promise');
// Hoist our users array.
let users = [];
const API_ENDPOINT = `https://publicapi.knowledgeanywhere.com`;
const CLIENT_ID = process.env.LMS_CLIENT_ID; // Loaded from our .env file
const CLIENT_SECRET = process.env.LMS_CLIENT_SECRET; // Loaded from our .env file
const PAGE_SIZE = 1000; // How many records the API returns in a page.
const go = async () => {
const tokenReq = {
method: 'POST',
uri: `${API_ENDPOINT}/auth/token`,
json: true,
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
form: {
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
grant_type: 'client_credentials'
}
};
const token = await request(tokenReq);
users = await getUsers(token);
// We're all done. Now we can do what we would like with our array of user objects.
console.log(`Total length of users is ${users.length}`);
}
const getUsers = async (token) => {
let records = [];
let keepGoing = true;
let offset = 0;
while (keepGoing) {
let response = await reqUsers(token, offset)
await records.push.apply(records, response);
offset += 1000;
if (response.length < PAGE_SIZE) {
keepGoing = false;
return records;
}
}
}
const reqUsers = async (token, offset) => {
const userReq = {
uri: `${API_ENDPOINT}/v1/users?offset=${offset}`,
json: true,
headers: {
"Authorization": `Bearer ${token.access_token}`,
}
};
let payload = await request(userReq);
return payload;
}
go()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.