Skip to content

Commit 5dc1606

Browse files
committed
Implement logging for pagination
1 parent 0925367 commit 5dc1606

2 files changed

Lines changed: 22 additions & 29 deletions

File tree

src/main/java/io/github/jpmorganchase/fusion/Fusion.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@
4141
import java.util.Map;
4242
import java.util.Objects;
4343
import lombok.Builder;
44+
import lombok.extern.slf4j.Slf4j;
4445

4546
/**
4647
* Class representing the Fusion API, providing methods that correspond to available API endpoints
4748
*/
49+
@Slf4j
4850
public class Fusion {
4951

5052
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
@@ -210,19 +212,27 @@ private Map<String, Map<String, Object>> callForMap(String url) {
210212
* @return aggregated JSON response containing all pages of data
211213
*/
212214
private String callAPIWithPagination(String url, Integer pageSize) {
215+
log.debug("Starting paginated request to URL: {}", url);
216+
213217
Map<String, String> headers = new HashMap<>();
214218
headers.put("x-jpmc-paginate", "true");
215219
if (pageSize != null) {
220+
log.debug("Using page size: {}", pageSize);
216221
headers.put("x-jpmc-page-size", String.valueOf(pageSize));
217222
}
218223

219224
Gson gson = new Gson();
220225
JsonArray aggregatedResources = new JsonArray();
221226
String nextToken = null;
227+
int pageCount = 0;
222228

223229
do {
230+
pageCount++;
224231
if (nextToken != null) {
225232
headers.put("x-next-token", nextToken);
233+
log.debug("Fetching page {} with next token", pageCount);
234+
} else {
235+
log.debug("Fetching page {}", pageCount);
226236
}
227237

228238
HttpResponse<String> response = this.api.callAPIWithResponse(url, headers);
@@ -231,13 +241,24 @@ private String callAPIWithPagination(String url, Integer pageSize) {
231241
JsonObject pageObject = JsonParser.parseString(pageJson).getAsJsonObject();
232242
if (pageObject.has("resources") && pageObject.get("resources").isJsonArray()) {
233243
JsonArray pageResources = pageObject.getAsJsonArray("resources");
244+
int pageResourceCount = pageResources.size();
234245
pageResources.forEach(aggregatedResources::add);
246+
log.debug("Retrieved {} resources from page {}", pageResourceCount, pageCount);
235247
}
236248

237249
nextToken = getHeaderValue(response.getHeaders(), "x-next-token");
238250

251+
if (nextToken != null && !nextToken.isEmpty()) {
252+
log.debug("Next token received, more pages available");
253+
}
254+
239255
} while (nextToken != null && !nextToken.isEmpty());
240256

257+
log.debug(
258+
"Pagination complete. Total pages fetched: {}, Total resources: {}",
259+
pageCount,
260+
aggregatedResources.size());
261+
241262
JsonObject result = new JsonObject();
242263
result.add("resources", aggregatedResources);
243264
return gson.toJson(result);
@@ -348,7 +369,7 @@ public Map<String, DataProduct> listProducts() {
348369
*/
349370
public Map<String, Dataset> listDatasets(String catalogName, String contains, boolean idContains) {
350371
String url = String.format("%1scatalogs/%2s/datasets", this.rootURL, catalogName);
351-
String json = callAPIWithPagination(url, null);
372+
String json = callAPIWithPagination(url, 500);
352373
return filterDatasets(responseParser.parseDatasetResponse(json, catalogName), contains, idContains);
353374
}
354375

src/test/java/io/github/jpmorganchase/fusion/FusionTest.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -750,32 +750,4 @@ public void testListAttributesWithPagination() throws Exception {
750750
// Verify pagination occurred
751751
verify(apiManager, times(2)).callAPIWithResponse(anyString(), anyMap());
752752
}
753-
754-
@Test
755-
public void testPaginationDoesNotSendPageSizeHeaderWhenNull() throws Exception {
756-
Fusion f = stubFusion();
757-
758-
// Setup single page response
759-
io.github.jpmorganchase.fusion.http.HttpResponse<String> httpResponse =
760-
io.github.jpmorganchase.fusion.http.HttpResponse.<String>builder()
761-
.statusCode(200)
762-
.body("{\"resources\":[{\"identifier\":\"dataset1\"}]}")
763-
.headers(new HashMap<>())
764-
.build();
765-
766-
when(apiManager.callAPIWithResponse(anyString(), anyMap())).thenReturn(httpResponse);
767-
768-
Map<String, Dataset> stubResponse = new HashMap<>();
769-
stubResponse.put("dataset1", Dataset.builder().identifier("dataset1").build());
770-
when(responseParser.parseDatasetResponse(anyString(), eq("common"))).thenReturn(stubResponse);
771-
772-
f.listDatasets("common");
773-
774-
// Verify that x-jpmc-page-size header is NOT present (only x-jpmc-paginate should be)
775-
verify(apiManager, times(1))
776-
.callAPIWithResponse(
777-
anyString(),
778-
argThat(headers ->
779-
headers.containsKey("x-jpmc-paginate") && !headers.containsKey("x-jpmc-page-size")));
780-
}
781753
}

0 commit comments

Comments
 (0)