Skip to content

Commit

Permalink
Method implementation added
Browse files Browse the repository at this point in the history
  • Loading branch information
gagandeepp committed Oct 16, 2020
1 parent cc21629 commit dd7a51d
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 29 deletions.
100 changes: 77 additions & 23 deletions Vonage.Test.Unit/VoiceClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Vonage.Voice;
using System.Web;
using System.Globalization;
using Vonage.Voice.Nccos.Endpoints;

namespace Vonage.Test.Unit
{
Expand All @@ -30,8 +31,8 @@ public void CreateCall(bool passCreds)

var request = new Voice.CallCommand
{
To = new[]
{
To = new[]
{
new Voice.Nccos.Endpoints.PhoneEndpoint
{
Number="14155550100",
Expand All @@ -43,14 +44,14 @@ public void CreateCall(bool passCreds)
Number = "14155550100",
DtmfAnswer = "p*123#"
},
Ncco = new Voice.Nccos.Ncco(new Voice.Nccos.TalkAction { Text="Hello World"}),
AnswerUrl = new [] { "https://example.com/answer" },
AnswerMethod="GET",
EventUrl= new[] { "https://example.com/event" },
EventMethod="POST",
MachineDetection="continue",
LengthTimer=1,
RingingTimer=1,
Ncco = new Voice.Nccos.Ncco(new Voice.Nccos.TalkAction { Text = "Hello World" }),
AnswerUrl = new[] { "https://example.com/answer" },
AnswerMethod = "GET",
EventUrl = new[] { "https://example.com/event" },
EventMethod = "POST",
MachineDetection = "continue",
LengthTimer = 1,
RingingTimer = 1,
};
var creds = Request.Credentials.FromAppIdAndPrivateKey(AppId, PrivateKey);
var client = new VonageClient(creds);
Expand All @@ -71,7 +72,7 @@ public void CreateCall(bool passCreds)

[Theory]
[InlineData(true, true)]
[InlineData(false, false)]
[InlineData(false, false)]
public void TestListCalls(bool passCreds, bool kitchenSink)
{
var expectedResponse = @"{
Expand Down Expand Up @@ -139,12 +140,12 @@ public void TestListCalls(bool passCreds, bool kitchenSink)
expectedUri = $"{ApiUrl}/v1/calls";
filter = new CallSearchFilter();
}

Setup(expectedUri, expectedResponse);

var creds = Request.Credentials.FromAppIdAndPrivateKey(AppId, PrivateKey);
var client = new VonageClient(creds);

Common.PageResponse<CallList> callList;
if (passCreds)
{
Expand Down Expand Up @@ -229,8 +230,8 @@ public void TestGetSpecificCall(bool passCreds)
else
{
callRecord = client.VoiceClient.GetCall(uuid);
}
}

Assert.Equal("63f61863-4a51-4f6b-86e1-46edebcf9356", callRecord.Uuid);
Assert.Equal("CON-f972836a-550f-45fa-956c-12a2ab5b7d22", callRecord.ConversationUuid);
Assert.Equal("447700900000", callRecord.To.Number);
Expand All @@ -251,7 +252,7 @@ public void TestGetSpecificCall(bool passCreds)

[Theory]
[InlineData(true, true, true)]
[InlineData(false, false, true )]
[InlineData(false, false, true)]
[InlineData(false, false, false)]
public void TestUpdateCall(bool passCreds, bool inlineNcco, bool testTransfer)
{
Expand Down Expand Up @@ -334,7 +335,7 @@ public void TestStartStream(bool passCreds, bool kitchenSink)
CallCommandResponse response;
if (passCreds)
{
response = client.VoiceClient.StartStream(uuid,command, creds);
response = client.VoiceClient.StartStream(uuid, command, creds);
}
else
{
Expand All @@ -356,7 +357,7 @@ public void StopStream(bool passCreds)
""uuid"": ""63f61863-4a51-4f6b-86e1-46edebcf9356""
}";

Setup(expectedUri, expectedResponse,"{}");
Setup(expectedUri, expectedResponse, "{}");

var creds = Request.Credentials.FromAppIdAndPrivateKey(AppId, PrivateKey);
var client = new VonageClient(creds);
Expand Down Expand Up @@ -395,7 +396,7 @@ public void TestStartTalk(bool passCreds, bool kitchenSink)
Text = "Hello. How are you today?",
Loop = 0,
Level = "0.4",
VoiceName="salli"
VoiceName = "salli"
};
}
else
Expand Down Expand Up @@ -466,7 +467,7 @@ public void TestStartDtmf(bool passCreds)
""uuid"": ""63f61863-4a51-4f6b-86e1-46edebcf9356""
}";
var expectedRequestContent = @"{""digits"":""1234""}";
var command = new DtmfCommand { Digits = "1234" };
var command = new DtmfCommand { Digits = "1234" };
Setup(expectedUri, expectedResponse, expectedRequestContent);

var creds = Request.Credentials.FromAppIdAndPrivateKey(AppId, PrivateKey);
Expand All @@ -492,12 +493,12 @@ public void TestGetRecordings(bool passCreds)
{
var expectedUri = $"{ApiUrl}/v1/calls/63f61863-4a51-4f6b-86e1-46edebcf9356";
var creds = Request.Credentials.FromAppIdAndPrivateKey(AppId, PrivateKey);
var expectedResponse = new byte[] { 0,1,2,3,4,5,6,7,8,9};
var expectedResponse = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Setup(expectedUri, expectedResponse);
var client = new VonageClient(creds);

GetRecordingResponse response;
if (passCreds)
if (passCreds)
{
response = client.VoiceClient.GetRecording(expectedUri, creds);
}
Expand Down Expand Up @@ -558,5 +559,58 @@ public void CreateCallWithUnicodeCharecters()
Assert.Equal("outbound", response.Direction);
Assert.Equal("started", response.Status);
}


[Fact]
public void CreateCallWithStringParameters()
{
var expectedUri = "https://api.nexmo.com/v1/calls/";
var expectedResponse = @"{
""uuid"": ""63f61863-4a51-4f6b-86e1-46edebcf9356"",
""status"": ""started"",
""direction"": ""outbound"",
""conversation_uuid"": ""CON-f972836a-550f-45fa-956c-12a2ab5b7d22""
}";
var expectedRequesetContent = @"{""to"":[{""number"":""14155550100"",""dtmfAnswer"":""p*123#"",""type"":""phone""}],""from"":{""number"":""14155550100"",""dtmfAnswer"":""p*123#"",""type"":""phone""},""ncco"":[{""text"":""Hello World"",""action"":""talk""}],""answer_url"":[""https://example.com/answer""],""answer_method"":""GET"",""event_url"":[""https://example.com/event""],""event_method"":""POST"",""machine_detection"":""continue"",""length_timer"":1,""ringing_timer"":1}";

Setup(expectedUri, expectedResponse, expectedRequesetContent);
var creds = Request.Credentials.FromAppIdAndPrivateKey(AppId, PrivateKey);
var client = new VonageClient(creds);
CallResponse response;
response = client.VoiceClient.CreateCall("14155550100", "14155550100", new Voice.Nccos.Ncco(new Voice.Nccos.TalkAction { Text = "Hello World" }));


Assert.Equal("63f61863-4a51-4f6b-86e1-46edebcf9356", response.Uuid);
Assert.Equal("CON-f972836a-550f-45fa-956c-12a2ab5b7d22", response.ConversationUuid);
Assert.Equal("outbound", response.Direction);
Assert.Equal("started", response.Status);
}

[Fact]
public void CreateCallv2()
{
var expectedUri = "https://api.nexmo.com/v1/calls/";
var expectedResponse = @"{
""uuid"": ""63f61863-4a51-4f6b-86e1-46edebcf9356"",
""status"": ""started"",
""direction"": ""outbound"",
""conversation_uuid"": ""CON-f972836a-550f-45fa-956c-12a2ab5b7d22""
}";
var expectedRequesetContent = @"{""to"":[{""number"":""14155550100"",""dtmfAnswer"":""p*123#"",""type"":""phone""}],""from"":{""number"":""14155550100"",""dtmfAnswer"":""p*123#"",""type"":""phone""},""ncco"":[{""text"":""Hello World"",""action"":""talk""}],""answer_url"":[""https://example.com/answer""],""answer_method"":""GET"",""event_url"":[""https://example.com/event""],""event_method"":""POST"",""machine_detection"":""continue"",""length_timer"":1,""ringing_timer"":1}";

Setup(expectedUri, expectedResponse, expectedRequesetContent);
var creds = Request.Credentials.FromAppIdAndPrivateKey(AppId, PrivateKey);
var client = new VonageClient(creds);
CallResponse response;
var toEndpoint = new Nexmo.Api.Voice.Nccos.Endpoints.PhoneEndpoint() { Number = "14155550100" };
response = client.VoiceClient.CreateCall(
toEndpoint, "14155550100", new Voice.Nccos.Ncco(new Voice.Nccos.TalkAction { Text = "Hello World" }));


Assert.Equal("63f61863-4a51-4f6b-86e1-46edebcf9356", response.Uuid);
Assert.Equal("CON-f972836a-550f-45fa-956c-12a2ab5b7d22", response.ConversationUuid);
Assert.Equal("outbound", response.Direction);
Assert.Equal("started", response.Status);
}
}
}
6 changes: 4 additions & 2 deletions Vonage/Voice/IVoiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public interface IVoiceClient
/// <param name="ncco"></param>
/// <returns></returns>
/// <exception cref="VonageHttpRequestException">thrown if an error is encountered when talking to the API</exception>
CallResponse CreateCall(string toNumber, string fromNumber , Ncco ncco);
CallResponse CreateCall(string toNumber, string fromNumber, Nccos.Ncco ncco);


/// <summary>
Expand All @@ -112,6 +112,8 @@ public interface IVoiceClient
/// <param name="ncco"></param>
/// <returns></returns>
/// <exception cref="VonageHttpRequestException">thrown if an error is encountered when talking to the API</exception>
CallResponse CreateCall(Endpoint toEndPoint, string fromNumber, Ncco ncco);
CallResponse CreateCall(PhoneEndpoint toEndPoint, string fromNumber, Nccos.Ncco ncco);


}
}
53 changes: 49 additions & 4 deletions Vonage/Voice/VoiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class VoiceClient : IVoiceClient
public const string DELETE = "DELETE";
public const string PUT = "PUT";
public const string CALLS_ENDPOINT = "v1/calls";
public const string CALLS_ENDPOINT_LATEST = "v2/calls";
public Credentials Credentials { get; set; }

public VoiceClient(Credentials credentials = null)
Expand Down Expand Up @@ -203,14 +204,58 @@ public GetRecordingResponse GetRecording(string recordingUrl, Credentials creds
}
}

CallResponse IVoiceClient.CreateCall(string toNumber, string fromNumber, Ncco ncco)
CallResponse IVoiceClient.CreateCall(string toNumber, string fromNumber, Vonage.Voice.Nccos.Ncco ncco)
{
throw new NotImplementedException();
var command = new Voice.CallCommand
{
To = new[]
{
new Voice.Nccos.Endpoints.PhoneEndpoint
{
Number=toNumber
}
},
From = new Voice.Nccos.Endpoints.PhoneEndpoint
{
Number = fromNumber
},
Ncco = ncco,
};

return ApiRequest.DoRequestWithJsonContent<CallResponse>(
POST,
ApiRequest.GetBaseUri(ApiRequest.UriType.Api, CALLS_ENDPOINT_LATEST),
command,
ApiRequest.AuthType.Bearer,
Credentials
);
}

CallResponse IVoiceClient.CreateCall(Endpoint toEndPoint, string fromNumber, Ncco ncco)
CallResponse IVoiceClient.CreateCall(PhoneEndpoint toEndPoint, string fromNumber, Vonage.Voice.Nccos.Ncco ncco)
{
throw new NotImplementedException();
var command = new Voice.CallCommand
{
To = new[]
{
new Voice.Nccos.Endpoints.PhoneEndpoint
{
Number= toEndPoint.ToString()
}
},
From = new Voice.Nccos.Endpoints.PhoneEndpoint
{
Number = fromNumber
},
Ncco = ncco,
};

return ApiRequest.DoRequestWithJsonContent<CallResponse>(
POST,
ApiRequest.GetBaseUri(ApiRequest.UriType.Api, CALLS_ENDPOINT_LATEST),
command,
ApiRequest.AuthType.Bearer,
Credentials
);
}
}
}

0 comments on commit dd7a51d

Please sign in to comment.