Skip to content

Commit

Permalink
fix: 'page_size' returned by SMS API cannot be used to compute total …
Browse files Browse the repository at this point in the history
…pages count to detect end of list/pagination. Use dedicated 'SMSCursorPageNavigator' to compute end of list by checking result size against empty list
  • Loading branch information
JPPortier committed Nov 27, 2023
1 parent 529b7c5 commit 547cfb0
Show file tree
Hide file tree
Showing 26 changed files with 212 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
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.utils.Pair;
import com.sinch.sdk.domains.sms.adapters.api.v1.BatchesApi;
import com.sinch.sdk.domains.sms.adapters.converters.BatchDtoConverter;
import com.sinch.sdk.domains.sms.adapters.converters.DryRunDtoConverter;
import com.sinch.sdk.domains.sms.models.BaseBatch;
import com.sinch.sdk.domains.sms.models.Batch;
import com.sinch.sdk.domains.sms.models.DryRun;
import com.sinch.sdk.domains.sms.models.SMSCursorPageNavigator;
import com.sinch.sdk.domains.sms.models.dto.v1.ApiBatchListDto;
import com.sinch.sdk.domains.sms.models.requests.BatchesListRequestParameters;
import com.sinch.sdk.domains.sms.models.requests.UpdateBaseBatchRequest;
Expand Down Expand Up @@ -76,10 +75,11 @@ public BatchesListResponse list(BatchesListRequestParameters parameters) throws
guardParameters.getEndDate().map(Instant::toString).orElse(null),
guardParameters.getClientReference().orElse(null));

Pair<Collection<Batch<?>>, CursorPageNavigator> content = BatchDtoConverter.convert(response);
Collection<Batch<?>> content = BatchDtoConverter.convert(response);
SMSCursorPageNavigator navigator =
new SMSCursorPageNavigator(response.getPage(), response.getPageSize());

return new BatchesListResponse(
this, new Page<>(guardParameters, content.getLeft(), content.getRight()));
return new BatchesListResponse(this, new Page<>(guardParameters, content, navigator));
}

public <T extends Batch<?>> T update(String batchId, UpdateBaseBatchRequest<?> batch)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
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.utils.EnumDynamic;
import com.sinch.sdk.core.utils.Pair;
import com.sinch.sdk.domains.sms.adapters.api.v1.DeliveryReportsApi;
import com.sinch.sdk.domains.sms.adapters.converters.DeliveryReportDtoConverter;
import com.sinch.sdk.domains.sms.models.DeliveryReportBatch;
import com.sinch.sdk.domains.sms.models.DeliveryReportRecipient;
import com.sinch.sdk.domains.sms.models.DeliveryReportType;
import com.sinch.sdk.domains.sms.models.SMSCursorPageNavigator;
import com.sinch.sdk.domains.sms.models.dto.v1.DeliveryReportListDto;
import com.sinch.sdk.domains.sms.models.requests.DeliveryReportBatchGetRequestParameters;
import com.sinch.sdk.domains.sms.models.requests.DeliveryReportListRequestParameters;
Expand Down Expand Up @@ -104,10 +103,10 @@ public DeliveryReportsListResponse list(DeliveryReportListRequestParameters para
.orElse(null),
guardParameters.getClientReference().orElse(null));

Pair<Collection<DeliveryReportRecipient>, CursorPageNavigator> content =
DeliveryReportDtoConverter.convert(response);
Collection<DeliveryReportRecipient> content = DeliveryReportDtoConverter.convert(response);
SMSCursorPageNavigator navigator =
new SMSCursorPageNavigator(response.getPage(), response.getPageSize());

return new DeliveryReportsListResponse(
this, new Page<>(guardParameters, content.getLeft(), content.getRight()));
return new DeliveryReportsListResponse(this, new Page<>(guardParameters, content, navigator));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
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.utils.Pair;
import com.sinch.sdk.domains.sms.adapters.api.v1.GroupsApi;
import com.sinch.sdk.domains.sms.adapters.converters.GroupsDtoConverter;
import com.sinch.sdk.domains.sms.models.Group;
import com.sinch.sdk.domains.sms.models.SMSCursorPageNavigator;
import com.sinch.sdk.domains.sms.models.dto.v1.ApiGroupListDto;
import com.sinch.sdk.domains.sms.models.dto.v1.CreateGroupResponseDto;
import com.sinch.sdk.domains.sms.models.requests.GroupCreateRequestParameters;
Expand Down Expand Up @@ -70,10 +69,11 @@ public GroupsListResponse list(GroupsListRequestParameters parameters) throws Ap
guardParameters.getPage().orElse(null),
guardParameters.getPageSize().orElse(null));

Pair<Collection<Group>, CursorPageNavigator> content = GroupsDtoConverter.convert(response);
Collection<Group> content = GroupsDtoConverter.convert(response);
SMSCursorPageNavigator navigator =
new SMSCursorPageNavigator(response.getPage(), response.getPageSize());

return new GroupsListResponse(
this, new Page<>(guardParameters, content.getLeft(), content.getRight()));
return new GroupsListResponse(this, new Page<>(guardParameters, content, navigator));
}

public Group replace(String groupId, GroupReplaceRequestParameters parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
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.utils.Pair;
import com.sinch.sdk.domains.sms.adapters.api.v1.InboundsApi;
import com.sinch.sdk.domains.sms.adapters.converters.InboundsDtoConverter;
import com.sinch.sdk.domains.sms.models.Inbound;
import com.sinch.sdk.domains.sms.models.SMSCursorPageNavigator;
import com.sinch.sdk.domains.sms.models.dto.v1.ApiInboundListDto;
import com.sinch.sdk.domains.sms.models.dto.v1.InboundDto;
import com.sinch.sdk.domains.sms.models.requests.InboundsListRequestParameters;
Expand Down Expand Up @@ -52,11 +51,11 @@ public InboundsListResponse list(InboundsListRequestParameters parameters) throw
guardParameters.getEndDate().map(Instant::toString).orElse(null),
guardParameters.getClientReference().orElse(null));

Pair<Collection<Inbound<?>>, CursorPageNavigator> content =
InboundsDtoConverter.convert(response);
Collection<Inbound<?>> content = InboundsDtoConverter.convert(response);
SMSCursorPageNavigator navigator =
new SMSCursorPageNavigator(response.getPage(), response.getPageSize());

return new InboundsListResponse(
this, new Page<>(guardParameters, content.getLeft(), content.getRight()));
return new InboundsListResponse(this, new Page<>(guardParameters, content, navigator));
}

public Inbound<?> get(String inboundId) throws ApiException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.core.models.AbstractOpenApiSchema;
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;
import com.sinch.sdk.domains.sms.models.BatchBinary;
Expand Down Expand Up @@ -269,10 +267,8 @@ 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>, CursorPageNavigator> convert(
ApiBatchListDto dto) {
CursorPageNavigator navigator =
new CursorPageNavigator(dto.getPage(), dto.getPageSize(), dto.getCount());
public static <T extends Batch<?>> Collection<T> convert(ApiBatchListDto dto) {

Collection<ApiBatchListBatchesInnerDto> collection = dto.getBatches();
Collection<T> pageContent = new ArrayList<>();
if (null != collection) {
Expand All @@ -281,7 +277,7 @@ public static <T extends Batch<?>> Pair<Collection<T>, CursorPageNavigator> conv
pageContent.add(convert);
}
}
return new Pair<>(pageContent, navigator);
return pageContent;
}

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

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;
import com.sinch.sdk.domains.sms.models.DeliveryReportBatchSMS;
Expand Down Expand Up @@ -74,10 +72,7 @@ public static DeliveryReportRecipient convert(RecipientDeliveryReportDto dto) {
.build();
}

public static Pair<Collection<DeliveryReportRecipient>, CursorPageNavigator> convert(
DeliveryReportListDto dto) {
CursorPageNavigator navigator =
new CursorPageNavigator(dto.getPage(), dto.getPageSize(), dto.getCount());
public static Collection<DeliveryReportRecipient> convert(DeliveryReportListDto dto) {
Collection<RecipientDeliveryReportDto> collection = dto.getDeliveryReports();
Collection<DeliveryReportRecipient> pageContent = new ArrayList<>();
if (null != collection) {
Expand All @@ -86,7 +81,7 @@ public static Pair<Collection<DeliveryReportRecipient>, CursorPageNavigator> con
pageContent.add(convert);
}
}
return new Pair<>(pageContent, navigator);
return pageContent;
}

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

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;
import com.sinch.sdk.domains.sms.models.GroupAutoUpdate;
import com.sinch.sdk.domains.sms.models.GroupAutoUpdateKeyword;
Expand Down Expand Up @@ -51,9 +49,8 @@ public static GroupObjectDto convert(GroupCreateRequestParameters client) {
.autoUpdate(client.getAutoUpdate().map(GroupsDtoConverter::convert).orElse(null));
}

public static Pair<Collection<Group>, CursorPageNavigator> convert(ApiGroupListDto dto) {
CursorPageNavigator navigator =
new CursorPageNavigator(dto.getPage(), dto.getPageSize(), dto.getCount());
public static Collection<Group> convert(ApiGroupListDto dto) {

Collection<ApiGroupDto> collection = dto.getGroups();
Collection<Group> pageContent = new ArrayList<>();
if (null != collection) {
Expand All @@ -62,7 +59,7 @@ public static Pair<Collection<Group>, CursorPageNavigator> convert(ApiGroupListD
pageContent.add(convert);
}
}
return new Pair<>(pageContent, navigator);
return pageContent;
}

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

import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.core.models.AbstractOpenApiSchema;
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;
import com.sinch.sdk.domains.sms.models.InboundText;
Expand All @@ -27,9 +25,8 @@ public static Inbound<?> convert(AbstractOpenApiSchema dto) {
}
}

public static Pair<Collection<Inbound<?>>, CursorPageNavigator> convert(ApiInboundListDto dto) {
CursorPageNavigator navigator =
new CursorPageNavigator(dto.getPage(), dto.getPageSize(), dto.getCount());
public static Collection<Inbound<?>> convert(ApiInboundListDto dto) {

Collection<InboundDto> collection = dto.getInbounds();
Collection<Inbound<?>> pageContent = new ArrayList<>();
if (null != collection) {
Expand All @@ -38,7 +35,7 @@ public static Pair<Collection<Inbound<?>>, CursorPageNavigator> convert(ApiInbou
pageContent.add(convert);
}
}
return new Pair<>(pageContent, navigator);
return pageContent;
}

public static InboundBinary convert(MOBinaryDto dto) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.sinch.sdk.domains.sms.models;

import com.sinch.sdk.core.models.pagination.PageNavigator;

public class SMSCursorPageNavigator extends PageNavigator<Integer> {

private final Integer currentPage;
private final Integer pageSize;

public SMSCursorPageNavigator(Integer currentPage, Integer pageSize) {
super(null);
this.currentPage = currentPage;
this.pageSize = pageSize;
}

private Integer computeNextPageCursor() {
return null == pageSize || pageSize == 0 ? null : currentPage + 1;
}

@Override
public Integer getToken() {
return computeNextPageCursor();
}

@Override
public String toString() {
return "SMSCursorPageNavigator{"
+ "currentPage="
+ currentPage
+ ", pageSize="
+ pageSize
+ "} "
+ super.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class BatchesListResponse extends ListResponse<Batch<?>> {

private final Page<BatchesListRequestParameters, Batch<?>, Integer> page;
private final BatchesService service;
private BatchesListResponse nextPage;

public BatchesListResponse(
BatchesService service, Page<BatchesListRequestParameters, Batch<?>, Integer> page) {
Expand All @@ -25,17 +26,25 @@ public BatchesListResponse(
}

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

if (null == nextPage) {
BatchesListRequestParameters.Builder newParameters =
BatchesListRequestParameters.builder(page.getParameters());
newParameters.setPage(page.getNextPageToken());
nextPage = service.list(newParameters.build());
}
return (null != nextPage.getContent() && !nextPage.getContent().isEmpty());
}

public BatchesListResponse nextPage() {

if (!hasNextPage()) {
throw new NoSuchElementException("Reached the last page of the API response");
}
BatchesListRequestParameters.Builder newParameters =
BatchesListRequestParameters.builder(page.getParameters());
newParameters.setPage(page.getNextPageToken());
return service.list(newParameters.build());

BatchesListResponse response = nextPage;
nextPage = null;
return response;
}

public Collection<Batch<?>> getContent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class DeliveryReportsListResponse extends ListResponse<DeliveryReportReci

private final Page<DeliveryReportListRequestParameters, DeliveryReportRecipient, Integer> page;
private final DeliveryReportsService service;
private DeliveryReportsListResponse nextPage;

public DeliveryReportsListResponse(
DeliveryReportsService service,
Expand All @@ -26,17 +27,23 @@ public DeliveryReportsListResponse(
}

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

if (null == nextPage) {
DeliveryReportListRequestParameters.Builder newParameters =
DeliveryReportListRequestParameters.builder(page.getParameters());
newParameters.setPage(page.getNextPageToken());
nextPage = service.list(newParameters.build());
}
return (null != nextPage.getContent() && !nextPage.getContent().isEmpty());
}

public DeliveryReportsListResponse nextPage() {
if (!hasNextPage()) {
throw new NoSuchElementException("Reached the last page of the API response");
}
DeliveryReportListRequestParameters.Builder newParameters =
DeliveryReportListRequestParameters.builder(page.getParameters());
newParameters.setPage(page.getNextPageToken());
return service.list(newParameters.build());
DeliveryReportsListResponse response = nextPage;
nextPage = null;
return response;
}

public Collection<DeliveryReportRecipient> getContent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class GroupsListResponse extends ListResponse<Group> {

private final Page<GroupsListRequestParameters, Group, Integer> page;
private final GroupsService service;
private GroupsListResponse nextPage;

public GroupsListResponse(
GroupsService service, Page<GroupsListRequestParameters, Group, Integer> page) {
Expand All @@ -25,17 +26,23 @@ public GroupsListResponse(
}

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

if (null == nextPage) {
GroupsListRequestParameters.Builder newParameters =
GroupsListRequestParameters.builder(page.getParameters());
newParameters.setPage(page.getNextPageToken());
nextPage = service.list(newParameters.build());
}
return (null != nextPage.getContent() && !nextPage.getContent().isEmpty());
}

public GroupsListResponse nextPage() {
if (!hasNextPage()) {
throw new NoSuchElementException("Reached the last page of the API response");
}
GroupsListRequestParameters.Builder newParameters =
GroupsListRequestParameters.builder(page.getParameters());
newParameters.setPage(page.getNextPageToken());
return service.list(newParameters.build());
GroupsListResponse response = nextPage;
nextPage = null;
return response;
}

public Collection<Group> getContent() {
Expand Down
Loading

0 comments on commit 547cfb0

Please sign in to comment.