Skip to content

Commit

Permalink
refactor: [breaking] remove sync methods from SmsClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Mar 14, 2024
1 parent 7b5732a commit 52f4822
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 103 deletions.
72 changes: 10 additions & 62 deletions Vonage.Test/MessagingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ namespace Vonage.Test
public class MessagingTests : TestBase
{
[Fact]
public void NullMessagesResponse()
public async Task NullMessagesResponse()
{
var expectedResponse = @"";
var expectedUri = $"{this.RestUrl}/sms/json";
var expectedRequestContent =
$"from=AcmeInc&to=447700900000&text={WebUtility.UrlEncode("Hello World!")}&api_key={this.ApiKey}&api_secret={this.ApiSecret}&";
this.Setup(expectedUri, expectedResponse, expectedRequestContent);
var client = this.BuildVonageClient(Credentials.FromApiKeyAndSecret(this.ApiKey, this.ApiSecret));
var exception = Assert.Throws<VonageSmsResponseException>(() =>
client.SmsClient.SendAnSms(new SendSmsRequest
var exception = await Assert.ThrowsAsync<VonageSmsResponseException>(() =>
client.SmsClient.SendAnSmsAsync(new SendSmsRequest
{From = "AcmeInc", To = "447700900000", Text = "Hello World!"}));
Assert.NotNull(exception);
Assert.Equal("Encountered an Empty SMS response", exception.Message);
Expand Down Expand Up @@ -93,26 +93,7 @@ public async Task SendSmsAsyncWithAllPropertiesSet(bool passCreds)
}

[Fact]
public void SendSmsBadResponse()
{
var expectedResponse = this.GetResponseJson();
var expectedUri = $"{this.RestUrl}/sms/json";
var expectedRequestContent =
$"from=AcmeInc&to=447700900000&text={WebUtility.UrlEncode("Hello World!")}&api_key={this.ApiKey}&api_secret={this.ApiSecret}&";
this.Setup(expectedUri, expectedResponse, expectedRequestContent);
var client = this.BuildVonageClient(Credentials.FromApiKeyAndSecret(this.ApiKey, this.ApiSecret));
var exception = Assert.Throws<VonageSmsResponseException>(() =>
client.SmsClient.SendAnSms(new SendSmsRequest
{From = "AcmeInc", To = "447700900000", Text = "Hello World!"}));
Assert.NotNull(exception);
Assert.Equal(
$"SMS Request Failed with status: {exception.Response.Messages[0].Status} and error message: {exception.Response.Messages[0].ErrorText}",
exception.Message);
Assert.Equal(SmsStatusCode.InvalidCredentials, exception.Response.Messages[0].StatusCode);
}

[Fact]
public void SendSmsTypicalUsage()
public async Task SendSmsTypicalUsage()
{
var expectedResponse = @"{
""message-count"": ""1"",
Expand All @@ -133,7 +114,7 @@ public void SendSmsTypicalUsage()
$"from=AcmeInc&to=447700900000&text={WebUtility.UrlEncode("Hello World!")}&api_key={this.ApiKey}&api_secret={this.ApiSecret}&";
this.Setup(expectedUri, expectedResponse, expectedRequestContent);
var client = this.BuildVonageClient(Credentials.FromApiKeyAndSecret(this.ApiKey, this.ApiSecret));
var response = client.SmsClient.SendAnSms(new SendSmsRequest
var response = await client.SmsClient.SendAnSmsAsync(new SendSmsRequest
{From = "AcmeInc", To = "447700900000", Text = "Hello World!"});
Assert.Equal("1", response.MessageCount);
Assert.Equal("447700900000", response.Messages[0].To);
Expand All @@ -145,39 +126,6 @@ public void SendSmsTypicalUsage()
Assert.Equal("customer1234", response.Messages[0].AccountRef);
}

[Fact]
public void SendSmsTypicalUsageSimplified()
{
var expectedResponse = @"{
""message-count"": ""1"",
""messages"": [
{
""to"": ""447700900000"",
""message-id"": ""0A0000000123ABCD1"",
""status"": ""0"",
""remaining-balance"": ""3.14159265"",
""message-price"": ""0.03330000"",
""network"": ""12345"",
""account-ref"": ""customer1234""
}
]
}";
var expectedUri = $"{this.RestUrl}/sms/json";
var expectedRequestContent =
$"from=AcmeInc&to=447700900000&text={WebUtility.UrlEncode("Hello World!")}&type=text&api_key={this.ApiKey}&api_secret={this.ApiSecret}&";
this.Setup(expectedUri, expectedResponse, expectedRequestContent);
var client = this.BuildVonageClient(Credentials.FromApiKeyAndSecret(this.ApiKey, this.ApiSecret));
var response = client.SmsClient.SendAnSms("AcmeInc", "447700900000", "Hello World!");
Assert.Equal("1", response.MessageCount);
Assert.Equal("447700900000", response.Messages[0].To);
Assert.Equal("0A0000000123ABCD1", response.Messages[0].MessageId);
Assert.Equal("0", response.Messages[0].Status);
Assert.Equal(SmsStatusCode.Success, response.Messages[0].StatusCode);
Assert.Equal("3.14159265", response.Messages[0].RemainingBalance);
Assert.Equal("12345", response.Messages[0].Network);
Assert.Equal("customer1234", response.Messages[0].AccountRef);
}

[Fact]
public async Task SendSmsTypicalUsageSimplifiedAsync()
{
Expand All @@ -199,7 +147,7 @@ public async Task SendSmsTypicalUsageSimplifiedAsync()
}

[Fact]
public void SendSmsUnicode()
public async Task SendSmsUnicode()
{
var expectedResponse = @"{
""message-count"": ""1"",
Expand All @@ -220,7 +168,7 @@ public void SendSmsUnicode()
$"from=AcmeInc&to=447700900000&text={WebUtility.UrlEncode("こんにちは世界")}&api_key={this.ApiKey}&api_secret={this.ApiSecret}&";
this.Setup(expectedUri, expectedResponse, expectedRequestContent);
var client = this.BuildVonageClient(Credentials.FromApiKeyAndSecret(this.ApiKey, this.ApiSecret));
var response = client.SmsClient.SendAnSms(new SendSmsRequest
var response = await client.SmsClient.SendAnSmsAsync(new SendSmsRequest
{From = "AcmeInc", To = "447700900000", Text = "こんにちは世界"});
Assert.Equal("1", response.MessageCount);
Assert.Equal("447700900000", response.Messages[0].To);
Expand All @@ -235,7 +183,7 @@ public void SendSmsUnicode()
[Theory]
[InlineData(false)]
[InlineData(true)]
public void SendSmsWithAllPropertiesSet(bool passCreds)
public async Task SendSmsWithAllPropertiesSet(bool passCreds)
{
var expectedResponse = this.GetResponseJson();
var expectedUri = $"{this.RestUrl}/sms/json";
Expand Down Expand Up @@ -265,8 +213,8 @@ public void SendSmsWithAllPropertiesSet(bool passCreds)
this.Setup(expectedUri, expectedResponse, expectedRequestContent);
var client = this.BuildVonageClient(creds);
var response = passCreds
? client.SmsClient.SendAnSms(request, creds)
: client.SmsClient.SendAnSms(request);
? await client.SmsClient.SendAnSmsAsync(request, creds)
: await client.SmsClient.SendAnSmsAsync(request);
Assert.Equal("1", response.MessageCount);
Assert.Equal("447700900000", response.Messages[0].To);
Assert.Equal("0A0000000123ABCD1", response.Messages[0].MessageId);
Expand Down
22 changes: 2 additions & 20 deletions Vonage/Messaging/ISmsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,6 @@ public interface ISmsClient
/// <returns></returns>
Task<SendSmsResponse> SendAnSmsAsync(SendSmsRequest request, Credentials creds = null);

/// <summary>
/// Send an outbound SMS from your Vonage account
/// </summary>
/// <param name="request"></param>
/// <param name="creds"></param>
/// <returns></returns>
SendSmsResponse SendAnSms(SendSmsRequest request, Credentials creds = null);

/// <summary>
/// Send an outbound SMS from your Vonage account
/// </summary>
/// <param name="from">The name or number the message should be sent from.</param>
/// <param name="to">The number that the message should be sent to. Numbers are specified in E.164 format.</param>
/// <param name="text">The body of the message being sent.</param>
/// <param name="type">The format of the message body.</param>
/// <param name="creds"></param>
/// <returns></returns>
Task<SendSmsResponse> SendAnSmsAsync(string from, string to, string text, SmsType type = SmsType.Text, Credentials creds = null);

/// <summary>
/// Send an outbound SMS from your Vonage account
/// </summary>
Expand All @@ -41,5 +22,6 @@ public interface ISmsClient
/// <param name="type">The format of the message body.</param>
/// <param name="creds"></param>
/// <returns></returns>
SendSmsResponse SendAnSms(string from, string to, string text, SmsType type = SmsType.Text, Credentials creds = null);
Task<SendSmsResponse> SendAnSmsAsync(string from, string to, string text, SmsType type = SmsType.Text,
Credentials creds = null);
}
22 changes: 1 addition & 21 deletions Vonage/Messaging/SmsClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Vonage.Common;
using Vonage.Request;

Expand All @@ -25,25 +24,6 @@ internal SmsClient(Credentials credentials, Configuration configuration, ITimePr

public Credentials Credentials { get; set; }

/// <inheritdoc/>
[Obsolete("Favor asynchronous version instead.")]
public SendSmsResponse SendAnSms(SendSmsRequest request, Credentials creds = null)
{
var result = ApiRequest.Build(this.GetCredentials(creds), this.configuration, this.timeProvider)
.DoPostRequestUrlContentFromObject<SendSmsResponse>(
ApiRequest.GetBaseUri(ApiRequest.UriType.Rest, this.configuration, "/sms/json"),
request
);
ValidSmsResponse(result);
return result;
}

/// <inheritdoc/>
[Obsolete("Favor asynchronous version instead.")]
public SendSmsResponse SendAnSms(string from, string to, string text, SmsType type = SmsType.Text,
Credentials creds = null) =>
this.SendAnSms(new SendSmsRequest {From = from, To = to, Type = type, Text = text}, creds);

/// <summary>
/// Send a SMS message.
/// </summary>
Expand Down

0 comments on commit 52f4822

Please sign in to comment.