Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 63 additions & 23 deletions src/main/java/com/facebook/ads/sdk/APINodeList.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package com.facebook.ads.sdk;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.net.URL;
Expand Down Expand Up @@ -59,39 +60,78 @@ public APINodeList<T> withAutoPaginationIterator(boolean autoPagination) {
return this;
}

private boolean prepareCallUsingNext(){
// App secret won't return with the 'next' URL. Need to append it
// for paging.
if (this.appSecret == null) {
this.request.setOverrideUrl(this.next);
return true;
} else {
try {
URL a = new URL(this.next);
String connector = a.getQuery() == null ? "?" : "&";
this.request.setOverrideUrl(
this.next +
connector +
"appsecret_proof=" +
this.appSecret
);
return true;
} catch (java.net.MalformedURLException e) {
return false;
}
}
}

private Map<String, Object> prepareCallWithExtraParams(int limit) {
if (after == null) return null;
this.request.setOverrideUrl(null);
Map<String, Object> extraParams = new HashMap<String, Object>();
if (limit > 0) extraParams.put("limit", limit);
extraParams.put("after", after);
return extraParams;
}

public APINodeList<T> nextPage() throws APIException {
return nextPage(0);
}

public ListenableFuture<APIResponse> nextPageAsync() throws APIException {
return nextPageAsync(0);
}

public ListenableFuture<APIResponse> nextPageAsync(int limit) throws APIException {
// First check if 'next' url is returned. If so, always use the it.
if (this.next != null) {
if(prepareCallUsingNext()) {
return request.executeAsyncBase();
} else {
return null;
}
}
Map<String, Object> extraParams = prepareCallWithExtraParams(limit);
if(extraParams != null) {
return request.executeAsyncBase(extraParams);
} else {
return null;
}
}

public APINodeList<T> nextPage(int limit) throws APIException {
// First check if 'next' url is retured. If so, always use the it.
// First check if 'next' url is returned. If so, always use the it.
if (this.next != null) {
// App secret won't return with the 'next' URL. Need to append it
// for paging.
if (this.appSecret == null) {
this.request.setOverrideUrl(this.next);
if(prepareCallUsingNext()) {
return (APINodeList<T>) request.execute();
} else {
try {
URL a = new URL(this.next);
String connector = a.getQuery() == null ? "?" : "&";
this.request.setOverrideUrl(
this.next +
connector +
"appsecret_proof=" +
this.appSecret
);
} catch (java.net.MalformedURLException e) {
return null;
}
}
return (APINodeList<T>) request.execute();
}
if (after == null) return null;
this.request.setOverrideUrl(null);
Map<String, Object> extraParams = new HashMap<String, Object>();
if (limit > 0) extraParams.put("limit", limit);
extraParams.put("after", after);
return (APINodeList<T>) request.execute(extraParams);
Map<String, Object> extraParams = prepareCallWithExtraParams(limit);
if(extraParams != null) {
return (APINodeList<T>) request.execute(extraParams);
} else {
return null;
}
}

public void setCursors(String before, String after) {
Expand Down