com.gocardless.http.PaginatingIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gocardless-pro Show documentation
Show all versions of gocardless-pro Show documentation
Client library for accessing the GoCardless Pro API
package com.gocardless.http;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.Lists;
import java.util.List;
class PaginatingIterator extends AbstractIterator {
private final ListRequest, T> request;
private final HttpClient client;
private List items;
private String nextCursor;
PaginatingIterator(ListRequest, T> request, HttpClient client) {
this.request = request;
this.client = client;
loadPage();
}
@Override
protected T computeNext() {
if (items.isEmpty() && nextCursor != null) {
loadPage();
}
if (items.isEmpty()) {
return endOfData();
}
T item = items.get(0);
items.remove(0);
return item;
}
private void loadPage() {
request.setAfter(nextCursor);
ListResponse response = client.executeWithRetries(request);
items = Lists.newArrayList(response.getItems());
nextCursor = response.getAfter();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy