Skip to content

Commit

Permalink
Merge pull request #6 from iloire/add-pagination
Browse files Browse the repository at this point in the history
Add recursive pagination request looping
  • Loading branch information
tylerfloyd committed Dec 2, 2019
2 parents 6888c73 + 7ba1f99 commit a644ef6
Showing 1 changed file with 40 additions and 25 deletions.
65 changes: 40 additions & 25 deletions lib/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,45 @@
const request = require('superagent');
const endpoints = require('../util/endpoints');

module.exports = (headers, account_id, start_date, end_date) => {
const endpoint = endpoints['history'](account_id);
return request
.get(`${endpoint}`)
.query({
'start-date': `${start_date}T07:00:00.000Z`
})
.query({
'end-date': `${end_date}T07:00:00.000Z`
})
.set(headers)
.send()
.then(res => {
const {
body: {
data: {
items
}
}
} = res;
const doRequest = (headers, endpoint, start_date, end_date, pageOffset) => {
return request
.get(`${endpoint}`)
.query({
'start-date': `${start_date}T07:00:00.000Z`
})
.query({
'end-date': `${end_date}T07:00:00.000Z`
})
.query({
'page-offset': pageOffset
})
.set(headers)
.send()
.then(res => {
const {
body: {
pagination,
data: {
items
}
}
} = res;
return { items, pagination };
})
.catch(err => {
return err.message;
});
}

return items;
})
.catch(err => {
return err.message;
});
module.exports = async (headers, account_id, start_date, end_date) => {
const endpoint = endpoints['history'](account_id);
const items = [];
let currentPage = -1;
let keepGoing = true;
while (keepGoing) { // run through pagination
const r = await doRequest(headers, endpoint, start_date, end_date, ++currentPage);
items.push.apply(items, r.items);
keepGoing = currentPage < r.pagination['total-pages'] - 1;
}
return items;
}

0 comments on commit a644ef6

Please sign in to comment.