Skip to content

Commit

Permalink
refactor: Define dedicated PageNavigator for token based and cursor b…
Browse files Browse the repository at this point in the history
…ased navigation. Logic about next page computation for cursor based is shared for all list using it
  • Loading branch information
JPPortier committed Nov 16, 2023
1 parent 5313997 commit 6b6e031
Show file tree
Hide file tree
Showing 22 changed files with 114 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.sinch.sdk.core.http.HttpClient;
import com.sinch.sdk.core.http.HttpMapper;
import com.sinch.sdk.core.models.pagination.Page;
import com.sinch.sdk.core.models.pagination.PageToken;
import com.sinch.sdk.core.models.pagination.TokenPageNavigator;
import com.sinch.sdk.core.utils.EnumDynamic;
import com.sinch.sdk.core.utils.Pair;
import com.sinch.sdk.domains.numbers.adapters.api.v1.ActiveNumberApi;
Expand Down Expand Up @@ -84,7 +84,7 @@ public ActiveNumberListResponse list(ActiveNumberListRequestParameters parameter
pageSize,
pageToken,
orderBy);
Pair<Collection<ActiveNumber>, PageToken<String>> content =
Pair<Collection<ActiveNumber>, TokenPageNavigator> content =
ActiveNumberDtoConverter.convert(response);
return new ActiveNumberListResponse(
this, new Page<>(parameters, content.getLeft(), content.getRight()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sinch.sdk.domains.numbers.adapters.converters;

import com.sinch.sdk.core.models.pagination.PageToken;
import com.sinch.sdk.core.models.pagination.TokenPageNavigator;
import com.sinch.sdk.core.utils.Pair;
import com.sinch.sdk.domains.numbers.models.ActiveNumber;
import com.sinch.sdk.domains.numbers.models.Capability;
Expand All @@ -14,7 +14,7 @@

public class ActiveNumberDtoConverter {

public static Pair<Collection<ActiveNumber>, PageToken<String>> convert(
public static Pair<Collection<ActiveNumber>, TokenPageNavigator> convert(
ActiveNumbersResponseDto dto) {
String nextPageToken = dto.getNextPageToken();
List<ActiveNumberDto> list = dto.getActiveNumbers();
Expand All @@ -23,7 +23,7 @@ public static Pair<Collection<ActiveNumber>, PageToken<String>> convert(
pageContent =
list.stream().map(ActiveNumberDtoConverter::convert).collect(Collectors.toList());
}
return new Pair<>(pageContent, new PageToken<>(nextPageToken));
return new Pair<>(pageContent, new TokenPageNavigator(nextPageToken));
}

public static ActiveNumber convert(ActiveNumberDto dto) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public ActiveNumberListResponse(
}

public boolean hasNextPage() {
return (null != page.getNextPageToken()
&& !StringUtil.isEmpty(page.getNextPageToken().getToken()));
return (null != page.getNextPageToken() && !StringUtil.isEmpty(page.getNextPageToken()));
}

public ActiveNumberListResponse nextPage() {
Expand All @@ -37,8 +36,7 @@ public ActiveNumberListResponse nextPage() {
}
ActiveNumberListRequestParameters.Builder newParameters =
new ActiveNumberListRequestParameters.Builder(page.getParameters());
String nextToken = page.getNextPageToken().getToken();
newParameters.setPageToken(nextToken);
newParameters.setPageToken(page.getNextPageToken());
return service.list(newParameters.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.core.http.HttpClient;
import com.sinch.sdk.core.http.HttpMapper;
import com.sinch.sdk.core.models.pagination.CursorPageNavigator;
import com.sinch.sdk.core.models.pagination.Page;
import com.sinch.sdk.core.models.pagination.PageToken;
import com.sinch.sdk.core.utils.Pair;
import com.sinch.sdk.domains.sms.adapters.api.v1.BatchesApi;
import com.sinch.sdk.domains.sms.adapters.converters.BatchDtoConverter;
Expand Down Expand Up @@ -76,7 +76,7 @@ public BatchesListResponse list(BatchesListRequestParameters parameters) throws
guardParameters.getEndDate().map(Instant::toString).orElse(null),
guardParameters.getClientReference().orElse(null));

Pair<Collection<Batch<?>>, PageToken<Integer>> content = BatchDtoConverter.convert(response);
Pair<Collection<Batch<?>>, CursorPageNavigator> content = BatchDtoConverter.convert(response);

return new BatchesListResponse(
this, new Page<>(guardParameters, content.getLeft(), content.getRight()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.core.http.HttpClient;
import com.sinch.sdk.core.http.HttpMapper;
import com.sinch.sdk.core.models.pagination.CursorPageNavigator;
import com.sinch.sdk.core.models.pagination.Page;
import com.sinch.sdk.core.models.pagination.PageToken;
import com.sinch.sdk.core.utils.EnumDynamic;
import com.sinch.sdk.core.utils.Pair;
import com.sinch.sdk.domains.sms.adapters.api.v1.DeliveryReportsApi;
Expand Down Expand Up @@ -104,7 +104,7 @@ public DeliveryReportsListResponse list(DeliveryReportListRequestParameters para
.orElse(null),
guardParameters.getClientReference().orElse(null));

Pair<Collection<DeliveryReportRecipient>, PageToken<Integer>> content =
Pair<Collection<DeliveryReportRecipient>, CursorPageNavigator> content =
DeliveryReportDtoConverter.convert(response);

return new DeliveryReportsListResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.core.http.HttpClient;
import com.sinch.sdk.core.http.HttpMapper;
import com.sinch.sdk.core.models.pagination.CursorPageNavigator;
import com.sinch.sdk.core.models.pagination.Page;
import com.sinch.sdk.core.models.pagination.PageToken;
import com.sinch.sdk.core.utils.Pair;
import com.sinch.sdk.domains.sms.adapters.api.v1.GroupsApi;
import com.sinch.sdk.domains.sms.adapters.converters.GroupsDtoConverter;
Expand Down Expand Up @@ -70,7 +70,7 @@ public GroupsListResponse list(GroupsListRequestParameters parameters) throws Ap
guardParameters.getPage().orElse(null),
guardParameters.getPageSize().orElse(null));

Pair<Collection<Group>, PageToken<Integer>> content = GroupsDtoConverter.convert(response);
Pair<Collection<Group>, CursorPageNavigator> content = GroupsDtoConverter.convert(response);

return new GroupsListResponse(
this, new Page<>(guardParameters, content.getLeft(), content.getRight()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.core.http.HttpClient;
import com.sinch.sdk.core.http.HttpMapper;
import com.sinch.sdk.core.models.pagination.CursorPageNavigator;
import com.sinch.sdk.core.models.pagination.Page;
import com.sinch.sdk.core.models.pagination.PageToken;
import com.sinch.sdk.core.utils.Pair;
import com.sinch.sdk.domains.sms.adapters.api.v1.InboundsApi;
import com.sinch.sdk.domains.sms.adapters.converters.InboundsDtoConverter;
Expand Down Expand Up @@ -52,7 +52,7 @@ public InboundsListResponse list(InboundsListRequestParameters parameters) throw
guardParameters.getEndDate().map(Instant::toString).orElse(null),
guardParameters.getClientReference().orElse(null));

Pair<Collection<Inbound<?>>, PageToken<Integer>> content =
Pair<Collection<Inbound<?>>, CursorPageNavigator> content =
InboundsDtoConverter.convert(response);

return new InboundsListResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.core.models.AbstractOpenApiSchema;
import com.sinch.sdk.core.models.pagination.PageToken;
import com.sinch.sdk.core.models.pagination.CursorPageNavigator;
import com.sinch.sdk.core.utils.Pair;
import com.sinch.sdk.domains.sms.models.BaseBatch;
import com.sinch.sdk.domains.sms.models.Batch;
Expand Down Expand Up @@ -269,14 +269,10 @@ private static MediaBodyDto convert(MediaBody value) {
return new MediaBodyDto().url(value.getUrl()).message(value.getMessage().orElse(null));
}

public static <T extends Batch<?>> Pair<Collection<T>, PageToken<Integer>> convert(
public static <T extends Batch<?>> Pair<Collection<T>, CursorPageNavigator> convert(
ApiBatchListDto dto) {
// check end of pagination limit reached: (current page number * page size ) cannot be greater
// than "count" to be able to call next page
Integer nextPageToken =
((dto.getPage() + 1) * Long.valueOf(dto.getPageSize())) >= dto.getCount()
? null
: dto.getPage() + 1;
CursorPageNavigator navigator =
new CursorPageNavigator(dto.getPage(), dto.getPageSize(), dto.getCount());
Collection<ApiBatchListBatchesInnerDto> collection = dto.getBatches();
Collection<T> pageContent = new ArrayList<>();
if (null != collection) {
Expand All @@ -285,7 +281,7 @@ public static <T extends Batch<?>> Pair<Collection<T>, PageToken<Integer>> conve
pageContent.add(convert);
}
}
return new Pair<>(pageContent, new PageToken<>(nextPageToken));
return new Pair<>(pageContent, navigator);
}

public static ApiDeliveryFeedbackDto convert(Collection<String> recipients) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sinch.sdk.domains.sms.adapters.converters;

import com.sinch.sdk.core.models.pagination.PageToken;
import com.sinch.sdk.core.models.pagination.CursorPageNavigator;
import com.sinch.sdk.core.utils.Pair;
import com.sinch.sdk.domains.sms.models.DeliveryReportBatch;
import com.sinch.sdk.domains.sms.models.DeliveryReportBatchMMS;
Expand Down Expand Up @@ -74,14 +74,10 @@ public static DeliveryReportRecipient convert(RecipientDeliveryReportDto dto) {
.build();
}

public static Pair<Collection<DeliveryReportRecipient>, PageToken<Integer>> convert(
public static Pair<Collection<DeliveryReportRecipient>, CursorPageNavigator> convert(
DeliveryReportListDto dto) {
// check end of pagination limit reached: (current page number * page size ) cannot be greater
// than "count" to be able to call next page
Integer nextPageToken =
((dto.getPage() + 1) * Long.valueOf(dto.getPageSize())) >= dto.getCount()
? null
: dto.getPage() + 1;
CursorPageNavigator navigator =
new CursorPageNavigator(dto.getPage(), dto.getPageSize(), dto.getCount());
Collection<RecipientDeliveryReportDto> collection = dto.getDeliveryReports();
Collection<DeliveryReportRecipient> pageContent = new ArrayList<>();
if (null != collection) {
Expand All @@ -90,7 +86,7 @@ public static Pair<Collection<DeliveryReportRecipient>, PageToken<Integer>> conv
pageContent.add(convert);
}
}
return new Pair<>(pageContent, new PageToken<>(nextPageToken));
return new Pair<>(pageContent, navigator);
}

private static DeliveryReportStatusDetails convert(MessageDeliveryStatusDto dto) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sinch.sdk.domains.sms.adapters.converters;

import com.sinch.sdk.core.models.pagination.PageToken;
import com.sinch.sdk.core.models.pagination.CursorPageNavigator;
import com.sinch.sdk.core.utils.DateUtil;
import com.sinch.sdk.core.utils.Pair;
import com.sinch.sdk.domains.sms.models.Group;
Expand Down Expand Up @@ -51,13 +51,9 @@ public static GroupObjectDto convert(GroupCreateRequestParameters client) {
.autoUpdate(client.getAutoUpdate().map(GroupsDtoConverter::convert).orElse(null));
}

public static Pair<Collection<Group>, PageToken<Integer>> convert(ApiGroupListDto dto) {
// check end of pagination limit reached: (current page number * page size ) cannot be greater
// than "count" to be able to call next page
Integer nextPageToken =
((dto.getPage() + 1) * Long.valueOf(dto.getPageSize())) >= dto.getCount()
? null
: dto.getPage() + 1;
public static Pair<Collection<Group>, CursorPageNavigator> convert(ApiGroupListDto dto) {
CursorPageNavigator navigator =
new CursorPageNavigator(dto.getPage(), dto.getPageSize(), dto.getCount());
Collection<ApiGroupDto> collection = dto.getGroups();
Collection<Group> pageContent = new ArrayList<>();
if (null != collection) {
Expand All @@ -66,7 +62,7 @@ public static Pair<Collection<Group>, PageToken<Integer>> convert(ApiGroupListDt
pageContent.add(convert);
}
}
return new Pair<>(pageContent, new PageToken<>(nextPageToken));
return new Pair<>(pageContent, navigator);
}

public static ReplaceGroupRequestDto convert(GroupReplaceRequestParameters client) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.core.models.AbstractOpenApiSchema;
import com.sinch.sdk.core.models.pagination.PageToken;
import com.sinch.sdk.core.models.pagination.CursorPageNavigator;
import com.sinch.sdk.core.utils.Pair;
import com.sinch.sdk.domains.sms.models.Inbound;
import com.sinch.sdk.domains.sms.models.InboundBinary;
Expand All @@ -27,13 +27,9 @@ public static Inbound<?> convert(AbstractOpenApiSchema dto) {
}
}

public static Pair<Collection<Inbound<?>>, PageToken<Integer>> convert(ApiInboundListDto dto) {
// check end of pagination limit reached: (current page number * page size ) cannot be greater
// than "count" to be able to call next page
Integer nextPageToken =
((dto.getPage() + 1) * Long.valueOf(dto.getPageSize())) >= dto.getCount()
? null
: dto.getPage() + 1;
public static Pair<Collection<Inbound<?>>, CursorPageNavigator> convert(ApiInboundListDto dto) {
CursorPageNavigator navigator =
new CursorPageNavigator(dto.getPage(), dto.getPageSize(), dto.getCount());
Collection<InboundDto> collection = dto.getInbounds();
Collection<Inbound<?>> pageContent = new ArrayList<>();
if (null != collection) {
Expand All @@ -42,7 +38,7 @@ public static Pair<Collection<Inbound<?>>, PageToken<Integer>> convert(ApiInboun
pageContent.add(convert);
}
}
return new Pair<>(pageContent, new PageToken<>(nextPageToken));
return new Pair<>(pageContent, navigator);
}

public static InboundBinary convert(MOBinaryDto dto) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public BatchesListResponse(
}

public boolean hasNextPage() {
return (null != page.getNextPageToken() && null != page.getNextPageToken().getToken());
return (null != page.getNextPageToken());
}

public BatchesListResponse nextPage() {
Expand All @@ -34,8 +34,7 @@ public BatchesListResponse nextPage() {
}
BatchesListRequestParameters.Builder newParameters =
BatchesListRequestParameters.builder(page.getParameters());
Integer nextToken = page.getNextPageToken().getToken();
newParameters.setPage(nextToken);
newParameters.setPage(page.getNextPageToken());
return service.list(newParameters.build());
}

Expand All @@ -45,6 +44,6 @@ public Collection<Batch<?>> getContent() {

@Override
public String toString() {
return "ActiveNumberListResponse{" + "page=" + page + '}';
return "BatchesListResponse{" + "page=" + page + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public DeliveryReportsListResponse(
}

public boolean hasNextPage() {
return (null != page.getNextPageToken() && null != page.getNextPageToken().getToken());
return (null != page.getNextPageToken());
}

public DeliveryReportsListResponse nextPage() {
Expand All @@ -35,8 +35,7 @@ public DeliveryReportsListResponse nextPage() {
}
DeliveryReportListRequestParameters.Builder newParameters =
DeliveryReportListRequestParameters.builder(page.getParameters());
Integer nextToken = page.getNextPageToken().getToken();
newParameters.setPage(nextToken);
newParameters.setPage(page.getNextPageToken());
return service.list(newParameters.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public GroupsListResponse(
}

public boolean hasNextPage() {
return (null != page.getNextPageToken() && null != page.getNextPageToken().getToken());
return (null != page.getNextPageToken());
}

public GroupsListResponse nextPage() {
Expand All @@ -34,8 +34,7 @@ public GroupsListResponse nextPage() {
}
GroupsListRequestParameters.Builder newParameters =
GroupsListRequestParameters.builder(page.getParameters());
Integer nextToken = page.getNextPageToken().getToken();
newParameters.setPage(nextToken);
newParameters.setPage(page.getNextPageToken());
return service.list(newParameters.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public InboundsListResponse(
}

public boolean hasNextPage() {
return (null != page.getNextPageToken() && null != page.getNextPageToken().getToken());
return (null != page.getNextPageToken());
}

public InboundsListResponse nextPage() {
Expand All @@ -34,8 +34,7 @@ public InboundsListResponse nextPage() {
}
InboundsListRequestParameters.Builder newParameters =
InboundsListRequestParameters.builder(page.getParameters());
Integer nextToken = page.getNextPageToken().getToken();
newParameters.setPage(nextToken);
newParameters.setPage(page.getNextPageToken());
return service.list(newParameters.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.sinch.sdk.BaseTest;
import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.core.models.pagination.Page;
import com.sinch.sdk.core.models.pagination.PageToken;
import com.sinch.sdk.core.models.pagination.TokenPageNavigator;
import com.sinch.sdk.domains.numbers.adapters.api.v1.ActiveNumberApi;
import com.sinch.sdk.domains.numbers.adapters.converters.ActiveNumberUpdateRequestParametersDtoConverter;
import com.sinch.sdk.domains.numbers.models.*;
Expand Down Expand Up @@ -89,7 +89,7 @@ void list() throws ApiException {
.setVoiceConfiguration(new VoiceConfiguration("app id", null, null))
.setCallbackUrl("")
.build()),
new PageToken<>(""));
new TokenPageNavigator(""));
Assertions.assertThat(response.getContent())
.usingRecursiveComparison()
.isEqualTo(expected.getEntities());
Expand Down Expand Up @@ -172,7 +172,7 @@ void listWithParameters() throws ApiException {
Instant.parse("2023-09-25T12:08:02.115Z"))))
.setCallbackUrl("foo callback")
.build()),
new PageToken<>("foo"));
new TokenPageNavigator("foo"));

ActiveNumberListResponse response = service.list(parameters);

Expand Down
Loading

0 comments on commit 6b6e031

Please sign in to comment.