Skip to content

Commit

Permalink
Merge pull request #177 from sinch/DEVEXP-469-SMS-V1-design
Browse files Browse the repository at this point in the history
DEVEXP- 469: SMS V1 design for batches
  • Loading branch information
JPPortier authored Dec 17, 2024
2 parents 4c43b01 + 0a20f75 commit 21b3f53
Show file tree
Hide file tree
Showing 95 changed files with 12,617 additions and 50 deletions.
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
13 changes: 13 additions & 0 deletions client/src/main/com/sinch/sdk/domains/sms/adapters/SMSService.java
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");
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() {
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

0 comments on commit 21b3f53

Please sign in to comment.