Skip to content

Commit

Permalink
Implemented NCCO param for creating a call
Browse files Browse the repository at this point in the history
  • Loading branch information
BiBi committed Apr 11, 2019
1 parent 835c0cf commit 4915afc
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 8 deletions.
31 changes: 31 additions & 0 deletions Nexmo.Api.Test.Integration/CallTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Nexmo.Api.Voice;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;

namespace Nexmo.Api.Test.Integration
{
Expand Down Expand Up @@ -32,6 +33,36 @@ public void should_call()
Assert.AreEqual("started", results.status);
}

[TestMethod]
public void Should_call_with_NCCO()
{
dynamic TalkNCCO = new JObject();
TalkNCCO.action = "talk";
TalkNCCO.text = "This is a text to speech call from Nexmo";

JArray nccoObject= new JArray();
nccoObject.Add(TalkNCCO);

var results = Call.Do(new Call.CallCommand
{
to = new[]
{
new Call.Endpoint
{
type = "phone",
number = Configuration.Instance.Settings["test_number"]
}
},
from = new Call.Endpoint
{
type = "phone",
number = Configuration.Instance.Settings["nexmo_number"]
},
Ncco = nccoObject
});
Assert.AreEqual("started", results.status);
}

[TestMethod]
public void should_get_calls()
{
Expand Down
12 changes: 4 additions & 8 deletions Nexmo.Api.Test.Integration/Nexmo.Api.Test.Integration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

<ItemGroup>
<ProjectReference Include="..\Nexmo.Api\Nexmo.Api.csproj" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />

<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
Expand Down Expand Up @@ -53,12 +55,6 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.0.0" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
Expand Down
61 changes: 61 additions & 0 deletions Nexmo.Api/Helpers/RequiredIfAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nexmo.Api.Helpers
{
public class RequiredIfAttribute : ValidationAttribute
{
protected RequiredAttribute _innerAttribute;
public string DependentProperty { get; set; }
public object TargetValue { get; set; }

public RequiredIfAttribute(string dependentProperty, object targetValue)
{
_innerAttribute = new RequiredAttribute();
DependentProperty = dependentProperty;
TargetValue = targetValue;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var containerType = validationContext.ObjectInstance.GetType();
var field = containerType.GetProperty(DependentProperty);

if (field != null)
{
if (field != null)
{
// get the value of the dependent property
var dependentValue = field.GetValue(validationContext.ObjectInstance, null);
// trim spaces of dependent value
if (dependentValue != null && dependentValue is string)
{
dependentValue = (dependentValue as string).Trim();

if ((dependentValue as string).Length == 0)
{
dependentValue = null;
}
}

// compare the value against the target value
if ((dependentValue == null && TargetValue == null) ||
(dependentValue != null && (TargetValue.Equals("*") || dependentValue.Equals(TargetValue))))
{
// match => means we should try validating this field
if (!_innerAttribute.IsValid(value))
// validation failed - return an error
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName), new[] { validationContext.MemberName });
}

}
}

return ValidationResult.Success;
}
}
}
12 changes: 12 additions & 0 deletions Nexmo.Api/Voice/Call.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Nexmo.Api.Helpers;
using Nexmo.Api.Request;

namespace Nexmo.Api.Voice
Expand Down Expand Up @@ -53,14 +55,24 @@ public class CallCommand
/// <summary>
/// The single or mixed collection of endpoint types you connected to. Possible values.
/// </summary>
[JsonRequired]
public Endpoint[] to { get; set; }
/// <summary>
/// The endpoint you are calling from. Possible value are the same as to.
/// </summary>
[JsonRequired]
public Endpoint from { get; set; }
/// <summary>
/// The Nexmo Call Control Object to use for this call.
/// Required unless answer url is provided.
/// </summary>
[RequiredIfAttribute("answer_url", null, ErrorMessage = "You must provide an NCCO object or an answer url")]
[JsonProperty("ncco")]
public JArray Ncco { get; set; }
/// <summary>
/// The webhook endpoint where you provide the Nexmo Call Control Object that governs this call. As soon as your user answers a call, Platform requests this NCCO from answer_url. Use answer_method to manage the HTTP method.
/// </summary>
[RequiredIfAttribute("Ncco", null, ErrorMessage = "You must provide an NCCO object or an answer url")]
public string[] answer_url { get; set; }
/// <summary>
/// Optional. The HTTP method used to send event information to answer_url. The default value is GET.
Expand Down

0 comments on commit 4915afc

Please # to comment.