Skip to content

Commit

Permalink
fix: remove stringified booleans (#544)
Browse files Browse the repository at this point in the history
* Remove stringified booleans to match api specs

* Implement proper converter for RealTimeData, fix expected JSON to match specs

* Fix Json property ordering (broken by linter)
  • Loading branch information
Tr00d authored Oct 20, 2023
1 parent 102682f commit 972c624
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"musicOnHoldUrl": [
"https://example.com/music.mp3"
],
"startOnEnter": "false",
"startOnEnter": false,
"canSpeak": [
"6a4d6af0-55a6-4667-be90-8614e4c8e83c"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"musicOnHoldUrl": [
"https://example.com/music.mp3"
],
"startOnEnter": "true",
"endOnExit": "true",
"record": "true",
"startOnEnter": true,
"endOnExit": true,
"record": true,
"canSpeak": [
"6a4d6af0-55a6-4667-be90-8614e4c8e83c"
],
Expand Down
2 changes: 1 addition & 1 deletion Vonage.Test.Unit/Data/NccoTests/TestRecord-request.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"endOnSilence": "3",
"endOnKey": "#",
"timeOut": "60",
"beepStart": "true",
"beepStart": true,
"eventUrl": [
"https://example.com/record",
"https://test.com/record"
Expand Down
2 changes: 1 addition & 1 deletion Vonage.Test.Unit/Data/NccoTests/TestStream-request.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"https://acme.com/streams/music.mp3"
],
"level": "0",
"bargeIn": "true",
"bargeIn": true,
"loop": "2"
}
]
2 changes: 1 addition & 1 deletion Vonage.Test.Unit/Data/NccoTests/TestTalk-request.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"action": "talk",
"text": "Hello World",
"bargeIn": "true",
"bargeIn": true,
"loop": "2",
"level": "0",
"voiceName": "kimberly",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"valid_number": "valid",
"reachable": "reachable",
"real_time_data" : {
"active_status": true,
"active_status": "active",
"handset_status" : "on"
}
}
18 changes: 8 additions & 10 deletions Vonage/Conversions/ConversionRequest.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
using Newtonsoft.Json;
using Vonage.Serialization;

namespace Vonage.Conversions;

public class ConversionRequest
{
/// <summary>
/// The ID you receive in the response to a request. * From the Verify API - use the event_id in the response to Verify Check.
/// * From the SMS API - use the message-id * From the Text-To-Speech API - use the call-id * From the Text-To-Speech-Prompt API - use the call-id
/// Set to true if your user replied to the message you sent. Otherwise, set to false. Note: for curl, use 0 and 1.
/// </summary>
[JsonProperty("message-id")]
public string MessageId { get; set; }
[JsonProperty("delivered", Order = 1)]
public bool Delivered { get; set; }

/// <summary>
/// Set to true if your user replied to the message you sent. Otherwise, set to false. Note: for curl, use 0 and 1.
/// The ID you receive in the response to a request. * From the Verify API - use the event_id in the response to Verify Check.
/// * From the SMS API - use the message-id * From the Text-To-Speech API - use the call-id * From the Text-To-Speech-Prompt API - use the call-id
/// </summary>
[JsonProperty("delivered")]
[JsonConverter(typeof(StringBoolConverter))]
public bool Delivered { get; set; }
[JsonProperty("message-id", Order = 0)]
public string MessageId { get; set; }

/// <summary>
/// When the user completed your call-to-action (e.g. visited your website, installed your app)
/// in UTC±00:00 format: yyyy-MM-dd HH:mm:ss. If you do not set this parameter, Vonage uses the time it receives this request.
/// </summary>
[JsonProperty("timestamp")]
[JsonProperty("timestamp", Order = 2)]
public string TimeStamp { get; set; }
}
104 changes: 55 additions & 49 deletions Vonage/Messaging/InboundSms.cs
Original file line number Diff line number Diff line change
@@ -1,76 +1,50 @@
using Newtonsoft.Json;
using Vonage.Cryptography;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Newtonsoft.Json;
using Vonage.Cryptography;
using Vonage.Serialization;

namespace Vonage.Messaging;

public class InboundSms
{
[JsonProperty("api-key")]
public string ApiKey { get; set; }
[JsonProperty("api-key")] public string ApiKey { get; set; }

[JsonProperty("msisdn")]
public string Msisdn { get; set; }
[JsonProperty("concat")]
[JsonConverter(typeof(StringBoolConverter))]
public bool Concat { get; set; }

[JsonProperty("to")]
public string To { get; set; }
[JsonProperty("concat-part")] public string ConcatPart { get; set; }

[JsonProperty("messageId")]
public string MessageId { get; set; }
[JsonProperty("concat-ref")] public string ConcatRef { get; set; }

[JsonProperty("text")]
public string Text { get; set; }
[JsonProperty("concat-total")] public string ConcatTotal { get; set; }

[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("data")] public string Data { get; set; }

[JsonProperty("keyword")]
public string Keyword { get; set; }
[JsonProperty("keyword")] public string Keyword { get; set; }

[JsonProperty("message-timestamp")]
public string MessageTimestamp { get; set; }
[JsonProperty("messageId")] public string MessageId { get; set; }

[JsonProperty("timestamp")]
public string Timestamp { get; set; }
[JsonProperty("message-timestamp")] public string MessageTimestamp { get; set; }

[JsonProperty("nonce")]
public string Nonce { get; set; }
[JsonProperty("msisdn")] public string Msisdn { get; set; }

[JsonProperty("concat")]
[JsonConverter(typeof(StringBoolConverter))]
public bool Concat { get; set; }
[JsonProperty("nonce")] public string Nonce { get; set; }

[JsonProperty("concat-ref")]
public string ConcatRef { get; set; }
[JsonProperty("sig")] public string Sig { get; set; }

[JsonProperty("concat-total")]
public string ConcatTotal { get; set; }
[JsonProperty("concat-part")]
public string ConcatPart { get; set; }
[JsonProperty("text")] public string Text { get; set; }

[JsonProperty("data")]
public string Data { get; set; }
[JsonProperty("timestamp")] public string Timestamp { get; set; }

[JsonProperty("udh")]
public string Udh { get; set; }

[JsonProperty("sig")]
public string Sig { get; set; }
[JsonProperty("to")] public string To { get; set; }

public bool ValidateSignature(string signatureSecret, SmsSignatureGenerator.Method method)
{
//use json representation to create a useable dictionary
var json = JsonConvert.SerializeObject(this, VonageSerialization.SerializerSettings);
var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
[JsonProperty("type")] public string Type { get; set; }

var signatureString = ConstructSignatureStringFromDictionary(dict);
var testSig = SmsSignatureGenerator.GenerateSignature(signatureString, signatureSecret, method).ToString();
System.Diagnostics.Debug.WriteLine(testSig);
return testSig == this.Sig;
}
[JsonProperty("udh")] public string Udh { get; set; }

public static string ConstructSignatureStringFromDictionary(IDictionary<string, string> query)
{
Expand All @@ -82,14 +56,18 @@ public static string ConstructSignatureStringFromDictionary(IDictionary<string,
{
sorted_dict.Add(key, query[key].ToString());
}

foreach (var key in sorted_dict.Keys)
{
if (key == "sig")
{
continue;
}
sig_sb.AppendFormat("&{0}={1}", key.Replace('=', '_').Replace('&', '_'), sorted_dict[key].ToString().Replace('=', '_').Replace('&', '_'));

sig_sb.AppendFormat("&{0}={1}", key.Replace('=', '_').Replace('&', '_'),
sorted_dict[key].ToString().Replace('=', '_').Replace('&', '_'));
}

return sig_sb.ToString();
}
catch
Expand All @@ -98,4 +76,32 @@ public static string ConstructSignatureStringFromDictionary(IDictionary<string,
}
}

public bool ValidateSignature(string signatureSecret, SmsSignatureGenerator.Method method)
{
//use json representation to create a useable dictionary
var json = JsonConvert.SerializeObject(this, VonageSerialization.SerializerSettings);
var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
var signatureString = ConstructSignatureStringFromDictionary(dict);
var testSig = SmsSignatureGenerator.GenerateSignature(signatureString, signatureSecret, method).ToString();
Debug.WriteLine(testSig);
return testSig == this.Sig;
}
}

internal class StringBoolConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => objectType == typeof(bool);

public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer) =>
bool.TryParse(reader.Value?.ToString(), out var boolValue) && boolValue;

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (bool.TryParse(value?.ToString(), out var boolValue))
{
writer.WriteValue(boolValue.ToString().ToLowerInvariant());
}
else writer.WriteNull();
}
}
29 changes: 25 additions & 4 deletions Vonage/NumberInsights/RealTimeData.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
using System;
using Newtonsoft.Json;
using Vonage.Serialization;

namespace Vonage.NumberInsights;

/// <summary>
/// Real time data about the number.
/// </summary>
public class RealTimeData
{
/// <summary>
/// Whether the end-user's phone number is active within an operator's network.
/// Whether the end-user's phone number is active within an operator's network.
/// </summary>
[JsonProperty("active_status")]
[JsonConverter(typeof(StringBoolConverter))]
[JsonConverter(typeof(StatusConverter))]
public bool ActiveStatus { get; set; }

/// <summary>
/// Whether the end-user's handset is turned on or off.
/// Whether the end-user's handset is turned on or off.
/// </summary>
[JsonProperty("handset_status")]
public string HandsetStatus { get; set; }
}

internal class StatusConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => objectType == typeof(bool);

public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer) =>
(string) reader.Value == "active";

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (bool.TryParse(value?.ToString(), out var boolValue))
{
writer.WriteValue(boolValue.ToString().ToLowerInvariant());
}
else writer.WriteNull();
}
}
42 changes: 0 additions & 42 deletions Vonage/Serialization/StringBoolConverter.cs

This file was deleted.

4 changes: 0 additions & 4 deletions Vonage/Voice/Nccos/ConversationAction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Newtonsoft.Json;
using Vonage.Serialization;

namespace Vonage.Voice.Nccos;

Expand Down Expand Up @@ -38,7 +37,6 @@ public class ConversationAction : NccoAction
/// moderator hangs up.
/// </summary>
[JsonProperty("endOnExit", Order = 4)]
[JsonConverter(typeof(StringBoolConverter))]
public bool EndOnExit { get; set; }

/// <summary>
Expand Down Expand Up @@ -78,14 +76,12 @@ public class ConversationAction : NccoAction
/// See the recording guide for more details
/// </summary>
[JsonProperty("record", Order = 5)]
[JsonConverter(typeof(StringBoolConverter))]
public bool Record { get; set; }

/// <summary>
/// The default value of true ensures that the conversation starts when this caller
/// joins conversation name. Set to false for attendees in a moderated conversation.
/// </summary>
[JsonProperty("startOnEnter", DefaultValueHandling = DefaultValueHandling.Include, Order = 3)]
[JsonConverter(typeof(StringBoolConverter))]
public bool StartOnEnter { get; set; }
}
Loading

0 comments on commit 972c624

Please sign in to comment.