|
22 | 22 | */
|
23 | 23 | package com.facebook.ads.sdk;
|
24 | 24 |
|
| 25 | +import com.google.common.util.concurrent.ListenableFuture; |
25 | 26 | import com.google.gson.JsonObject;
|
26 | 27 | import com.google.gson.JsonParser;
|
27 | 28 | import java.net.URL;
|
@@ -59,39 +60,78 @@ public APINodeList<T> withAutoPaginationIterator(boolean autoPagination) {
|
59 | 60 | return this;
|
60 | 61 | }
|
61 | 62 |
|
| 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 | + |
62 | 95 | public APINodeList<T> nextPage() throws APIException {
|
63 | 96 | return nextPage(0);
|
64 | 97 | }
|
65 | 98 |
|
| 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 | + |
66 | 120 | 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. |
68 | 122 | 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(); |
73 | 125 | } 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) { |
84 | 126 | return null;
|
85 |
| - } |
86 | 127 | }
|
87 |
| - return (APINodeList<T>) request.execute(); |
88 | 128 | }
|
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 | + } |
95 | 135 | }
|
96 | 136 |
|
97 | 137 | public void setCursors(String before, String after) {
|
|
0 commit comments