Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batches: "update", "replace", "cancel" requests #4

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 94 additions & 6 deletions client/src/main/com/sinch/sdk/domains/sms/BatchesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
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.requests.UpdateBaseBatchRequest;
import com.sinch.sdk.domains.sms.models.responses.BatchesListResponse;
import java.util.Collection;

/**
* Batches Service
Expand All @@ -17,30 +19,37 @@
public interface BatchesService {

/**
* Get a batch message
* Get a batch message <br>
* This operation returns a specific batch that matches the provided batch ID.
*
* @param batchId The batch ID you received from sending a message
* @param <T> A type of Batch
* @return Batch information
* @see <a
* href="https://developers.sinch.com/docs/sms/api-reference/sms/tag/Batches/#tag/Batches/operation/GetBatchMessage">https://developers.sinch.com/docs/sms/api-reference/sms/tag/Batches/#tag/Batches/operation/GetBatchMessage</a>
* @since 1.0
*/
<T extends Batch<?>> T get(String batchId) throws ApiException;

/**
* Send a message or a batch of messages Depending on the length of the body, one message might be
* split into multiple parts and charged accordingly.
*
* <p>Any groups targeted in a scheduled batch will be evaluated at the time of sending. If a
* group is deleted between batch creation and scheduled date, it will be considered empty.
* Send a message or a batch of messages <br>
* Depending on the length of the body, one message might be split into multiple parts and charged
* accordingly. <br>
* Any groups targeted in a scheduled batch will be evaluated at the time of sending. If a group
* is deleted between batch creation and scheduled date, it will be considered empty. <br>
* Be sure to use the correct region in the server URL.
*
* @param batch The batch to be created
* @param <T> A type of Batch
* @return Batch information
* @see <a
* href="https://developers.sinch.com/docs/sms/api-reference/sms/tag/Batches/#tag/Batches/operation/SendSMS">https://developers.sinch.com/docs/sms/api-reference/sms/tag/Batches/#tag/Batches/operation/SendSMS</a>
* @since 1.0
*/
<T extends Batch<?>> T send(BaseBatch<?> batch) throws ApiException;

/**
* Dry run <br>
* This operation will perform a dry run of a batch which calculates the bodies and number of
* parts for all messages in the batch without actually sending any messages.
*
Expand All @@ -49,17 +58,96 @@ public interface BatchesService {
* response
* @param batch The batch to be send
* @return Details about dryRun execution
* @see <a
* href="https://developers.sinch.com/docs/sms/api-reference/sms/tag/Batches/#tag/Batches/operation/Dry_Run">https://developers.sinch.com/docs/sms/api-reference/sms/tag/Batches/#tag/Batches/operation/Dry_Run</a>
* @since 1.0
*/
DryRun dryRun(boolean perRecipient, int numberOfRecipient, BaseBatch<?> batch)
throws ApiException;

/**
* List Batches <br>
* 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
* @return Paginated list of Batches
* @since 1.0
* @see <a
* href="https://developers.sinch.com/docs/sms/api-reference/sms/tag/Batches/#tag/Batches/operation/ListBatches">https://developers.sinch.com/docs/sms/api-reference/sms/tag/Batches/#tag/Batches/operation/ListBatches</a>
*/
BatchesListResponse list(BatchesListRequestParameters parameters) throws ApiException;

/**
* Update a Batch message <br>
* This operation updates all specified parameters of a batch that matches the provided batch ID.
*
* @param batchId The batch ID you received from sending a message
* @param batch Batch parameters to be updated
* @param <T> A type of Batch
* @return Batch information
* @see <a
* href="https://developers.sinch.com/docs/sms/api-reference/sms/tag/Batches/#tag/Batches/operation/UpdateBatchMessage">https://developers.sinch.com/docs/sms/api-reference/sms/tag/Batches/#tag/Batches/operation/UpdateBatchMessage</a>
* @since 1.0
*/
<T extends Batch<?>> T update(String batchId, UpdateBaseBatchRequest<?> batch)
throws ApiException;

/**
* Replace a batch <br>
* This operation will replace all the parameters of a batch with the provided values. It is the
* same as cancelling a batch and sending a new one instead.
*
* @param batchId The batch ID you received from sending a message
* @param batch Batch parameters to be used
* @param <T> A type of Batch
* @return Batch information
* @see <a
* href="https://developers.sinch.com/docs/sms/api-reference/sms/tag/Batches/#tag/Batches/operation/ReplaceBatch">https://developers.sinch.com/docs/sms/api-reference/sms/tag/Batches/#tag/Batches/operation/ReplaceBatch</a>
* @since 1.0
*/
<T extends Batch<?>> T replace(String batchId, BaseBatch<?> batch) throws ApiException;

/**
* Cancel a batch message <br>
* A batch can be canceled at any point. If a batch is canceled while it's currently being
* delivered some messages currently being processed might still be delivered. The delivery report
* will indicate which messages were canceled and which weren't. <br>
* Canceling a batch scheduled in the future will result in an empty delivery report while
* canceling an already sent batch would result in no change to the completed delivery report.
*
* @param batchId The batch ID you received from sending a message
* @param <T> A type of Batch
* @return Batch information
* @see <a
* href="https://developers.sinch.com/docs/sms/api-reference/sms/tag/Batches/#tag/Batches/operation/CancelBatchMessage">https://developers.sinch.com/docs/sms/api-reference/sms/tag/Batches/#tag/Batches/operation/CancelBatchMessage</a>
* @since 1.0
*/
<T extends Batch<?>> T cancel(String batchId) throws ApiException;

/**
* Send delivery feedback for a message
*
* <p>Send feedback if your system can confirm successful message delivery.
*
* <p>Feedback can only be provided if feedback_enabled was set when batch was submitted.
*
* <p><em>Batches</em>: It is possible to submit feedback multiple times for the same batch for
* different recipients. Feedback without specified recipients is treated as successful message
* delivery to all recipients referenced in the batch. Note that the recipients key is still
* required even if the value is empty.
*
* <p><em>Groups</em>: If the batch message was creating using a group ID, at least one recipient
* is required. Excluding recipients (an empty recipient list) does not work and will result in a
* failed request.
*
* @param batchId The batch ID you received from sending a message
* @param recipients A list of phone numbers (MSISDNs) that have successfully received the
* message. The key is required, however, the value can be an empty array ([]) for a batch. If
* the feedback was enabled for a group, at least one phone number is required.
* @see <a
* href="https://developers.sinch.com/docs/sms/api-reference/sms/tag/Batches/#tag/Batches/operation/deliveryFeedback">https://developers.sinch.com/docs/sms/api-reference/sms/tag/Batches/#tag/Batches/operation/deliveryFeedback</a>
* @since 1.0
*/
void sendDeliveryFeedback(String batchId, Collection<String> recipients) throws ApiException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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.requests.UpdateBaseBatchRequest;
import com.sinch.sdk.domains.sms.models.responses.BatchesListResponse;
import com.sinch.sdk.models.Configuration;
import java.time.Instant;
Expand Down Expand Up @@ -76,4 +77,30 @@ public BatchesListResponse list(BatchesListRequestParameters parameters) throws
return new BatchesListResponse(
this, new Page<>(guardParameters, content.getLeft(), content.getRight()));
}

public <T extends Batch<?>> T update(String batchId, UpdateBaseBatchRequest<?> batch)
throws ApiException {
return BatchDtoConverter.convert(
getApi()
.updateBatchMessage(
configuration.getProjectId(), batchId, BatchDtoConverter.convert(batch)));
}

public <T extends Batch<?>> T replace(String batchId, BaseBatch<?> batch) throws ApiException {
return BatchDtoConverter.convert(
getApi()
.replaceBatch(configuration.getProjectId(), batchId, BatchDtoConverter.convert(batch)));
}

public <T extends Batch<?>> T cancel(String batchId) throws ApiException {
return BatchDtoConverter.convert(
getApi().cancelBatchMessage(configuration.getProjectId(), batchId));
}

public void sendDeliveryFeedback(String batchId, Collection<String> recipients)
throws ApiException {
getApi()
.deliveryFeedback(
configuration.getProjectId(), batchId, BatchDtoConverter.convert(recipients));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.sinch.sdk.domains.sms.adapters.converters;

import static com.sinch.sdk.domains.sms.models.dto.v1.BinaryRequestDto.TypeEnum.MT_BINARY;
import static com.sinch.sdk.domains.sms.models.dto.v1.MediaRequestDto.TypeEnum.MT_MEDIA;
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.core.models.AbstractOpenApiSchema;
import com.sinch.sdk.core.models.pagination.PageToken;
Expand All @@ -17,6 +13,10 @@
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.ApiDeliveryFeedbackDto;
import com.sinch.sdk.domains.sms.models.dto.v1.ApiUpdateBinaryMtMessageDto;
import com.sinch.sdk.domains.sms.models.dto.v1.ApiUpdateMmsMtMessageDto;
import com.sinch.sdk.domains.sms.models.dto.v1.ApiUpdateTextMtMessageDto;
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;
Expand All @@ -25,9 +25,14 @@
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;
import com.sinch.sdk.domains.sms.models.dto.v1.UpdateBatchMessageRequestDto;
import com.sinch.sdk.domains.sms.models.requests.SendSmsBatchBinaryRequest;
import com.sinch.sdk.domains.sms.models.requests.SendSmsBatchMediaRequest;
import com.sinch.sdk.domains.sms.models.requests.SendSmsBatchTextRequest;
import com.sinch.sdk.domains.sms.models.requests.UpdateBaseBatchRequest;
import com.sinch.sdk.domains.sms.models.requests.UpdateSmsBatchBinaryRequest;
import com.sinch.sdk.domains.sms.models.requests.UpdateSmsBatchMediaRequest;
import com.sinch.sdk.domains.sms.models.requests.UpdateSmsBatchTextRequest;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -138,10 +143,22 @@ public static SendSMSRequestDto convert(BaseBatch<?> value) {
}
}

public static UpdateBatchMessageRequestDto convert(UpdateBaseBatchRequest<?> value) {
if (value instanceof UpdateSmsBatchBinaryRequest) {
return convert((UpdateSmsBatchBinaryRequest) value);
} else if (value instanceof UpdateSmsBatchMediaRequest) {
return convert((UpdateSmsBatchMediaRequest) value);
} else if (value instanceof UpdateSmsBatchTextRequest) {
return convert((UpdateSmsBatchTextRequest) value);
} else {
throw new ApiException("Unexpected class:" + value.getClass().getName());
}
}

private static SendSMSRequestDto convert(SendSmsBatchBinaryRequest value) {
BinaryRequestDto dto =
new BinaryRequestDto()
.type(MT_BINARY.getValue())
.type(BinaryRequestDto.TypeEnum.MT_BINARY.getValue())
.to(new ArrayList<>(value.getTo()))
.body(value.getBody());
value.getFrom().ifPresent(dto::from);
Expand All @@ -162,7 +179,7 @@ private static SendSMSRequestDto convert(SendSmsBatchBinaryRequest value) {

private static SendSMSRequestDto convert(SendSmsBatchMediaRequest value) {
MediaRequestDto dto =
new MediaRequestDto(MT_MEDIA.getValue())
new MediaRequestDto(MediaRequestDto.TypeEnum.MT_MEDIA.getValue())
.to(new ArrayList<>(value.getTo()))
.body(convert(value.getBody()));
value.getFrom().ifPresent(dto::from);
Expand All @@ -180,7 +197,7 @@ private static SendSMSRequestDto convert(SendSmsBatchMediaRequest value) {
private static SendSMSRequestDto convert(SendSmsBatchTextRequest value) {
TextRequestDto dto =
new TextRequestDto()
.type(MT_TEXT.getValue())
.type(TextRequestDto.TypeEnum.MT_TEXT.getValue())
.to(new ArrayList<>(value.getTo()))
.body(value.getBody());
value.getFrom().ifPresent(dto::from);
Expand All @@ -199,6 +216,55 @@ private static SendSMSRequestDto convert(SendSmsBatchTextRequest value) {
return new SendSMSRequestDto(dto);
}

private static UpdateBatchMessageRequestDto convert(UpdateSmsBatchTextRequest value) {
ApiUpdateTextMtMessageDto dto =
new ApiUpdateTextMtMessageDto().type(ApiUpdateTextMtMessageDto.TypeEnum.MT_TEXT.getValue());

value.getToAdd().ifPresent(f -> dto.toAdd(new ArrayList<>(f)));
value.getToRemove().ifPresent(f -> dto.toRemove(new ArrayList<>(f)));
value.getFrom().ifPresent(dto::from);
value.getBody().ifPresent(dto::body);
value.getDeliveryReport().ifPresent(f -> dto.setDeliveryReport(f.value()));
value.getSendAt().ifPresent(f -> dto.setSendAt(f.atOffset(ZoneOffset.UTC)));
value.getExpireAt().ifPresent(f -> dto.setExpireAt(f.atOffset(ZoneOffset.UTC)));
value.getCallbackUrl().ifPresent(dto::callbackUrl);
value.getParameters().ifPresent(f -> dto.setParameters(ParametersDtoConverter.convert(f)));
return new UpdateBatchMessageRequestDto(dto);
}

private static UpdateBatchMessageRequestDto convert(UpdateSmsBatchMediaRequest value) {
ApiUpdateMmsMtMessageDto dto =
new ApiUpdateMmsMtMessageDto().type(ApiUpdateMmsMtMessageDto.TypeEnum.MT_MEDIA.getValue());

value.getToAdd().ifPresent(f -> dto.toAdd(new ArrayList<>(f)));
value.getToRemove().ifPresent(f -> dto.toRemove(new ArrayList<>(f)));
value.getFrom().ifPresent(dto::from);
value.getBody().ifPresent(f -> dto.setBody(convert(f)));
value.getDeliveryReport().ifPresent(f -> dto.setDeliveryReport(f.value()));
value.getSendAt().ifPresent(f -> dto.setSendAt(f.atOffset(ZoneOffset.UTC)));
value.getExpireAt().ifPresent(f -> dto.setExpireAt(f.atOffset(ZoneOffset.UTC)));
value.getCallbackUrl().ifPresent(dto::callbackUrl);
value.getParameters().ifPresent(f -> dto.setParameters(ParametersDtoConverter.convert(f)));
value.isStrictValidation().ifPresent(dto::strictValidation);
return new UpdateBatchMessageRequestDto(dto);
}

private static UpdateBatchMessageRequestDto convert(UpdateSmsBatchBinaryRequest value) {
ApiUpdateBinaryMtMessageDto dto =
new ApiUpdateBinaryMtMessageDto()
.type(ApiUpdateBinaryMtMessageDto.TypeEnum.MT_BINARY.getValue());
value.getToAdd().ifPresent(f -> dto.toAdd(new ArrayList<>(f)));
value.getToRemove().ifPresent(f -> dto.toRemove(new ArrayList<>(f)));
value.getFrom().ifPresent(dto::from);
value.getBody().ifPresent(dto::setBody);
value.getDeliveryReport().ifPresent(f -> dto.setDeliveryReport(f.value()));
value.getSendAt().ifPresent(f -> dto.setSendAt(f.atOffset(ZoneOffset.UTC)));
value.getExpireAt().ifPresent(f -> dto.setExpireAt(f.atOffset(ZoneOffset.UTC)));
value.getCallbackUrl().ifPresent(dto::callbackUrl);
value.getUdh().ifPresent(dto::udh);
return new UpdateBatchMessageRequestDto(dto);
}

private static MediaBodyDto convert(MediaBody value) {
return new MediaBodyDto().url(value.getUrl()).message(value.getMessage().orElse(null));
}
Expand All @@ -221,4 +287,8 @@ public static <T extends Batch<?>> Pair<Collection<T>, PageToken<Integer>> conve
}
return new Pair<>(pageContent, new PageToken<>(nextPageToken));
}

public static ApiDeliveryFeedbackDto convert(Collection<String> recipients) {
return new ApiDeliveryFeedbackDto().recipients(new ArrayList<>(recipients));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ public static Builder builder() {
}

public static class Builder extends BaseBatch.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() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static Builder builder() {

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

private Builder() {}

Expand All @@ -95,7 +95,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
Loading