Skip to content

Commit 4f005d1

Browse files
committed
add suggested changes to PR
1 parent cee79a6 commit 4f005d1

14 files changed

Lines changed: 315 additions & 227 deletions

src/main/java/io/apimatic/core/HttpRequest.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ private void updateBodyParams(UnaryOperator<Object> setter, String point) {
381381
}
382382

383383
body = CoreHelper.updateValueByPointer(body, point, setter);
384+
return;
384385
}
385386

386387
if (bodyParameters != null) {
@@ -634,19 +635,6 @@ private static String getSerializedHeaderValue(Object obj) {
634635
return CoreHelper.trySerialize(obj);
635636
}
636637

637-
/**
638-
* @return A query URL combining the path, query and template parameters.
639-
*/
640-
public String getQueryUrl() {
641-
StringBuilder builder = new StringBuilder(path);
642-
643-
CoreHelper.appendUrlWithQueryParameters(builder, queryParams,
644-
arraySerializationFormat);
645-
CoreHelper.appendUrlWithTemplateParameters(builder, templateParams);
646-
647-
return builder.toString();
648-
}
649-
650638
/**
651639
* @return A copy of this request builder instance.
652640
*/

src/main/java/io/apimatic/core/types/pagination/CursorPagination.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public Builder apply(PaginatedData<?, ?, ?, ?> paginatedData) {
2525
Response response = paginatedData.getResponse();
2626
Builder reqBuilder = paginatedData.getRequestBuilder();
2727
AtomicBoolean isUpdated = new AtomicBoolean(false);
28+
currentRequestCursor = null;
2829

2930
reqBuilder.updateParameterByJsonPointer(input, old -> {
30-
3131
if (response == null) {
3232
currentRequestCursor = (String) old;
3333
isUpdated.set(true);
@@ -55,6 +55,5 @@ public Builder apply(PaginatedData<?, ?, ?, ?> paginatedData) {
5555
@Override
5656
public void addMetaData(PageWrapper<?, ?> page) {
5757
page.setCursorInput(currentRequestCursor);
58-
currentRequestCursor = null;
5958
}
6059
}

src/main/java/io/apimatic/core/types/pagination/LinkPagination.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public LinkPagination(final String next) {
1818
@Override
1919
public Builder apply(PaginatedData<?, ?, ?, ?> paginatedData) {
2020
Response response = paginatedData.getResponse();
21+
currentRequestLink = null;
2122

2223
if (response == null) {
23-
currentRequestLink = paginatedData.getRequestBuilder().getQueryUrl();
2424
return paginatedData.getRequestBuilder();
2525
}
2626

@@ -38,6 +38,5 @@ public Builder apply(PaginatedData<?, ?, ?, ?> paginatedData) {
3838
@Override
3939
public void addMetaData(PageWrapper<?, ?> page) {
4040
page.setNextLinkInput(currentRequestLink);
41-
currentRequestLink = null;
4241
}
4342
}

src/main/java/io/apimatic/core/types/pagination/OffsetPagination.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public class OffsetPagination implements PaginationStrategy {
99
private final String input;
10-
private int currentRequestOffset = -1;
10+
private int currentRequestOffset;
1111

1212
/**
1313
* @param input JsonPointer of a field in request, representing offset.
@@ -21,9 +21,10 @@ public Builder apply(PaginatedData<?, ?, ?, ?> paginatedData) {
2121
Response response = paginatedData.getResponse();
2222
Builder reqBuilder = paginatedData.getRequestBuilder();
2323
AtomicBoolean isUpdated = new AtomicBoolean(false);
24+
currentRequestOffset = 0;
2425

2526
reqBuilder.updateParameterByJsonPointer(input, old -> {
26-
int oldValue = Integer.parseInt("" + old);
27+
int oldValue = old == null ? 0 : Integer.parseInt("" + old);
2728

2829
if (response == null) {
2930
currentRequestOffset = oldValue;
@@ -47,6 +48,5 @@ public Builder apply(PaginatedData<?, ?, ?, ?> paginatedData) {
4748
@Override
4849
public void addMetaData(PageWrapper<?, ?> page) {
4950
page.setOffsetInput(currentRequestOffset);
50-
currentRequestOffset = -1;
5151
}
5252
}

src/main/java/io/apimatic/core/types/pagination/PagePagination.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public class PagePagination implements PaginationStrategy {
99
private final String input;
10-
private int currentRequestPageNumber = -1;
10+
private int currentRequestPageNumber;
1111

1212
/**
1313
* @param input JsonPointer of a field in request, representing page.
@@ -21,9 +21,10 @@ public Builder apply(PaginatedData<?, ?, ?, ?> paginatedData) {
2121
Response response = paginatedData.getResponse();
2222
Builder reqBuilder = paginatedData.getRequestBuilder();
2323
AtomicBoolean isUpdated = new AtomicBoolean(false);
24+
currentRequestPageNumber = 0;
2425

2526
reqBuilder.updateParameterByJsonPointer(input, old -> {
26-
int oldValue = Integer.parseInt("" + old);
27+
int oldValue = old == null ? 1 : Integer.parseInt("" + old);
2728

2829
if (response == null) {
2930
currentRequestPageNumber = oldValue;
@@ -47,6 +48,5 @@ public Builder apply(PaginatedData<?, ?, ?, ?> paginatedData) {
4748
@Override
4849
public void addMetaData(PageWrapper<?, ?> page) {
4950
page.setPageInput(currentRequestPageNumber);
50-
currentRequestPageNumber = -1;
5151
}
5252
}

src/main/java/io/apimatic/core/types/pagination/PageWrapper.java

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,67 @@ public final class PageWrapper<I, P> implements ApiResponseType<P> {
1717
* @param items Extracted items from the page.
1818
* @return An new instance of PageWrapper.
1919
*/
20-
public static <I, P> PageWrapper<I, P> create(Response response, P page, List<I> items) {
21-
return new PageWrapper<I, P>(response.getStatusCode(), response.getHeaders(), page, items);
20+
public static <I, P> PageWrapper<I, P> create(Response response, P page,
21+
List<I> items, PaginationStrategy strategy) {
22+
return new PageWrapper<I, P>(response.getStatusCode(), response.getHeaders(), page,
23+
items, strategy);
2224
}
2325

2426
private final int statusCode;
2527
private final HttpHeaders headers;
26-
private P page;
27-
private List<I> items;
28+
private final P page;
29+
private final List<I> items;
30+
private final PaginationStrategy strategy;
2831

29-
private String nextLinkInput = null;
30-
private int offsetInput = -1;
31-
private int pageInput = -1;
32-
private String cursorInput = null;
32+
private String nextLinkInput;
33+
private int offsetInput;
34+
private int pageInput;
35+
private String cursorInput;
3336

3437
private PageWrapper(int statusCode, final HttpHeaders headers, final P page,
35-
final List<I> items) {
38+
final List<I> items, final PaginationStrategy strategy) {
3639
this.statusCode = statusCode;
3740
this.headers = headers;
3841
this.page = page;
3942
this.items = items;
43+
this.strategy = strategy;
44+
strategy.addMetaData(this);
4045
}
4146

42-
public void setNextLinkInput(String nextLinkInput) {
43-
this.nextLinkInput = nextLinkInput;
47+
/**
48+
* @return True if cursor paginated page is wrapped
49+
*/
50+
public boolean isCursorPagination() {
51+
return strategy instanceof CursorPagination;
4452
}
4553

46-
public void setOffsetInput(int offsetInput) {
47-
this.offsetInput = offsetInput;
54+
/**
55+
* @return True if link paginated page is wrapped
56+
*/
57+
public boolean isLinkPagination() {
58+
return strategy instanceof LinkPagination;
4859
}
4960

50-
public void setPageInput(int pageInput) {
51-
this.pageInput = pageInput;
61+
/**
62+
* @return True if offset paginated page is wrapped
63+
*/
64+
public boolean isOffsetPagination() {
65+
return strategy instanceof OffsetPagination;
5266
}
5367

54-
public void setCursorInput(String cursorInput) {
55-
this.cursorInput = cursorInput;
68+
/**
69+
* @return True if number paginated page is wrapped
70+
*/
71+
public boolean isNumberPagination() {
72+
return strategy instanceof PagePagination;
73+
}
74+
75+
/**
76+
* Sets the next link input
77+
* @param strategy value of next link input
78+
*/
79+
public void setNextLinkInput(String nextLinkInput) {
80+
this.nextLinkInput = nextLinkInput;
5681
}
5782

5883
/**
@@ -63,6 +88,30 @@ public String getNextLinkInput() {
6388
return nextLinkInput;
6489
}
6590

91+
/**
92+
* Sets the cursor input
93+
* @param strategy value of cursor input
94+
*/
95+
public void setCursorInput(String cursorInput) {
96+
this.cursorInput = cursorInput;
97+
}
98+
99+
/**
100+
* Gets the cursor input used for cursor-based pagination.
101+
* @return The cursor token
102+
*/
103+
public String getCursorInput() {
104+
return cursorInput;
105+
}
106+
107+
/**
108+
* Sets the offset input
109+
* @param strategy value of offset input
110+
*/
111+
public void setOffsetInput(int offsetInput) {
112+
this.offsetInput = offsetInput;
113+
}
114+
66115
/**
67116
* Gets the offset input used for offset-based pagination.
68117
* @return The offset value
@@ -72,19 +121,19 @@ public int getOffsetInput() {
72121
}
73122

74123
/**
75-
* Gets the page number input used for page-based pagination.
76-
* @return The page number
124+
* Sets the page input
125+
* @param strategy value of page input
77126
*/
78-
public int getPageInput() {
79-
return pageInput;
127+
public void setPageInput(int pageInput) {
128+
this.pageInput = pageInput;
80129
}
81130

82131
/**
83-
* Gets the cursor input used for cursor-based pagination.
84-
* @return The cursor token
132+
* Gets the page number input used for page-based pagination.
133+
* @return The page number
85134
*/
86-
public String getCursorInput() {
87-
return cursorInput;
135+
public int getPageInput() {
136+
return pageInput;
88137
}
89138

90139
/**

src/main/java/io/apimatic/core/types/pagination/PaginatedData.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ private boolean updateWith(ApiCall<R, E> apiCall, R pageUnWrapped,
240240

241241
this.apiCall = apiCall;
242242
PageWrapper<I, R> pageWrapper = PageWrapper.create(apiCall.getResponse(), pageUnWrapped,
243-
itemsUnWrapped);
244-
strategy.addMetaData(pageWrapper);
243+
itemsUnWrapped, strategy);
245244
this.page = CheckedSupplier.create(pageCreator.apply(pageWrapper));
246245
itemsUnWrapped.forEach(i -> items.add(CheckedSupplier.create(i)));
247246

src/main/java/io/apimatic/core/utilities/CoreHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,10 +1708,10 @@ public static <T> T updateValueByPointer(T value, String pointer,
17081708
}
17091709

17101710
JsonPointer jsonPointer = Json.createPointer(pointer);
1711+
17111712
if (!jsonPointer.containsValue(structure)) {
1712-
return value;
1713+
structure = jsonPointer.add(structure, JsonValue.NULL);
17131714
}
1714-
17151715
JsonValue oldJsonValue = jsonPointer.getValue(structure);
17161716
Object oldValue = toObject(oldJsonValue);
17171717
Object newValueRaw = updater.apply(oldValue);
@@ -1755,7 +1755,7 @@ private static Object toObject(JsonValue value) {
17551755
case FALSE:
17561756
return false;
17571757
default:
1758-
return value.toString();
1758+
return null;
17591759
}
17601760
}
17611761

0 commit comments

Comments
 (0)