Skip to content

Commit

Permalink
feature: SMS Delivery Report (List)
Browse files Browse the repository at this point in the history
  • Loading branch information
JPPortier committed Nov 9, 2023
1 parent 203f305 commit 89d0cf7
Show file tree
Hide file tree
Showing 12 changed files with 527 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.sinch.sdk.domains.sms.models.DeliveryReportBatch;
import com.sinch.sdk.domains.sms.models.DeliveryReportRecipient;
import com.sinch.sdk.domains.sms.models.requests.DeliveryReportBatchGetRequestParameters;
import com.sinch.sdk.domains.sms.models.requests.DeliveryReportListRequestParameters;
import com.sinch.sdk.domains.sms.models.responses.DeliveryReportsListResponse;

/**
* Delivery reports Service
Expand Down Expand Up @@ -50,4 +52,17 @@ DeliveryReportBatch get(String batchId, DeliveryReportBatchGetRequestParameters
* @since 1.0
*/
DeliveryReportRecipient getForNumber(String batchId, String recipient) throws ApiException;

/**
* Get a list of finished delivery reports.<br>
* This operation supports pagination.
*
* @param parameters Filtering parameters
* @return Delivery report related to batchId according to filters
* @see <a
* href="https://developers.sinch.com/docs/sms/api-reference/sms/tag/Delivery-reports/#tag/Delivery-reports/operation/getDeliveryReports">https://developers.sinch.com/docs/sms/api-reference/sms/tag/Delivery-reports/#tag/Delivery-reports/operation/getDeliveryReports</a>
* @since 1.0
*/
DeliveryReportsListResponse list(DeliveryReportListRequestParameters parameters)
throws ApiException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
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.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;
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.dto.v1.DeliveryReportListDto;
import com.sinch.sdk.domains.sms.models.requests.DeliveryReportBatchGetRequestParameters;
import com.sinch.sdk.domains.sms.models.requests.DeliveryReportListRequestParameters;
import com.sinch.sdk.domains.sms.models.responses.DeliveryReportsListResponse;
import com.sinch.sdk.models.Configuration;
import java.time.Instant;
import java.util.Collection;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -68,4 +76,34 @@ public DeliveryReportRecipient getForNumber(String batchId, String recipient)
return DeliveryReportDtoConverter.convert(
getApi().getDeliveryReportByPhoneNumber(configuration.getProjectId(), batchId, recipient));
}

public DeliveryReportsListResponse list(DeliveryReportListRequestParameters parameters)
throws ApiException {
DeliveryReportListRequestParameters guardParameters =
null != parameters ? parameters : DeliveryReportListRequestParameters.builder().build();

DeliveryReportListDto response =
getApi()
.getDeliveryReports(
configuration.getProjectId(),
guardParameters.getPage().orElse(null),
guardParameters.getPageSize().orElse(null),
guardParameters.getStartDate().map(Instant::toString).orElse(null),
guardParameters.getEndDate().map(Instant::toString).orElse(null),
guardParameters
.getStatuses()
.map(f -> f.stream().map(EnumDynamic::value).collect(Collectors.joining(",")))
.orElse(null),
guardParameters
.getCodes()
.map(f -> f.stream().map(Object::toString).collect(Collectors.joining(",")))
.orElse(null),
guardParameters.getClientReference().orElse(null));

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

return new DeliveryReportsListResponse(
this, new Page<>(guardParameters, content.getLeft(), content.getRight()));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sinch.sdk.domains.sms.adapters.converters;

import com.sinch.sdk.core.models.pagination.PageToken;
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 All @@ -10,8 +12,11 @@
import com.sinch.sdk.domains.sms.models.DeliveryReportStatus;
import com.sinch.sdk.domains.sms.models.DeliveryReportStatusDetails;
import com.sinch.sdk.domains.sms.models.dto.v1.DeliveryReportDto;
import com.sinch.sdk.domains.sms.models.dto.v1.DeliveryReportListDto;
import com.sinch.sdk.domains.sms.models.dto.v1.MessageDeliveryStatusDto;
import com.sinch.sdk.domains.sms.models.dto.v1.RecipientDeliveryReportDto;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -68,6 +73,25 @@ public static DeliveryReportRecipient convert(RecipientDeliveryReportDto dto) {
.build();
}

public static Pair<Collection<DeliveryReportRecipient>, PageToken<Integer>> 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;
Collection<RecipientDeliveryReportDto> collection = dto.getDeliveryReports();
Collection<DeliveryReportRecipient> pageContent = new ArrayList<>();
if (null != collection) {
for (RecipientDeliveryReportDto innerDto : collection) {
DeliveryReportRecipient convert = convert(innerDto);
pageContent.add(convert);
}
}
return new Pair<>(pageContent, new PageToken<>(nextPageToken));
}

private static DeliveryReportStatusDetails convert(MessageDeliveryStatusDto dto) {
return DeliveryReportStatusDetails.builder()
.setCode(dto.getCode())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package com.sinch.sdk.domains.sms.models.requests;

import com.sinch.sdk.domains.sms.models.DeliveryReportStatus;
import java.time.Instant;
import java.util.Collection;
import java.util.Optional;

/**
* Parameters request to list delivery reports for a project
*
* @see <a
* href="https://developers.sinch.com/docs/sms/api-reference/sms/tag/Delivery-reports/#tag/Delivery-reports/operation/getDeliveryReports">https://developers.sinch.com/docs/sms/api-reference/sms/tag/Delivery-reports/#tag/Delivery-reports/operation/getDeliveryReports</a>
* @since 1.0
*/
public class DeliveryReportListRequestParameters {

private final Instant startDate;
private final Instant endDate;
private final String clientReference;
private final Integer page;
private final Integer pageSize;
private final Collection<DeliveryReportStatus> statuses;
private final Collection<Integer> codes;

public DeliveryReportListRequestParameters(
Instant startDate,
Instant endDate,
String clientReference,
Integer page,
Integer pageSize,
Collection<DeliveryReportStatus> statuses,
Collection<Integer> codes) {
this.startDate = startDate;
this.endDate = endDate;
this.clientReference = clientReference;
this.page = page;
this.pageSize = pageSize;
this.statuses = statuses;
this.codes = codes;
}

public Optional<Instant> getStartDate() {
return Optional.ofNullable(startDate);
}

public Optional<Instant> getEndDate() {
return Optional.ofNullable(endDate);
}

public Optional<String> getClientReference() {
return Optional.ofNullable(clientReference);
}

public Optional<Integer> getPage() {
return Optional.ofNullable(page);
}

public Optional<Integer> getPageSize() {
return Optional.ofNullable(pageSize);
}

public Optional<Collection<DeliveryReportStatus>> getStatuses() {
return Optional.ofNullable(statuses);
}

public Optional<Collection<Integer>> getCodes() {
return Optional.ofNullable(codes);
}

public static Builder builder() {
return new Builder();
}

public static Builder builder(DeliveryReportListRequestParameters parameters) {
return new Builder(parameters);
}

public static class Builder {

Instant startDate;
Instant endDate;
String clientReference;
Integer page;
Integer pageSize;
Collection<DeliveryReportStatus> statuses;
Collection<Integer> codes;

private Builder() {}

private Builder(DeliveryReportListRequestParameters parameters) {
this.startDate = parameters.startDate;
this.endDate = parameters.endDate;
this.clientReference = parameters.clientReference;
this.page = parameters.page;
this.pageSize = parameters.pageSize;
this.statuses = parameters.statuses;
this.codes = parameters.codes;
}

public Builder setStartDate(Instant startDate) {
this.startDate = startDate;
return this;
}

public Builder setEndDate(Instant endDate) {
this.endDate = endDate;
return this;
}

public Builder setClientReference(String clientReference) {
this.clientReference = clientReference;
return this;
}

public Builder setPage(Integer page) {
this.page = page;
return this;
}

public Builder setPageSize(Integer pageSize) {
this.pageSize = pageSize;
return this;
}

public Builder setStatuses(Collection<DeliveryReportStatus> statuses) {
this.statuses = statuses;
return this;
}

public Builder setCodes(Collection<Integer> codes) {
this.codes = codes;
return this;
}

public DeliveryReportListRequestParameters build() {
return new DeliveryReportListRequestParameters(
startDate, endDate, clientReference, page, pageSize, statuses, codes);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.sinch.sdk.domains.sms.models.responses;

import com.sinch.sdk.core.models.pagination.ListResponse;
import com.sinch.sdk.core.models.pagination.Page;
import com.sinch.sdk.domains.sms.DeliveryReportsService;
import com.sinch.sdk.domains.sms.models.DeliveryReportRecipient;
import com.sinch.sdk.domains.sms.models.requests.DeliveryReportListRequestParameters;
import java.util.Collection;
import java.util.NoSuchElementException;

/**
* Lists batches response
*
* @since 1.0
*/
public class DeliveryReportsListResponse extends ListResponse<DeliveryReportRecipient> {

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

public DeliveryReportsListResponse(
DeliveryReportsService service,
Page<DeliveryReportListRequestParameters, DeliveryReportRecipient, Integer> page) {
this.service = service;
this.page = page;
}

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

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

public Collection<DeliveryReportRecipient> getContent() {
return page.getEntities();
}

@Override
public String toString() {
return "DeliveryReportsListResponse{" + "page=" + page + '}';
}
}
Loading

0 comments on commit 89d0cf7

Please sign in to comment.