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

DEVEXP- 469: SMS V1 design for batches #177

Merged
merged 19 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
569dcbd
feat (SMS): Define SMS v1
JPPortier Dec 7, 2024
fe713d4
feat (SMS/Batches): Support 'send' operation
JPPortier Dec 7, 2024
43b1f7d
feat (SMS/Batches): Support 'list' operation
JPPortier Dec 7, 2024
4a1da86
feat (SMS/Batches): Support 'dryRun' operation
JPPortier Dec 7, 2024
304b55e
feat (SMS/Batches): Support 'get' operation
JPPortier Dec 8, 2024
9cd60f4
feat (SMS/Batches): Support 'replace' operation
JPPortier Dec 9, 2024
d2d588a
feat (SMS/Batches): Support 'cancel' operation
JPPortier Dec 9, 2024
43a5214
feat (SMS/Batches): Support 'sendDeliveryFeedback' operation
JPPortier Dec 9, 2024
aa4223e
feat (SMS/Batches): Support 'update' operation
JPPortier Dec 9, 2024
7d3cce1
refactor (SMS/Batches): 'flash_message', 'truncat_concat' and 'max_nu…
JPPortier Dec 9, 2024
56ac119
refactor (SMS/Batche): Support new 'subject' field for MediaMessage
JPPortier Dec 14, 2024
858ea5e
test (SMS/Batches): Add IT and e2e tests for V1
JPPortier Dec 14, 2024
59753f2
feat (SMS/Batches): Generated files
JPPortier Dec 16, 2024
6ad8141
feat (SMS): Add guard against null SMS URL for service plan id usage
JPPortier Dec 17, 2024
21dddce
test (SMS): Use valid phone number format for unit tests
JPPortier Dec 17, 2024
b37e901
test (SMS): Refactor test filenames
JPPortier Dec 17, 2024
b7b1d79
test (SMS): Extend SMS service init unit tests coverage
JPPortier Dec 17, 2024
79fa747
refactor (SMS/Batches): Rename 'Batch' class with 'BatchResponse'
JPPortier Dec 17, 2024
0a20f75
refactor (SMS/Batches): Generated files update
JPPortier Dec 17, 2024
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
10 changes: 10 additions & 0 deletions client/src/main/com/sinch/sdk/domains/sms/SMSService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
*/
public interface SMSService {

/**
* SMS Service V1
*
* @return V1 service instance for project
* @see <a
* href="https://developers.sinch.com/docs/sms/sdks/java/syntax-reference/">Documentation</a>
* @since
*/
com.sinch.sdk.domains.sms.api.v1.SMSService v1();

/**
* Batches Service instance
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class SMSService implements com.sinch.sdk.domains.sms.SMSService {
private final String uriUUID;
private final SmsContext context;
private final HttpClient httpClient;
private com.sinch.sdk.domains.sms.api.v1.SMSService v1;
private BatchesService batches;
private WebHooksService webHooks;
private DeliveryReportsService deliveryReports;
Expand Down Expand Up @@ -60,6 +61,10 @@ public SMSService(
this.authManagers =
Stream.of(new AbstractMap.SimpleEntry<>(SECURITY_SCHEME_KEYWORD_SMS, oAuthManager))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

this.v1 =
new com.sinch.sdk.domains.sms.api.v1.adapters.SMSService(
credentials, context, oAuthServer, httpClient);
}

public SMSService(
Expand All @@ -83,6 +88,14 @@ public SMSService(
this.authManagers =
Stream.of(new AbstractMap.SimpleEntry<>(SECURITY_SCHEME_KEYWORD_SMS, authManager))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

this.v1 =
new com.sinch.sdk.domains.sms.api.v1.adapters.SMSService(credentials, context, httpClient);
}

@Override
public com.sinch.sdk.domains.sms.api.v1.SMSService v1() {
return this.v1;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.sinch.sdk.domains.sms.api.v1;

import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.domains.sms.models.v1.batches.request.BatchRequest;
import com.sinch.sdk.domains.sms.models.v1.batches.request.ListBatchesRequest;
import com.sinch.sdk.domains.sms.models.v1.batches.request.SendDeliveryFeedbackRequest;
import com.sinch.sdk.domains.sms.models.v1.batches.request.UpdateBatchRequest;
import com.sinch.sdk.domains.sms.models.v1.batches.response.BatchResponse;
import com.sinch.sdk.domains.sms.models.v1.batches.response.DryRunResponse;
import com.sinch.sdk.domains.sms.models.v1.batches.response.ListBatchesResponse;

public interface BatchesService {

BatchResponse send(BatchRequest batch) throws ApiException;

ListBatchesResponse list(ListBatchesRequest parameters) throws ApiException;

DryRunResponse dryRun(boolean perRecipient, int numberOfRecipient, BatchRequest batch);

BatchResponse get(String batchId) throws ApiException;

BatchResponse replace(String batchId, BatchRequest batch) throws ApiException;

BatchResponse cancel(String batchId) throws ApiException;

void sendDeliveryFeedback(String batchId, SendDeliveryFeedbackRequest recipients)
throws ApiException;

BatchResponse update(String batchId, UpdateBatchRequest request) throws ApiException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.sinch.sdk.domains.sms.api.v1;

public interface SMSService {

BatchesService batches();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.sinch.sdk.domains.sms.api.v1.adapters;

import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.core.http.AuthManager;
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.domains.sms.api.v1.internal.BatchesApi;
import com.sinch.sdk.domains.sms.models.v1.batches.internal.SMSCursorPageNavigator;
import com.sinch.sdk.domains.sms.models.v1.batches.request.BatchRequest;
import com.sinch.sdk.domains.sms.models.v1.batches.request.ListBatchesRequest;
import com.sinch.sdk.domains.sms.models.v1.batches.request.SendDeliveryFeedbackRequest;
import com.sinch.sdk.domains.sms.models.v1.batches.request.UpdateBatchRequest;
import com.sinch.sdk.domains.sms.models.v1.batches.response.BatchResponse;
import com.sinch.sdk.domains.sms.models.v1.batches.response.DryRunResponse;
import com.sinch.sdk.domains.sms.models.v1.batches.response.ListBatchesResponse;
import com.sinch.sdk.domains.sms.models.v1.batches.response.internal.ApiBatchList;
import com.sinch.sdk.models.SmsContext;
import java.time.Instant;
import java.util.Map;

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

private final BatchesApi api;

public BatchesService(
String uriUUID,
SmsContext context,
HttpClient httpClient,
Map<String, AuthManager> authManagers) {
this.api =
new BatchesApi(httpClient, context.getSmsServer(), authManagers, new HttpMapper(), uriUUID);
}

protected BatchesApi getApi() {
return this.api;
}

public BatchResponse send(BatchRequest batch) throws ApiException {
return getApi().send(batch);
}

public ListBatchesResponse list(ListBatchesRequest parameters) throws ApiException {

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

ApiBatchList response =
getApi()
.list(
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));

SMSCursorPageNavigator navigator =
new SMSCursorPageNavigator(response.getPage(), response.getPageSize());

return new ListBatchesResponse(
this, new Page<>(guardParameters, response.getBatches(), navigator));
}

public DryRunResponse dryRun(boolean perRecipient, int numberOfRecipient, BatchRequest batch) {
return getApi().dryRun(perRecipient, numberOfRecipient, batch);
}

public BatchResponse get(String batchId) throws ApiException {
return getApi().get(batchId);
}

public BatchResponse replace(String batchId, BatchRequest batch) throws ApiException {
return getApi().replace(batchId, batch);
}

public BatchResponse cancel(String batchId) throws ApiException {
return getApi().cancel(batchId);
}

public void sendDeliveryFeedback(String batchId, SendDeliveryFeedbackRequest request)
throws ApiException {
getApi().sendDeliveryFeedback(batchId, request);
}

public BatchResponse update(String batchId, UpdateBatchRequest request) throws ApiException {
return getApi().update(batchId, request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.sinch.sdk.domains.sms.api.v1.adapters;

import com.sinch.sdk.auth.adapters.BearerAuthManager;
import com.sinch.sdk.auth.adapters.OAuthManager;
import com.sinch.sdk.core.http.AuthManager;
import com.sinch.sdk.core.http.HttpClient;
import com.sinch.sdk.core.http.HttpMapper;
import com.sinch.sdk.core.models.ServerConfiguration;
import com.sinch.sdk.core.utils.StringUtil;
import com.sinch.sdk.models.SmsContext;
import com.sinch.sdk.models.SmsServicePlanCredentials;
import com.sinch.sdk.models.UnifiedCredentials;
import java.util.AbstractMap;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SMSService implements com.sinch.sdk.domains.sms.api.v1.SMSService {

private static final Logger LOGGER = Logger.getLogger(SMSService.class.getName());

private static final String SECURITY_SCHEME_KEYWORD_SMS = "BearerAuth";
private final String uriUUID;
private final SmsContext context;
private final HttpClient httpClient;
private BatchesService batches;
private final Map<String, AuthManager> authManagers;

public SMSService(
UnifiedCredentials credentials,
SmsContext context,
ServerConfiguration oAuthServer,
HttpClient httpClient) {

Objects.requireNonNull(credentials, "Credentials must be defined");
Objects.requireNonNull(context, "Context must be defined");
StringUtil.requireNonEmpty(credentials.getKeyId(), "'keyId' must be defined");
StringUtil.requireNonEmpty(credentials.getKeySecret(), "'keySecret' must be defined");
StringUtil.requireNonEmpty(credentials.getProjectId(), "'projectId' must be defined");
StringUtil.requireNonEmpty(context.getSmsUrl(), "'smsUrl' must be defined");

LOGGER.fine("Activate SMS API with server='" + context.getSmsServer().getUrl() + "'");

OAuthManager oAuthManager =
new OAuthManager(credentials, oAuthServer, new HttpMapper(), httpClient);

this.uriUUID = credentials.getProjectId();
this.context = context;
this.httpClient = httpClient;
this.authManagers =
Stream.of(new AbstractMap.SimpleEntry<>(SECURITY_SCHEME_KEYWORD_SMS, oAuthManager))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

public SMSService(
SmsServicePlanCredentials credentials, SmsContext context, HttpClient httpClient) {

Objects.requireNonNull(credentials, "Credentials must be defined");
Objects.requireNonNull(context, "Context must be defined");
StringUtil.requireNonEmpty(credentials.getServicePlanId(), "'servicePlanId' must be defined");
StringUtil.requireNonEmpty(credentials.getApiToken(), "'apiToken' must be defined");
asein-sinch marked this conversation as resolved.
Show resolved Hide resolved
StringUtil.requireNonEmpty(context.getSmsUrl(), "'smsUrl' must be defined");

LOGGER.fine(
"Activate SMS API with service plan ID support and server='"
+ context.getSmsServer().getUrl()
+ "'");

BearerAuthManager authManager = new BearerAuthManager(credentials.getApiToken());

this.uriUUID = credentials.getServicePlanId();
this.context = context;
this.httpClient = httpClient;
this.authManagers =
Stream.of(new AbstractMap.SimpleEntry<>(SECURITY_SCHEME_KEYWORD_SMS, authManager))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

@Override
public BatchesService batches() {
if (null == this.batches) {
this.batches = new BatchesService(uriUUID, context, httpClient, authManagers);
}
return this.batches;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private Builder() {
/**
* @param flashMessage If sent as a flash message, displays true.
* @return current builder
* @deprecated
*/
public Builder setFlashMessage(boolean flashMessage) {
this.flashMessage = OptionalValue.of(flashMessage);
Expand All @@ -121,6 +122,7 @@ public Builder setFlashMessage(boolean flashMessage) {
/**
* @param truncateConcat If set to true, the message was shortened when exceeding one part.
* @return current builder
* @deprecated
*/
public Builder setTruncateConcat(boolean truncateConcat) {
this.truncateConcat = OptionalValue.of(truncateConcat);
Expand All @@ -130,6 +132,7 @@ public Builder setTruncateConcat(boolean truncateConcat) {
/**
* @param maxNumberOfMessageParts Displays the number of message parts set in the request.
* @return current builder
* @deprecated
*/
public Builder setMaxNumberOfMessageParts(int maxNumberOfMessageParts) {
this.maxNumberOfMessageParts = OptionalValue.of(maxNumberOfMessageParts);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.sinch.sdk.domains.sms.models.v1.batches.internal;

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() {
asein-sinch marked this conversation as resolved.
Show resolved Hide resolved
return computeNextPageCursor();
}

@Override
public String toString() {
return "SMSCursorPageNavigator{"
+ "currentPage="
+ currentPage
+ ", pageSize="
+ pageSize
+ "} "
+ super.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.sinch.sdk.domains.sms.models.v1.batches.request;

public interface BatchRequest {}
Loading
Loading