From d70dcfb9699484de616f66ac4b5c4c977041c76f Mon Sep 17 00:00:00 2001 From: Thang Nguyen Date: Mon, 16 Oct 2017 22:43:05 +0700 Subject: [PATCH] [add_pagination_interest_categories] Added pagination for interest categories --- .../output/mailchimp/MailChimpClient.java | 36 ++++++++++++++----- .../model/InterestCategoriesResponse.java | 13 +++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/embulk/output/mailchimp/MailChimpClient.java b/src/main/java/org/embulk/output/mailchimp/MailChimpClient.java index 4e793a3..9b03327 100644 --- a/src/main/java/org/embulk/output/mailchimp/MailChimpClient.java +++ b/src/main/java/org/embulk/output/mailchimp/MailChimpClient.java @@ -132,12 +132,32 @@ Map> extractInterestCategoriesByGroupNames if (task.getGroupingColumns().isPresent() && !task.getGroupingColumns().get().isEmpty()) { List interestCategoryNames = task.getGroupingColumns().get(); - String endpoint = MessageFormat.format(mailchimpEndpoint + "/lists/{0}/interest-categories", - task.getListId()); - - JsonNode response = client.sendRequest(endpoint, HttpMethod.GET, task); - InterestCategoriesResponse interestCategoriesResponse = mapper.treeToValue(response, - InterestCategoriesResponse.class); + int count = 100; + int offset = 0; + int page = 1; + boolean hasMore = true; + JsonNode response; + List allCategoriesResponse = new ArrayList<>(); + + while (hasMore) { + String endpoint = MessageFormat.format(mailchimpEndpoint + "/lists/{0}/interest-categories?count={1}&offset={2}", + task.getListId(), + count, + offset); + + response = client.sendRequest(endpoint, HttpMethod.GET, task); + InterestCategoriesResponse interestCategoriesResponse = mapper.treeToValue(response, + InterestCategoriesResponse.class); + + allCategoriesResponse.addAll(interestCategoriesResponse.getCategories()); + if (hasMorePage(interestCategoriesResponse.getTotalItems(), count, page)) { + offset = count; + page++; + } + else { + hasMore = false; + } + } Function function = new Function() { @@ -150,7 +170,7 @@ public String apply(CategoriesResponse input) // Transform to a list of available category names and validate with data that user input ImmutableList availableCategories = FluentIterable - .from(interestCategoriesResponse.getCategories()) + .from(allCategoriesResponse) .transform(function) .toList(); @@ -160,7 +180,7 @@ public String apply(CategoriesResponse input) } } - for (CategoriesResponse categoriesResponse : interestCategoriesResponse.getCategories()) { + for (CategoriesResponse categoriesResponse : allCategoriesResponse) { String detailEndpoint = MessageFormat.format(mailchimpEndpoint + "/lists/{0}/interest-categories/{1}/interests", task.getListId(), categoriesResponse.getId()); diff --git a/src/main/java/org/embulk/output/mailchimp/model/InterestCategoriesResponse.java b/src/main/java/org/embulk/output/mailchimp/model/InterestCategoriesResponse.java index fd3baca..cba8157 100644 --- a/src/main/java/org/embulk/output/mailchimp/model/InterestCategoriesResponse.java +++ b/src/main/java/org/embulk/output/mailchimp/model/InterestCategoriesResponse.java @@ -12,6 +12,9 @@ public class InterestCategoriesResponse @JsonProperty("categories") private List categories; + @JsonProperty("total_items") + private int totalItems; + public List getCategories() { return categories; @@ -21,4 +24,14 @@ public void setCategories(List categories) { this.categories = categories; } + + public int getTotalItems() + { + return totalItems; + } + + public void setTotalItems(int totalItems) + { + this.totalItems = totalItems; + } }