Skip to content

Commit

Permalink
feat: Batches 'Cancel'
Browse files Browse the repository at this point in the history
  • Loading branch information
JPPortier committed Nov 6, 2023
1 parent 1a21cf4 commit 59cffc4
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 9 deletions.
50 changes: 44 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 @@ -18,30 +18,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 @@ -50,39 +57,70 @@ 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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,9 @@ public <T extends Batch<?>> T replace(String batchId, BaseBatch<?> batch) throws
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ public class UpdateSmsBatchMediaRequest extends UpdateBaseBatchRequest<MediaBody
private final Boolean strictValidation;
/**
* @param toAdd List of phone numbers and group IDs to add to the batch.
* @param toRemove List of phone numbers and group IDs to remove from the batch.
* @param body The message content
* @param toRemove List of phone numbers and group IDs to remove from the batch.
* @param from Sender number. Must be valid phone number, short code or alphanumeric. Required if
* Automatic Default Originator not configured. * @param body The message content
* @param deliveryReport Request delivery report callback. Note that delivery reports can be
* fetched from the API regardless of this setting
* @param sendAt If set in the future, the message will be delayed until send_at occurs. Must be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class UpdateSmsBatchTextRequest extends UpdateBaseBatchRequest<String> {
* @param toAdd List of phone numbers and group IDs to add to the batch.
* @param toRemove List of phone numbers and group IDs to remove from the batch.
* @param body The message content
* @param from Sender number. Must be valid phone number, short code or alphanumeric. Required if
* Automatic Default Originator not configured. * @param body The message content
* @param deliveryReport Request delivery report callback. Note that delivery reports can be
* fetched from the API regardless of this setting
* @param sendAt If set in the future, the message will be delayed until send_at occurs. Must be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,15 @@ void replaceText() throws ApiException {

Assertions.assertThat(response).usingRecursiveComparison().isEqualTo(batchText);
}

@Test
void cancelBatch() throws ApiException {

when(api.cancelBatchMessage(eq(configuration.getProjectId()), eq("foo text batch id")))
.thenReturn(textResponseDto);

Batch<?> response = service.cancel("foo text batch id");

Assertions.assertThat(response).usingRecursiveComparison().isEqualTo(batchText);
}
}
5 changes: 3 additions & 2 deletions sample-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ See https://developers.sinch.com for details about these parameters
| SMS | Batches | - Get | [com.sinch.sample.sms.batches.Get](src/main/java/com/sinch/sample/sms/batches/Get.java) | Require `BATCH_ID` parameter |
| | | - List | [com.sinch.sample.sms.batches.List](src/main/java/com/sinch/sample/sms/batches/List.java) | |
| | | - Send | [com.sinch.sample.sms.batches.Send](src/main/java/com/sinch/sample/sms/batches/Send.java) | |
| | | - Replace | [com.sinch.sample.sms.batches.Replace](src/main/java/com/sinch/sample/sms/batches/Replace.java) | Require `BATCH_ID` parameter |
| | | - Update | [com.sinch.sample.sms.batches.Update](src/main/java/com/sinch/sample/sms/batches/Update.java) | Require `BATCH_ID` parameter |
| | | - Replace | [com.sinch.sample.sms.batches.Replace](src/main/java/com/sinch/sample/sms/batches/Replace.java) | Require `BATCH_ID` parameter |
| | | - Update | [com.sinch.sample.sms.batches.Update](src/main/java/com/sinch/sample/sms/batches/Update.java) | Require `BATCH_ID` parameter |
| | | - DryRun | [com.sinch.sample.sms.batches.DryRun](src/main/java/com/sinch/sample/sms/batches/dryRun.java) | |
| | | - Cancel | [com.sinch.sample.sms.batches.Cancel](src/main/java/com/sinch/sample/sms/batches/Cancel.java) | Require `BATCH_ID` parameter |
29 changes: 29 additions & 0 deletions sample-app/src/main/java/com/sinch/sample/sms/batches/Cancel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.sinch.sample.sms.batches;

import com.sinch.sample.BaseApplication;
import com.sinch.sdk.domains.sms.models.BatchText;
import java.io.IOException;
import java.util.logging.Logger;

public class Cancel extends BaseApplication {
private static final Logger LOGGER = Logger.getLogger(Cancel.class.getName());

public Cancel() throws IOException {}

public static void main(String[] args) {
try {
new Cancel().run();
} catch (Exception e) {
LOGGER.severe(e.getMessage());
e.printStackTrace();
}
}

public void run() {

LOGGER.info("Cancelling batch: " + batchId);
BatchText value = client.sms().batches().cancel(batchId);

LOGGER.info("Response: " + value);
}
}

0 comments on commit 59cffc4

Please sign in to comment.