From db9081ee4bb480eb0e7920234426e8b7c6cef1d2 Mon Sep 17 00:00:00 2001 From: Zack Grannan Date: Wed, 20 Feb 2019 20:23:13 +0800 Subject: [PATCH] Add `nextPageAsync` method --- .../com/facebook/ads/sdk/APINodeList.java | 86 ++++++++++++++----- 1 file changed, 63 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/facebook/ads/sdk/APINodeList.java b/src/main/java/com/facebook/ads/sdk/APINodeList.java index 0ef02b41..838fbcf6 100644 --- a/src/main/java/com/facebook/ads/sdk/APINodeList.java +++ b/src/main/java/com/facebook/ads/sdk/APINodeList.java @@ -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; @@ -59,39 +60,78 @@ public APINodeList 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 prepareCallWithExtraParams(int limit) { + if (after == null) return null; + this.request.setOverrideUrl(null); + Map extraParams = new HashMap(); + if (limit > 0) extraParams.put("limit", limit); + extraParams.put("after", after); + return extraParams; + } + public APINodeList nextPage() throws APIException { return nextPage(0); } + public ListenableFuture nextPageAsync() throws APIException { + return nextPageAsync(0); + } + + public ListenableFuture 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 extraParams = prepareCallWithExtraParams(limit); + if(extraParams != null) { + return request.executeAsyncBase(extraParams); + } else { + return null; + } + } + public APINodeList 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) 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) request.execute(); } - if (after == null) return null; - this.request.setOverrideUrl(null); - Map extraParams = new HashMap(); - if (limit > 0) extraParams.put("limit", limit); - extraParams.put("after", after); - return (APINodeList) request.execute(extraParams); + Map extraParams = prepareCallWithExtraParams(limit); + if(extraParams != null) { + return (APINodeList) request.execute(extraParams); + } else { + return null; + } } public void setCursors(String before, String after) {