com.twilio.sdk.resource.NextGenListResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of twilio-java-sdk Show documentation
Show all versions of twilio-java-sdk Show documentation
Release Candidate for Next-Gen Twilio Java Helper Library
package com.twilio.sdk.resource;
import com.twilio.sdk.TwilioClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.TwilioRestResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public abstract class NextGenListResource extends Resource implements Iterable {
protected List pageData;
private String nextPageUrl = null;
private int page = 0;
private int pageSize = 0;
private String previousPageUrl = null;
private String url = null;
public NextGenListResource(final C client) {
this(client, new HashMap());
}
public NextGenListResource(final C client, final Map filters) {
super(client);
this.filters = filters;
}
public Iterator iterator() {
return new ListIterator(getPageData().iterator());
}
public List getPageData() {
if (!isLoaded()) {
try {
load(filters);
} catch (TwilioRestException e) {
throw new RuntimeException(e);
}
}
return Collections.unmodifiableList(pageData);
}
public String getNextPageUrl() {
return nextPageUrl;
}
public int getPage() {
return page;
}
public int getPageSize() {
return pageSize;
}
public String getPreviousPageUrl() {
return previousPageUrl;
}
public String getUrl() {
return url;
}
protected boolean hasNextPage() {
return (nextPageUrl != null && nextPageUrl.length() > 0);
}
protected void fetchNextPage() throws TwilioRestException {
TwilioRestResponse response = getClient().get(nextPageUrl);
parseResponse(response);
}
@Override
protected void parseResponse(TwilioRestResponse response) {
Map data = response.toMap();
Map meta = (Map) data.get("meta");
if (meta == null) {
throw new RuntimeException("Paging metadata not found in Twilio response");
}
nextPageUrl = (String) meta.get("next_page_url");
previousPageUrl = (String) meta.get("previous_page_url");
page = getIntValue(meta.get("page"));
pageSize = getIntValue(meta.get("page_size"));
url = (String) meta.get("url");
pageData = toList(response);
}
protected List toList(TwilioRestResponse response) {
List returnList = new ArrayList();
Map data = response.toMap();
Map meta = (Map) data.get("meta");
String resourceKey = (String) meta.get("key");
Object content = data.get(resourceKey);
if (content instanceof List) {
List