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

com.stripe.model.SearchPagingIterator Maven / Gradle / Ivy

There is a newer version: 28.1.0-beta.3
Show newest version
package com.stripe.model;

import com.stripe.net.*;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;

public class SearchPagingIterator extends ApiResource implements Iterator {
  private final String url;

  private final Type pageType;

  private StripeSearchResultInterface currentSearchResult;
  private Iterator currentDataIterator;

  private String nextPage;

  SearchPagingIterator(
      final StripeSearchResultInterface stripeSearchResult,
      StripeResponseGetter responseGetter,
      Type pageType) {
    this.url = stripeSearchResult.getUrl();
    this.nextPage = stripeSearchResult.getNextPage();

    this.currentSearchResult = stripeSearchResult;
    this.currentDataIterator = stripeSearchResult.getData().iterator();
    this.pageType = pageType;
    setResponseGetter(responseGetter);
  }

  @Override
  public boolean hasNext() {
    return currentDataIterator.hasNext() || currentSearchResult.getHasMore();
  }

  @Override
  public T next() {
    // if we've run out of data on the current page, try to fetch another
    // one
    if (!currentDataIterator.hasNext() && currentSearchResult.getHasMore()) {
      try {
        Map params = new HashMap<>();

        // copy all the parameters from the initial request
        Map initialParams = currentSearchResult.getRequestParams();
        if (initialParams != null) {
          params.putAll(initialParams);
        }

        // then put our new page start in
        params.put("page", this.nextPage);

        this.currentSearchResult = search(params, currentSearchResult.getRequestOptions());
        this.nextPage = this.currentSearchResult.getNextPage();

        this.currentDataIterator = currentSearchResult.getData().iterator();
      } catch (final Exception e) {
        throw new RuntimeException("Unable to lazy-load stripe objects", e);
      }
    }

    if (currentDataIterator.hasNext()) {
      final T next = currentDataIterator.next();
      return next;
    }

    throw new NoSuchElementException();
  }

  @Override
  public void remove() {
    throw new UnsupportedOperationException();
  }

  private StripeSearchResultInterface search(
      final Map params, final RequestOptions options) throws Exception {
    ApiRequest request =
        new ApiRequest(BaseAddress.API, RequestMethod.GET, url, params, options, ApiMode.V1);
    return getResponseGetter().request(request, pageType);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy