Skip to content

Commit a784838

Browse files
committed
Add nextPageAsync method
1 parent 4157585 commit a784838

File tree

1 file changed

+63
-23
lines changed

1 file changed

+63
-23
lines changed

src/main/java/com/facebook/ads/sdk/APINodeList.java

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
package com.facebook.ads.sdk;
2424

25+
import com.google.common.util.concurrent.ListenableFuture;
2526
import com.google.gson.JsonObject;
2627
import com.google.gson.JsonParser;
2728
import java.net.URL;
@@ -59,39 +60,78 @@ public APINodeList<T> withAutoPaginationIterator(boolean autoPagination) {
5960
return this;
6061
}
6162

63+
private boolean prepareCallUsingNext(){
64+
// App secret won't return with the 'next' URL. Need to append it
65+
// for paging.
66+
if (this.appSecret == null) {
67+
this.request.setOverrideUrl(this.next);
68+
return true;
69+
} else {
70+
try {
71+
URL a = new URL(this.next);
72+
String connector = a.getQuery() == null ? "?" : "&";
73+
this.request.setOverrideUrl(
74+
this.next +
75+
connector +
76+
"appsecret_proof=" +
77+
this.appSecret
78+
);
79+
return true;
80+
} catch (java.net.MalformedURLException e) {
81+
return false;
82+
}
83+
}
84+
}
85+
86+
private Map<String, Object> prepareCallWithExtraParams(int limit) {
87+
if (after == null) return null;
88+
this.request.setOverrideUrl(null);
89+
Map<String, Object> extraParams = new HashMap<String, Object>();
90+
if (limit > 0) extraParams.put("limit", limit);
91+
extraParams.put("after", after);
92+
return extraParams;
93+
}
94+
6295
public APINodeList<T> nextPage() throws APIException {
6396
return nextPage(0);
6497
}
6598

99+
public ListenableFuture<APIResponse> nextPageAsync() throws APIException {
100+
return nextPageAsync(0);
101+
}
102+
103+
public ListenableFuture<APIResponse> nextPageAsync(int limit) throws APIException {
104+
// First check if 'next' url is returned. If so, always use the it.
105+
if (this.next != null) {
106+
if(prepareCallUsingNext()) {
107+
return request.executeAsyncBase();
108+
} else {
109+
return null;
110+
}
111+
}
112+
Map<String, Object> extraParams = prepareCallWithExtraParams(limit);
113+
if(extraParams != null) {
114+
return request.executeAsyncBase(extraParams);
115+
} else {
116+
return null;
117+
}
118+
}
119+
66120
public APINodeList<T> nextPage(int limit) throws APIException {
67-
// First check if 'next' url is retured. If so, always use the it.
121+
// First check if 'next' url is returned. If so, always use the it.
68122
if (this.next != null) {
69-
// App secret won't return with the 'next' URL. Need to append it
70-
// for paging.
71-
if (this.appSecret == null) {
72-
this.request.setOverrideUrl(this.next);
123+
if(prepareCallUsingNext()) {
124+
return (APINodeList<T>) request.execute();
73125
} else {
74-
try {
75-
URL a = new URL(this.next);
76-
String connector = a.getQuery() == null ? "?" : "&";
77-
this.request.setOverrideUrl(
78-
this.next +
79-
connector +
80-
"appsecret_proof=" +
81-
this.appSecret
82-
);
83-
} catch (java.net.MalformedURLException e) {
84126
return null;
85-
}
86127
}
87-
return (APINodeList<T>) request.execute();
88128
}
89-
if (after == null) return null;
90-
this.request.setOverrideUrl(null);
91-
Map<String, Object> extraParams = new HashMap<String, Object>();
92-
if (limit > 0) extraParams.put("limit", limit);
93-
extraParams.put("after", after);
94-
return (APINodeList<T>) request.execute(extraParams);
129+
Map<String, Object> extraParams = prepareCallWithExtraParams(limit);
130+
if(extraParams != null) {
131+
return (APINodeList<T>) request.execute(extraParams);
132+
} else {
133+
return null;
134+
}
95135
}
96136

97137
public void setCursors(String before, String after) {

0 commit comments

Comments
 (0)