Skip to content

Commit 6773e93

Browse files
committed
Fixed total number of records in the collection
following this pull request fix: PubToolBox@f849f21 Getting the total number of records in the collection PubToolBox#71
1 parent 4ae69a1 commit 6773e93

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

src/main/java/com/afrozaar/wordpress/wpapi/v2/Strings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public interface Strings {
44
String HEADER_TOTAL_PAGES = "X-WP-TotalPages";
55
String HEADER_LINK = "Link";
6+
String HEADER_TOTAL = "x-wp-total";
67
String NEXT = "next";
78
String PREV = "prev";
89
}

src/main/java/com/afrozaar/wordpress/wpapi/v2/response/PagedResponse.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,33 @@ public class PagedResponse<T> {
3030
final String previous;
3131
final int pages;
3232
final List<T> list;
33-
34-
public PagedResponse(Class<T> clazz, String self, String next, String previous, int pages, List<T> list) {
33+
final int total;
34+
35+
// public PagedResponse(Class<T> clazz, String self, String next, String previous, int pages, List<T> list) {
36+
// this.clazz = clazz;
37+
// this.self = self;
38+
// this.next = next;
39+
// this.previous = previous;
40+
// this.pages = pages;
41+
// this.list = list;
42+
// }
43+
44+
public PagedResponse(Class<T> clazz, String self, String next, String previous, int pages, List<T> list, int total) {
3545
this.clazz = clazz;
3646
this.self = self;
3747
this.next = next;
3848
this.previous = previous;
3949
this.pages = pages;
4050
this.list = list;
51+
this.total = total;
52+
}
53+
54+
public int getPages() {
55+
return pages;
56+
}
57+
58+
public int getTotal() {
59+
return total;
4160
}
4261

4362
public boolean hasNext() {
@@ -88,6 +107,7 @@ public static class Builder<BT> {
88107
private List<BT> posts;
89108
private int pages;
90109
private Class<BT> t1;
110+
private int total;
91111

92112
private Builder(Class<BT> t1) {
93113
this.t1 = t1;
@@ -118,21 +138,29 @@ public Builder<BT> withPosts(List<BT> posts) {
118138
}
119139

120140
public PagedResponse<BT> build() {
121-
return new PagedResponse<>(t1, self, next, previous, pages, posts);
141+
return new PagedResponse<>(t1, self, next, previous, pages, posts, total);
122142
}
123143

124144
public Builder<BT> withPages(int pages) {
125145
this.pages = pages;
126146
return this;
127147
}
128148

149+
public Builder<BT> withPages(int pages, int total) {
150+
this.pages = pages;
151+
this.total = total;
152+
return this;
153+
}
154+
129155
public Builder<BT> withPages(HttpHeaders headers) {
130156
Optional<String> totalPages = headers.keySet().stream().filter(x -> Strings.HEADER_TOTAL_PAGES.compareToIgnoreCase(x) == 0).findFirst();
131-
LOG.debug("found pages {} from headers {}", totalPages, headers);
157+
Optional<String> totalOptional = headers.keySet().stream().filter(x -> Strings.HEADER_TOTAL.compareToIgnoreCase(x) == 0).findFirst();
158+
LOG.debug("found pages {}, total {} from headers {}", totalPages, totalOptional, headers);
132159
Stream<String> totalPageHeader = totalPages.map(x -> headers.get(x)).map(x -> x.stream()).orElse(Stream.empty());
133-
totalPageHeader.findFirst().ifPresent(pages -> Builder.this.withPages(Integer.valueOf(pages)));
160+
int page = Integer.valueOf(totalPageHeader.findFirst().get());
161+
Stream<String> totalHeader = totalOptional.map(x -> headers.get(x)).map(x -> x.stream()).orElse(Stream.empty());
162+
totalHeader.findFirst().ifPresent(total -> Builder.this.withPages(page, Integer.valueOf(total)));
134163
return this;
135164
}
136-
137165
}
138166
}

0 commit comments

Comments
 (0)