Skip to content

Commit

Permalink
feature (Conversation/InjectMessage): Support 'injectMessage' API
Browse files Browse the repository at this point in the history
  • Loading branch information
JPPortier committed Aug 14, 2024
1 parent 7292916 commit f1355af
Show file tree
Hide file tree
Showing 12 changed files with 515 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.ConversationsListRecentRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.ConversationsListRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.CreateConversationRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.InjectMessageRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.response.ConversationsListRecentResponse;
import com.sinch.sdk.domains.conversation.models.v1.conversation.response.ConversationsListResponse;
import com.sinch.sdk.domains.conversation.models.v1.request.MetadataUpdateStrategy;
Expand Down Expand Up @@ -87,7 +88,17 @@ public interface ConversationsService {
*
* @param request Conversation instance with fields to be updated
* @return Update conversation
* @since _NEXT_VERSION_
*/
Conversation update(
String conversationId, MetadataUpdateStrategy updateStrategy, Conversation request);

/**
* This operation injects a conversation message in to a specific conversation.
*
* @param conversationId The ID of the conversation.
* @param request Request parameters to inject message
* @since _NEXT_VERSION_
*/
void injectMessage(String conversationId, InjectMessageRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.ConversationsListRecentRequest.OrderEnum;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.ConversationsListRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.CreateConversationRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.InjectMessageRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.response.ConversationRecentMessage;
import com.sinch.sdk.domains.conversation.models.v1.conversation.response.ConversationsListRecentResponse;
import com.sinch.sdk.domains.conversation.models.v1.conversation.response.ConversationsListResponse;
Expand Down Expand Up @@ -102,6 +103,10 @@ public Conversation update(
.conversationUpdateConversation(uriUUID, conversationId, request, null, updateStrategy);
}

public void injectMessage(String conversationId, InjectMessageRequest request) {
getApi().conversationInjectMessage(uriUUID, conversationId, request);
}

private ConversationsListResponse mapForPaging(
ConversationsListRequest parameters, ListConversationsResponseInternal _dto) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.sinch.sdk.domains.conversation.models.v1.conversation.request;

import com.sinch.sdk.domains.conversation.models.v1.messages.ConversationMessageBody;

/** Message to be injected */
public interface InjectMessageRequest extends InjectMessageRequestBase {

/**
* Get message body
*
* @return Body message
*/
ConversationMessageBody getBody();

/**
* Getting builder
*
* @return New Builder instance
*/
@SuppressWarnings("rawtypes")
static Builder<?> builder() {
return new InjectMessageRequestImpl.Builder();
}

/** Dedicated Builder */
interface Builder<B extends Builder<B>> extends InjectMessageRequestBase.Builder<B> {

/**
* see getter
*
* @param body see getter
* @return Current builder
* @see #getBody()
*/
B setBody(ConversationMessageBody body);

/**
* Create instance
*
* @return The instance build with current builder values
*/
InjectMessageRequest build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.sinch.sdk.domains.conversation.models.v1.conversation.request;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sinch.sdk.core.models.OptionalValue;
import com.sinch.sdk.domains.conversation.models.v1.ChannelIdentity;
import com.sinch.sdk.domains.conversation.models.v1.ConversationDirection;
import com.sinch.sdk.domains.conversation.models.v1.ProcessingMode;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.InjectMessageRequestBaseImpl.Builder;
import com.sinch.sdk.domains.conversation.models.v1.messages.AppMessage;
import com.sinch.sdk.domains.conversation.models.v1.messages.ContactMessage;
import com.sinch.sdk.domains.conversation.models.v1.messages.ConversationMessageBody;
import java.time.Instant;

public class InjectMessageRequestImpl extends InjectMessageRequestBaseImpl
implements InjectMessageRequest {

@JsonIgnore
public ConversationMessageBody getBody() {
return appMessage()
.map(f -> (ConversationMessageBody) f)
.orElseGet(() -> contactMessage().orElse(null));
}

protected InjectMessageRequestImpl(
OptionalValue<AppMessage<?>> appMessage,
OptionalValue<ContactMessage<?>> contactMessage,
OptionalValue<Instant> acceptTime,
OptionalValue<ChannelIdentity> channelIdentity,
OptionalValue<String> contactId,
OptionalValue<ConversationDirection> direction,
OptionalValue<String> conversationId,
OptionalValue<Boolean> injected,
OptionalValue<String> senderId,
OptionalValue<ProcessingMode> processingMode,
OptionalValue<String> metadata) {
super(
appMessage,
contactMessage,
acceptTime,
channelIdentity,
contactId,
direction,
conversationId,
injected,
senderId,
processingMode,
metadata);
}

static class Builder<B extends Builder<B>> extends InjectMessageRequestBaseImpl.Builder<B>
implements InjectMessageRequest.Builder<B> {

public B setBody(ConversationMessageBody body) {
if (body instanceof AppMessage) {
this.appMessage = OptionalValue.of((AppMessage<?>) body);
// TODO: CACORE-2213 workaround
this.contactMessage = OptionalValue.of(null);
} else if (body instanceof ContactMessage) {
// TODO: CACORE-2213 workaround
this.appMessage = OptionalValue.of(null);
this.contactMessage = OptionalValue.of((ContactMessage<?>) body);
} else {
throw new IllegalStateException("Unexpected value: " + body);
}
return self();
}

@Override
@SuppressWarnings("unchecked")
protected B self() {
return (B) this;
}

public InjectMessageRequest build() {
return new InjectMessageRequestImpl(
appMessage,
contactMessage,
acceptTime,
channelIdentity,
contactId,
direction,
conversationId,
injected,
senderId,
processingMode,
metadata);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.ConversationsListRecentRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.ConversationsListRecentRequest.OrderEnum;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.ConversationsListRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.InjectMessageRequestBase;
import com.sinch.sdk.domains.conversation.models.v1.conversation.response.ConversationRecentMessage;
import com.sinch.sdk.domains.conversation.models.v1.conversation.response.ConversationsListRecentResponse;
import com.sinch.sdk.domains.conversation.models.v1.conversation.response.ConversationsListResponse;
import com.sinch.sdk.domains.conversation.models.v1.conversations.ConversationDtoTest;
import com.sinch.sdk.domains.conversation.models.v1.conversations.request.CreateConversationRequestTest;
import com.sinch.sdk.domains.conversation.models.v1.conversations.response.ConversationRecentMessageDtoTest;
import com.sinch.sdk.domains.conversation.models.v1.messages.request.InjectMessageDtoTest;
import com.sinch.sdk.domains.conversation.models.v1.request.MetadataUpdateStrategy;
import com.sinch.sdk.models.ConversationContext;
import java.util.Collection;
Expand All @@ -46,6 +48,7 @@ public class ConversationsServiceTest extends ConversationBaseTest {
@Mock Map<String, AuthManager> authManagers;
@Captor ArgumentCaptor<String> projectIdCaptor;
@Captor ArgumentCaptor<String> conversationIdCaptor;
@Captor ArgumentCaptor<InjectMessageRequestBase> injectMessageCaptor;

ConversationsService service;
String uriPartID = "foovalue";
Expand Down Expand Up @@ -243,4 +246,24 @@ void update() throws ApiException {

TestHelpers.recursiveEquals(response, ConversationDtoTest.conversation);
}

@Test
void injectMessage() throws ApiException {

service.injectMessage(
InjectMessageDtoTest.injectContactMessage.getConversationId(),
InjectMessageDtoTest.injectContactMessage);

verify(api)
.conversationInjectMessage(
projectIdCaptor.capture(),
conversationIdCaptor.capture(),
injectMessageCaptor.capture());

Assertions.assertThat(projectIdCaptor.getValue()).isEqualTo(uriPartID);
Assertions.assertThat(conversationIdCaptor.getValue())
.isEqualTo(InjectMessageDtoTest.injectContactMessage.getConversationId());
TestHelpers.recursiveEquals(
injectMessageCaptor.getValue(), InjectMessageDtoTest.injectContactMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.sinch.sdk.domains.conversation.models.v1.conversation.internal.ListRecentConversationsResponseInternal;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.CreateConversationRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.InjectConversationEventRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.InjectMessageRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.InjectMessageRequestBase;
import com.sinch.sdk.domains.conversation.models.v1.conversation.response.InjectEventResponse;
import com.sinch.sdk.domains.conversation.models.v1.request.MetadataUpdateStrategy;
import java.util.ArrayList;
Expand Down Expand Up @@ -329,11 +329,11 @@ private HttpRequest conversationGetConversationRequestBuilder(
* @param projectId The unique ID of the project. You can find this on the [Sinch
* Dashboard](https://dashboard.sinch.com/convapi/apps). (required)
* @param conversationId The ID of the conversation. (required)
* @param injectMessageRequest Message to be injected. (required)
* @param injectMessageRequestBase Message to be injected. (required)
* @throws ApiException if fails to make API call
*/
public void conversationInjectMessage(
String projectId, String conversationId, InjectMessageRequest injectMessageRequest)
String projectId, String conversationId, InjectMessageRequestBase injectMessageRequestBase)
throws ApiException {

LOGGER.finest(
Expand All @@ -345,11 +345,12 @@ public void conversationInjectMessage(
+ "conversationId: "
+ conversationId
+ ", "
+ "injectMessageRequest: "
+ injectMessageRequest);
+ "injectMessageRequestBase: "
+ injectMessageRequestBase);

HttpRequest httpRequest =
conversationInjectMessageRequestBuilder(projectId, conversationId, injectMessageRequest);
conversationInjectMessageRequestBuilder(
projectId, conversationId, injectMessageRequestBase);
HttpResponse response =
httpClient.invokeAPI(
this.serverConfiguration, this.authManagersByOasSecuritySchemes, httpRequest);
Expand All @@ -367,7 +368,7 @@ public void conversationInjectMessage(
}

private HttpRequest conversationInjectMessageRequestBuilder(
String projectId, String conversationId, InjectMessageRequest injectMessageRequest)
String projectId, String conversationId, InjectMessageRequestBase injectMessageRequestBase)
throws ApiException {
// verify the required parameter 'projectId' is set
if (projectId == null) {
Expand All @@ -380,11 +381,11 @@ private HttpRequest conversationInjectMessageRequestBuilder(
400,
"Missing the required parameter 'conversationId' when calling conversationInjectMessage");
}
// verify the required parameter 'injectMessageRequest' is set
if (injectMessageRequest == null) {
// verify the required parameter 'injectMessageRequestBase' is set
if (injectMessageRequestBase == null) {
throw new ApiException(
400,
"Missing the required parameter 'injectMessageRequest' when calling"
"Missing the required parameter 'injectMessageRequestBase' when calling"
+ " conversationInjectMessage");
}

Expand All @@ -405,7 +406,7 @@ private HttpRequest conversationInjectMessageRequestBuilder(
final Collection<String> localVarContentTypes = Arrays.asList("application/json");

final Collection<String> localVarAuthNames = Arrays.asList("Basic", "oAuth2");
final String serializedBody = mapper.serialize(localVarContentTypes, injectMessageRequest);
final String serializedBody = mapper.serialize(localVarContentTypes, injectMessageRequestBase);

return new HttpRequest(
localVarPath,
Expand Down
Loading

0 comments on commit f1355af

Please sign in to comment.