All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gocardless.http.PaginatingIterator Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
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 request;
    private final HttpClient client;
    private List items;
    private String nextCursor;

    PaginatingIterator(ListRequest 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