Skip to content

Commit

Permalink
feat: implement UpdateMessage in Messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Sep 2, 2024
1 parent 0327522 commit a46c451
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"status": "revoked"
}
7 changes: 7 additions & 0 deletions Vonage.Test/Messages/Rcs/RcsMessagesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ public async Task SendRcsVideoAsyncReturnsOk()
await this.AssertResponse(request, this.helper.GetRequestJson());
}

[Fact]
public async Task UpdateAsyncReturnsOk()
{
this.Setup(this.expectedUri, "", this.helper.GetRequestJson());
await this.client.MessagesClient.UpdateAsync(RcsUpdateMessageRequest.Build("ID-123"));
}

private async Task AssertResponse(IMessage request, string expectedRequest)
{
this.Setup(this.expectedUri, this.expectedResponse, expectedRequest);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"status": "read"
}
8 changes: 8 additions & 0 deletions Vonage.Test/Messages/WhatsApp/WhatsAppMessagesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,4 +511,12 @@ public async Task SendWhatsAppVideoAsyncReturnsOkWithContext()
};
await this.AssertResponse(request, expectedRequest);
}

[Fact]
public async Task UpdateAsyncReturnsOk()
{
this.Setup(this.expectedUri, "", this.helper.GetRequestJson());
await this.BuildVonageClient(Credentials.FromAppIdAndPrivateKey(this.AppId, this.PrivateKey))
.MessagesClient.UpdateAsync(WhatsAppUpdateMessageRequest.Build("ID-123"));
}
}
9 changes: 9 additions & 0 deletions Vonage.Test/Vonage.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,15 @@
<None Update="Messages\Rcs\Data\SendRcsVideoAsyncReturnsOk-request.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Messages\Rcs\Data\UpdateAsyncReturnsOk-request.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Messages\Rcs\Data\UpdateMessageRequest-request.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Messages\WhatsApp\Data\UpdateAsyncReturnsOk-request.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="node ../.scripts/init.js"/>
Expand Down
11 changes: 10 additions & 1 deletion Vonage/Messages/IMessagesClient.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
using System.Threading.Tasks;
#region
using System.Threading.Tasks;
#endregion

namespace Vonage.Messages;

public interface IMessagesClient
{
Task<MessagesResponse> SendAsync(IMessage message);

/// <summary>
/// This endpoint lets you update the status of outbound and/or inbound messages for certain channels. For example, you
/// can revoke outbound messages or mark inbound messages as read.
/// </summary>
/// <param name="request">The request.</param>
Task UpdateAsync(IUpdateMessageRequest request);
}
17 changes: 17 additions & 0 deletions Vonage/Messages/IUpdateMessageRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Vonage.Messages;

/// <summary>
/// Represents a request to update a message.
/// </summary>
public interface IUpdateMessageRequest
{
/// <summary>
/// UUID of the message to be updated
/// </summary>
string MessageUuid { get; }

/// <summary>
/// The status to set for the message.
/// </summary>
string Status { get; }
}
17 changes: 16 additions & 1 deletion Vonage/Messages/MessagesClient.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
#region
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Vonage.Common;
using Vonage.Request;
using Vonage.Serialization;
#endregion

namespace Vonage.Messages;

Expand Down Expand Up @@ -51,4 +53,17 @@ public Task<MessagesResponse> SendAsync(IMessage message)
value => JsonSerializerBuilder.BuildWithSnakeCase().DeserializeObject<MessagesResponse>(value)
.GetSuccessUnsafe());
}

/// <inheritdoc />
public async Task UpdateAsync(IUpdateMessageRequest request)
{
var authType = this.credentials.GetPreferredAuthenticationType()
.IfFailure(failure => throw failure.ToException());
await ApiRequest.Build(this.credentials, this.configuration, this.timeProvider).DoRequestWithJsonContentAsync(
new HttpMethod("PATCH"), this.uri,
request,
authType,
value => JsonSerializerBuilder.BuildWithSnakeCase().SerializeObject(value),
_ => _);
}
}
29 changes: 29 additions & 0 deletions Vonage/Messages/Rcs/RcsUpdateMessageRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#region
using System.Text.Json.Serialization;
#endregion

namespace Vonage.Messages.Rcs;

/// <summary>
/// Update message request for RCS.
/// </summary>
public struct RcsUpdateMessageRequest : IUpdateMessageRequest
{
/// <inheritdoc />
[JsonIgnore]
public string MessageUuid { get; private init; }

/// <inheritdoc />
public string Status { get; private init; }

/// <summary>
/// Build an update message request for RCS.
/// </summary>
/// <param name="messageUuid">UUID of the message to be updated</param>
/// <returns>The request.</returns>
public static RcsUpdateMessageRequest Build(string messageUuid) => new RcsUpdateMessageRequest
{
MessageUuid = messageUuid,
Status = "revoked",
};
}
29 changes: 29 additions & 0 deletions Vonage/Messages/WhatsApp/WhatsAppUpdateMessageRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#region
using System.Text.Json.Serialization;
#endregion

namespace Vonage.Messages.WhatsApp;

/// <summary>
/// Update message request for WhatsApp.
/// </summary>
public readonly struct WhatsAppUpdateMessageRequest : IUpdateMessageRequest
{
/// <inheritdoc />
[JsonIgnore]
public string MessageUuid { get; private init; }

/// <inheritdoc />
public string Status { get; private init; }

/// <summary>
/// Build an update message request for WhatsApp.
/// </summary>
/// <param name="messageUuid">UUID of the message to be updated</param>
/// <returns>The request.</returns>
public static WhatsAppUpdateMessageRequest Build(string messageUuid) => new WhatsAppUpdateMessageRequest
{
MessageUuid = messageUuid,
Status = "read",
};
}

0 comments on commit a46c451

Please sign in to comment.