Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/com/afrozaar/wordpress/wpapi/v2/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public interface Strings {
String HEADER_TOTAL_PAGES = "X-WP-TotalPages";
String HEADER_LINK = "Link";
String HEADER_TOTAL = "x-wp-total";
String NEXT = "next";
String PREV = "prev";
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,24 @@ public class PagedResponse<T> {
final String previous;
final int pages;
final List<T> list;
final int total;

public PagedResponse(Class<T> clazz, String self, String next, String previous, int pages, List<T> list) {
public int getPages() {
return pages;
}

public int getTotal() {
return total;
}

public PagedResponse(Class<T> clazz, String self, String next, String previous, int pages, List<T> list, int total) {
this.clazz = clazz;
this.self = self;
this.next = next;
this.previous = previous;
this.pages = pages;
this.list = list;
this.total = total;
}

public boolean hasNext() {
Expand Down Expand Up @@ -88,6 +98,7 @@ public static class Builder<BT> {
private List<BT> posts;
private int pages;
private Class<BT> t1;
private int total;

private Builder(Class<BT> t1) {
this.t1 = t1;
Expand Down Expand Up @@ -118,19 +129,30 @@ public Builder<BT> withPosts(List<BT> posts) {
}

public PagedResponse<BT> build() {
return new PagedResponse<>(t1, self, next, previous, pages, posts);
return new PagedResponse<>(t1, self, next, previous, pages, posts, total);
}

public Builder<BT> withPages(int pages) {
this.pages = pages;
return this;
}

public Builder<BT> withPages(int pages, int total) {
this.pages = pages;
this.total = total;
return this;
}

public Builder<BT> withPages(HttpHeaders headers) {
Optional<String> totalPages = headers.keySet().stream().filter(x -> Strings.HEADER_TOTAL_PAGES.compareToIgnoreCase(x) == 0).findFirst();
LOG.debug("found pages {} from headers {}", totalPages, headers);
Optional<String> totalOptional = headers.keySet().stream().filter(x -> Strings.HEADER_TOTAL.compareToIgnoreCase(x) == 0).findFirst();
LOG.debug("found pages {}, total {} from headers {}", totalPages, totalOptional, headers);
Stream<String> totalPageHeader = totalPages.map(x -> headers.get(x)).map(x -> x.stream()).orElse(Stream.empty());
totalPageHeader.findFirst().ifPresent(pages -> Builder.this.withPages(Integer.valueOf(pages)));
int page = Integer.valueOf(totalPageHeader.findFirst().get());

Stream<String> totalHeader = totalOptional.map(x -> headers.get(x)).map(x -> x.stream()).orElse(Stream.empty());
totalHeader.findFirst().ifPresent(total -> Builder.this.withPages(page, Integer.valueOf(total)));

return this;
}

Expand Down