Skip to content

Commit

Permalink
Feat/sms: batches list (#3)
Browse files Browse the repository at this point in the history
* feat (DEVEXP-166): Batches/SMS: get List
  • Loading branch information
JPPortier authored Nov 6, 2023
1 parent b7c53c6 commit b02440e
Show file tree
Hide file tree
Showing 25 changed files with 649 additions and 54 deletions.
12 changes: 12 additions & 0 deletions client/src/main/com/sinch/sdk/domains/sms/BatchesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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.requests.BatchesListRequestParameters;
import com.sinch.sdk.domains.sms.models.responses.BatchesListResponse;

/**
* Batches Service
Expand Down Expand Up @@ -47,7 +49,17 @@ public interface BatchesService {
* response
* @param batch The batch to be send
* @return Details about dryRun execution
* @since 1.0
*/
DryRun dryRun(boolean perRecipient, int numberOfRecipient, BaseBatch<?> batch)
throws ApiException;

/**
* With the list operation you can list batch messages created in the last 14 days that you have
* created. This operation supports pagination.
*
* @param parameters Query parameters filtering returned batches
* @since 1.0
*/
BatchesListResponse list(BatchesListRequestParameters parameters) throws ApiException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
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.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.dto.v1.ApiBatchListDto;
import com.sinch.sdk.domains.sms.models.requests.BatchesListRequestParameters;
import com.sinch.sdk.domains.sms.models.responses.BatchesListResponse;
import com.sinch.sdk.models.Configuration;
import java.time.Instant;
import java.util.Collection;

public class BatchesService implements com.sinch.sdk.domains.sms.BatchesService {

Expand Down Expand Up @@ -46,4 +54,26 @@ public DryRun dryRun(boolean perRecipient, int numberOfRecipient, BaseBatch<?> b
numberOfRecipient,
BatchDtoConverter.convert(batch)));
}

public BatchesListResponse list(BatchesListRequestParameters parameters) throws ApiException {

BatchesListRequestParameters guardParameters =
null != parameters ? parameters : BatchesListRequestParameters.builder().build();

ApiBatchListDto response =
getApi()
.listBatches(
configuration.getProjectId(),
guardParameters.getPage().orElse(null),
guardParameters.getPageSize().orElse(null),
guardParameters.getFrom().orElse(null),
guardParameters.getStartDate().map(Instant::toString).orElse(null),
guardParameters.getEndDate().map(Instant::toString).orElse(null),
guardParameters.getClientReference().orElse(null));

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

return new BatchesListResponse(
this, new Page<>(guardParameters, content.getLeft(), content.getRight()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@
import static com.sinch.sdk.domains.sms.models.dto.v1.TextRequestDto.TypeEnum.MT_TEXT;

import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.domains.sms.models.*;
import com.sinch.sdk.core.models.AbstractOpenApiSchema;
import com.sinch.sdk.core.models.pagination.PageToken;
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;
import com.sinch.sdk.domains.sms.models.BatchMedia;
import com.sinch.sdk.domains.sms.models.BatchText;
import com.sinch.sdk.domains.sms.models.DeliveryReport;
import com.sinch.sdk.domains.sms.models.MediaBody;
import com.sinch.sdk.domains.sms.models.dto.v1.ApiBatchListBatchesInnerDto;
import com.sinch.sdk.domains.sms.models.dto.v1.ApiBatchListDto;
import com.sinch.sdk.domains.sms.models.dto.v1.BinaryRequestDto;
import com.sinch.sdk.domains.sms.models.dto.v1.BinaryResponseDto;
import com.sinch.sdk.domains.sms.models.dto.v1.MediaBodyDto;
import com.sinch.sdk.domains.sms.models.dto.v1.MediaRequestDto;
import com.sinch.sdk.domains.sms.models.dto.v1.MediaResponseDto;
import com.sinch.sdk.domains.sms.models.dto.v1.SendSMS201ResponseDto;
import com.sinch.sdk.domains.sms.models.dto.v1.SendSMSRequestDto;
import com.sinch.sdk.domains.sms.models.dto.v1.TextRequestDto;
import com.sinch.sdk.domains.sms.models.dto.v1.TextResponseDto;
Expand All @@ -20,22 +30,23 @@
import com.sinch.sdk.domains.sms.models.requests.SendSmsBatchTextRequest;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collection;

public class BatchDtoConverter {

public static <T extends Batch<?>> T convert(SendSMS201ResponseDto dto) {
public static <T extends Batch<?>> T convert(AbstractOpenApiSchema dto) {
Object obj = dto.getActualInstance();
if (obj instanceof BinaryResponseDto) {
@SuppressWarnings("unchecked")
T t = (T) convertBinary(dto.getBinaryResponseDto());
T t = (T) convertBinary((BinaryResponseDto) obj);
return t;
} else if (obj instanceof MediaResponseDto) {
@SuppressWarnings("unchecked")
T t = (T) convertMedia(dto.getMediaResponseDto());
T t = (T) convertMedia((MediaResponseDto) obj);
return t;
} else if (obj instanceof TextResponseDto) {
@SuppressWarnings("unchecked")
T t = (T) convertText(dto.getTextResponseDto());
T t = (T) convertText((TextResponseDto) obj);
return t;
} else {
throw new ApiException("Unexpected class:" + obj.getClass().getName());
Expand Down Expand Up @@ -191,4 +202,23 @@ private static SendSMSRequestDto convert(SendSmsBatchTextRequest value) {
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(
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;
Collection<ApiBatchListBatchesInnerDto> collection = dto.getBatches();
Collection<T> pageContent = new ArrayList<>();
if (null != collection) {
for (ApiBatchListBatchesInnerDto apiBatchListBatchesInnerDto : collection) {
T convert = convert(apiBatchListBatchesInnerDto);
pageContent.add(convert);
}
}
return new Pair<>(pageContent, new PageToken<>(nextPageToken));
}
}
54 changes: 27 additions & 27 deletions client/src/main/com/sinch/sdk/domains/sms/models/BatchBinary.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
* @since 1.0
*/
public class BatchBinary extends Batch<String> {
private final boolean flashMessage;
private final boolean truncateConcat;
private final int maxNumberOfMessageParts;
private final int fromTon;
private final int fromNpi;
private final Boolean flashMessage;
private final Boolean truncateConcat;
private final Integer maxNumberOfMessageParts;
private final Integer fromTon;
private final Integer fromNpi;
private final String udh;

/**
Expand Down Expand Up @@ -46,7 +46,7 @@ public BatchBinary(
String id,
Collection<String> to,
String from,
boolean canceled,
Boolean canceled,
String body,
Instant createdAt,
Instant modifiedAt,
Expand All @@ -55,12 +55,12 @@ public BatchBinary(
Instant expireAt,
String callbackUrl,
String clientReference,
boolean feedbackEnabled,
boolean flashMessage,
boolean truncateConcat,
int maxNumberOfMessageParts,
int fromTon,
int fromNpi,
Boolean feedbackEnabled,
Boolean flashMessage,
Boolean truncateConcat,
Integer maxNumberOfMessageParts,
Integer fromTon,
Integer fromNpi,
String udh) {
super(
id,
Expand All @@ -84,23 +84,23 @@ public BatchBinary(
this.udh = udh;
}

public boolean isFlashMessage() {
public Boolean isFlashMessage() {
return flashMessage;
}

public boolean isTruncateConcat() {
public Boolean isTruncateConcat() {
return truncateConcat;
}

public int getMaxNumberOfMessageParts() {
public Integer getMaxNumberOfMessageParts() {
return maxNumberOfMessageParts;
}

public int getFromTon() {
public Integer getFromTon() {
return fromTon;
}

public int getFromNpi() {
public Integer getFromNpi() {
return fromNpi;
}

Expand Down Expand Up @@ -134,36 +134,36 @@ public String toString() {

public static class Builder extends Batch.Builder<String, Builder> {

private boolean flashMessage;
private boolean truncateConcat;
private int maxNumberOfMessageParts;
private int fromTon;
private int fromNpi;
private Boolean flashMessage;
private Boolean truncateConcat;
private Integer maxNumberOfMessageParts;
private Integer fromTon;
private Integer fromNpi;
private String udh;

private Builder() {}

public Builder setFlashMessage(boolean flashMessage) {
public Builder setFlashMessage(Boolean flashMessage) {
this.flashMessage = flashMessage;
return this;
}

public Builder setTruncateConcat(boolean truncateConcat) {
public Builder setTruncateConcat(Boolean truncateConcat) {
this.truncateConcat = truncateConcat;
return this;
}

public Builder setMaxNumberOfMessageParts(int maxNumberOfMessageParts) {
public Builder setMaxNumberOfMessageParts(Integer maxNumberOfMessageParts) {
this.maxNumberOfMessageParts = maxNumberOfMessageParts;
return this;
}

public Builder setFromTon(int fromTon) {
public Builder setFromTon(Integer fromTon) {
this.fromTon = fromTon;
return this;
}

public Builder setFromNpi(int fromNpi) {
public Builder setFromNpi(Integer fromNpi) {
this.fromNpi = fromNpi;
return this;
}
Expand Down
14 changes: 7 additions & 7 deletions client/src/main/com/sinch/sdk/domains/sms/models/BatchMedia.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
public class BatchMedia extends Batch<MediaBody> {
private final Parameters parameters;
private final boolean strictValidation;
private final Boolean strictValidation;

/**
* @param id Unique identifier for batch
Expand Down Expand Up @@ -42,7 +42,7 @@ public BatchMedia(
String id,
Collection<String> to,
String from,
boolean canceled,
Boolean canceled,
MediaBody body,
Instant createdAt,
Instant modifiedAt,
Expand All @@ -51,9 +51,9 @@ public BatchMedia(
Instant expireAt,
String callbackUrl,
String clientReference,
boolean feedbackEnabled,
Boolean feedbackEnabled,
Parameters parameters,
boolean strictValidation) {
Boolean strictValidation) {
super(
id,
to,
Expand All @@ -76,7 +76,7 @@ public Parameters getParameters() {
return parameters;
}

public boolean isStrictValidation() {
public Boolean isStrictValidation() {
return strictValidation;
}

Expand All @@ -97,7 +97,7 @@ public String toString() {

public static class Builder extends Batch.Builder<MediaBody, Builder> {
private Parameters parameters;
private boolean strictValidation;
private Boolean strictValidation;

private Builder() {}

Expand All @@ -106,7 +106,7 @@ public Builder setParameters(Parameters parameters) {
return this;
}

public Builder setStrictValidation(boolean strictValidation) {
public Builder setStrictValidation(Boolean strictValidation) {
this.strictValidation = strictValidation;
return this;
}
Expand Down
Loading

0 comments on commit b02440e

Please sign in to comment.