Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: application deserialization with Meetings custom webhooks #532

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 39 additions & 79 deletions Vonage.Test.Unit/ApplicationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;

Check notice on line 1 in Vonage.Test.Unit/ApplicationTests.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ Getting better: Code Duplication

reduced similar code in: GetApplication. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.
using Newtonsoft.Json;
using Vonage.Applications;
using Vonage.Applications.Capabilities;
Expand Down Expand Up @@ -27,21 +27,21 @@
//ACT
var messagesWebhooks = new Dictionary<Webhook.Type, Webhook>();
messagesWebhooks.Add(Webhook.Type.InboundUrl,
new Webhook {Address = "https://example.com/webhooks/inbound", Method = "POST"});
new Webhook { Address = "https://example.com/webhooks/inbound", Method = "POST" });
messagesWebhooks.Add(Webhook.Type.StatusUrl,
new Webhook {Address = "https://example.com/webhooks/status", Method = "POST"});
new Webhook { Address = "https://example.com/webhooks/status", Method = "POST" });
var messagesCapability = new Applications.Capabilities.Messages(messagesWebhooks);
var rtcWebhooks = new Dictionary<Webhook.Type, Webhook>();
rtcWebhooks.Add(Webhook.Type.EventUrl,
new Webhook {Address = "https://example.com/webhooks/events", Method = "POST"});
new Webhook { Address = "https://example.com/webhooks/events", Method = "POST" });
var rtcCapability = new Rtc(rtcWebhooks);
var voiceWebhooks = new Dictionary<Webhook.Type, Webhook>();
voiceWebhooks.Add(Webhook.Type.AnswerUrl,
new Webhook {Address = "https://example.com/webhooks/answer", Method = "GET"});
new Webhook { Address = "https://example.com/webhooks/answer", Method = "GET" });
voiceWebhooks.Add(Webhook.Type.EventUrl,
new Webhook {Address = "https://example.com/webhooks/events", Method = "POST"});
new Webhook { Address = "https://example.com/webhooks/events", Method = "POST" });
voiceWebhooks.Add(Webhook.Type.FallbackAnswerUrl,
new Webhook {Address = "https://fallback.example.com/webhooks/answer", Method = "GET"});
new Webhook { Address = "https://fallback.example.com/webhooks/answer", Method = "GET" });
var voiceCapability = new Applications.Capabilities.Voice(voiceWebhooks);
JsonConvert.SerializeObject(voiceCapability, VonageSerialization.SerializerSettings);
var vbcCapability = new Vbc();
Expand Down Expand Up @@ -154,32 +154,32 @@
//ACT
var messagesWebhooks = new Dictionary<Webhook.Type, Webhook>();
messagesWebhooks.Add(Webhook.Type.InboundUrl,
new Webhook {Address = "https://example.com/webhooks/inbound", Method = "POST"});
new Webhook { Address = "https://example.com/webhooks/inbound", Method = "POST" });
messagesWebhooks.Add(Webhook.Type.StatusUrl,
new Webhook {Address = "https://example.com/webhooks/status", Method = "POST"});
new Webhook { Address = "https://example.com/webhooks/status", Method = "POST" });
var messagesCapability = new Applications.Capabilities.Messages(messagesWebhooks);
var rtcWebhooks = new Dictionary<Webhook.Type, Webhook>();
rtcWebhooks.Add(Webhook.Type.EventUrl,
new Webhook {Address = "https://example.com/webhooks/events", Method = "POST"});
new Webhook { Address = "https://example.com/webhooks/events", Method = "POST" });
var rtcCapability = new Rtc(rtcWebhooks);
var voiceWebhooks = new Dictionary<Webhook.Type, Webhook>();
voiceWebhooks.Add(Webhook.Type.AnswerUrl,
new Webhook {Address = "https://example.com/webhooks/answer", Method = "GET"});
new Webhook { Address = "https://example.com/webhooks/answer", Method = "GET" });
voiceWebhooks.Add(Webhook.Type.EventUrl,
new Webhook {Address = "https://example.com/webhooks/events", Method = "POST"});
new Webhook { Address = "https://example.com/webhooks/events", Method = "POST" });
voiceWebhooks.Add(Webhook.Type.FallbackAnswerUrl,
new Webhook {Address = "https://fallback.example.com/webhooks/answer", Method = "GET"});
new Webhook { Address = "https://fallback.example.com/webhooks/answer", Method = "GET" });
var voiceCapability = new Applications.Capabilities.Voice(voiceWebhooks);
JsonConvert.SerializeObject(voiceCapability);
var vbcCapability = new Vbc();
var capabilities = new ApplicationCapabilities
{Messages = messagesCapability, Rtc = rtcCapability, Voice = voiceCapability, Vbc = vbcCapability};
{ Messages = messagesCapability, Rtc = rtcCapability, Voice = voiceCapability, Vbc = vbcCapability };
var keys = new Keys
{
PublicKey = PublicKey,
};
var request = new CreateApplicationRequest
{Capabilities = capabilities, Keys = keys, Name = "My Application"};
{ Capabilities = capabilities, Keys = keys, Name = "My Application" };
var creds = Credentials.FromApiKeyAndSecret(this.ApiKey, this.ApiSecret);
var client = this.BuildVonageClient(creds);
Application response;
Expand Down Expand Up @@ -268,87 +268,47 @@
public void GetApplication(bool passCreds)
{
var id = "78d335fa323d01149c3dd6f0d48968cf";
var expectedResponse = @"{
""id"": ""78d335fa323d01149c3dd6f0d48968cf"",
""name"": ""My Application"",
""capabilities"": {
""voice"": {
""webhooks"": {
""answer_url"": {
""address"": ""https://example.com/webhooks/answer"",
""http_method"": ""GET""
},
""fallback_answer_url"": {
""address"": ""https://fallback.example.com/webhooks/answer"",
""http_method"": ""GET""
},
""event_url"": {
""address"": ""https://example.com/webhooks/event"",
""http_method"": ""POST""
}
}
},
""messages"": {
""webhooks"": {
""inbound_url"": {
""address"": ""https://example.com/webhooks/inbound"",
""http_method"": ""POST""
},
""status_url"": {
""address"": ""https://example.com/webhooks/status"",
""http_method"": ""POST""
}
}
},
""rtc"": {
""webhooks"": {
""event_url"": {
""address"": ""https://example.com/webhooks/event"",
""http_method"": ""POST""
}
}
},
""vbc"": { }
},
""keys"": {
""public_key"": ""some public key"",
""private_key"": ""some private key""
}
}";
var expectedResponse = this.GetResponseJson();
var expectedUri = $"{this.ApiUrl}/v2/applications/{id}";
this.Setup(expectedUri, expectedResponse);
var creds = Credentials.FromApiKeyAndSecret(this.ApiKey, this.ApiSecret);
var client = this.BuildVonageClient(creds);
Application application;
if (passCreds)
{
application = client.ApplicationClient.GetApplication(id, creds);
}
else
{
application = client.ApplicationClient.GetApplication(id);
}

Assert.Equal("78d335fa323d01149c3dd6f0d48968cf", application.Id);
Assert.Equal("https://example.com/webhooks/answer",
application.Capabilities.Voice.Webhooks[Webhook.Type.AnswerUrl].Address);
Assert.Equal("GET", application.Capabilities.Voice.Webhooks[Webhook.Type.AnswerUrl].Method);
Assert.Equal("https://fallback.example.com/webhooks/answer",
application.Capabilities.Voice.Webhooks[Webhook.Type.FallbackAnswerUrl].Address);
Assert.Equal("GET", application.Capabilities.Voice.Webhooks[Webhook.Type.FallbackAnswerUrl].Method);
Assert.Equal("https://example.com/webhooks/event",
application.Capabilities.Voice.Webhooks[Webhook.Type.EventUrl].Address);
Assert.Equal("POST", application.Capabilities.Voice.Webhooks[Webhook.Type.EventUrl].Method);
Assert.Equal("https://example.com/webhooks/inbound",
application.Capabilities.Messages.Webhooks[Webhook.Type.InboundUrl].Address);
Assert.Equal("POST", application.Capabilities.Messages.Webhooks[Webhook.Type.InboundUrl].Method);
Assert.Equal("https://example.com/webhooks/status",
application.Capabilities.Messages.Webhooks[Webhook.Type.StatusUrl].Address);
Assert.Equal("POST", application.Capabilities.Messages.Webhooks[Webhook.Type.StatusUrl].Method);
Assert.Equal("https://example.com/webhooks/event",
application.Capabilities.Rtc.Webhooks[Webhook.Type.EventUrl].Address);
Assert.Equal("POST", application.Capabilities.Rtc.Webhooks[Webhook.Type.EventUrl].Method);
Assert.Equal("My Application", application.Name);
Assert.Equal("http://example.com", application.Capabilities.Meetings.Webhooks[Webhook.Type.RoomChanged].Address);
Assert.Equal("POST", application.Capabilities.Meetings.Webhooks[Webhook.Type.RoomChanged].Method);
Assert.Equal("http://example.com", application.Capabilities.Meetings.Webhooks[Webhook.Type.SessionChanged].Address);
Assert.Equal("POST", application.Capabilities.Meetings.Webhooks[Webhook.Type.SessionChanged].Method);
Assert.Equal("https://54eba990d025.ngrok.app/recordings", application.Capabilities.Meetings.Webhooks[Webhook.Type.RecordingChanged].Address);
Assert.Equal("POST", application.Capabilities.Meetings.Webhooks[Webhook.Type.RecordingChanged].Method);

Check notice on line 311 in Vonage.Test.Unit/ApplicationTests.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ No longer an issue: Large Method

GetApplication is no longer above the threshold for lines of code. Large functions with many lines of code are generally harder to understand and lower the code health. Avoid adding more lines to this function.
}

[Theory]
Expand Down Expand Up @@ -503,7 +463,7 @@
if (passParameters)
{
expectedUri = $"{this.ApiUrl}/v2/applications?page_size=10&page=1&";
request = new ListApplicationsRequest {Page = 1, PageSize = 10};
request = new ListApplicationsRequest { Page = 1, PageSize = 10 };
}
else
{
Expand Down Expand Up @@ -616,7 +576,7 @@
if (passParameters)
{
expectedUri = $"{this.ApiUrl}/v2/applications?page_size=10&page=1&";
request = new ListApplicationsRequest {Page = 1, PageSize = 10};
request = new ListApplicationsRequest { Page = 1, PageSize = 10 };
}
else
{
Expand Down Expand Up @@ -727,32 +687,32 @@
//ACT
var messagesWebhooks = new Dictionary<Webhook.Type, Webhook>();
messagesWebhooks.Add(Webhook.Type.InboundUrl,
new Webhook {Address = "https://example.com/webhooks/inbound", Method = "POST"});
new Webhook { Address = "https://example.com/webhooks/inbound", Method = "POST" });
messagesWebhooks.Add(Webhook.Type.StatusUrl,
new Webhook {Address = "https://example.com/webhooks/status", Method = "POST"});
new Webhook { Address = "https://example.com/webhooks/status", Method = "POST" });
var messagesCapability = new Applications.Capabilities.Messages(messagesWebhooks);
var rtcWebhooks = new Dictionary<Webhook.Type, Webhook>();
rtcWebhooks.Add(Webhook.Type.EventUrl,
new Webhook {Address = "https://example.com/webhooks/events", Method = "POST"});
new Webhook { Address = "https://example.com/webhooks/events", Method = "POST" });
var rtcCapability = new Rtc(rtcWebhooks);
var voiceWebhooks = new Dictionary<Webhook.Type, Webhook>();
voiceWebhooks.Add(Webhook.Type.AnswerUrl,
new Webhook {Address = "https://example.com/webhooks/answer", Method = "GET"});
new Webhook { Address = "https://example.com/webhooks/answer", Method = "GET" });
voiceWebhooks.Add(Webhook.Type.EventUrl,
new Webhook {Address = "https://example.com/webhooks/events", Method = "POST"});
new Webhook { Address = "https://example.com/webhooks/events", Method = "POST" });
voiceWebhooks.Add(Webhook.Type.FallbackAnswerUrl,
new Webhook {Address = "https://fallback.example.com/webhooks/answer", Method = "GET"});
new Webhook { Address = "https://fallback.example.com/webhooks/answer", Method = "GET" });
var voiceCapability = new Applications.Capabilities.Voice(voiceWebhooks);
JsonConvert.SerializeObject(voiceCapability);
var vbcCapability = new Vbc();
var capabilities = new ApplicationCapabilities
{Messages = messagesCapability, Rtc = rtcCapability, Voice = voiceCapability, Vbc = vbcCapability};
{ Messages = messagesCapability, Rtc = rtcCapability, Voice = voiceCapability, Vbc = vbcCapability };
var keys = new Keys
{
PublicKey = PublicKey,
};
var application = new CreateApplicationRequest
{Capabilities = capabilities, Keys = keys, Name = "My Application"};
{ Capabilities = capabilities, Keys = keys, Name = "My Application" };
var creds = Credentials.FromApiKeyAndSecret(this.ApiKey, this.ApiSecret);
var client = this.BuildVonageClient(creds);
Application response;
Expand Down Expand Up @@ -848,32 +808,32 @@
//ACT
var messagesWebhooks = new Dictionary<Webhook.Type, Webhook>();
messagesWebhooks.Add(Webhook.Type.InboundUrl,
new Webhook {Address = "https://example.com/webhooks/inbound", Method = "POST"});
new Webhook { Address = "https://example.com/webhooks/inbound", Method = "POST" });
messagesWebhooks.Add(Webhook.Type.StatusUrl,
new Webhook {Address = "https://example.com/webhooks/status", Method = "POST"});
new Webhook { Address = "https://example.com/webhooks/status", Method = "POST" });
var messagesCapability = new Applications.Capabilities.Messages(messagesWebhooks);
var rtcWebhooks = new Dictionary<Webhook.Type, Webhook>();
rtcWebhooks.Add(Webhook.Type.EventUrl,
new Webhook {Address = "https://example.com/webhooks/events", Method = "POST"});
new Webhook { Address = "https://example.com/webhooks/events", Method = "POST" });
var rtcCapability = new Rtc(rtcWebhooks);
var voiceWebhooks = new Dictionary<Webhook.Type, Webhook>();
voiceWebhooks.Add(Webhook.Type.AnswerUrl,
new Webhook {Address = "https://example.com/webhooks/answer", Method = "GET"});
new Webhook { Address = "https://example.com/webhooks/answer", Method = "GET" });
voiceWebhooks.Add(Webhook.Type.EventUrl,
new Webhook {Address = "https://example.com/webhooks/events", Method = "POST"});
new Webhook { Address = "https://example.com/webhooks/events", Method = "POST" });
voiceWebhooks.Add(Webhook.Type.FallbackAnswerUrl,
new Webhook {Address = "https://fallback.example.com/webhooks/answer", Method = "GET"});
new Webhook { Address = "https://fallback.example.com/webhooks/answer", Method = "GET" });
var voiceCapability = new Applications.Capabilities.Voice(voiceWebhooks);
JsonConvert.SerializeObject(voiceCapability);
var vbcCapability = new Vbc();
var capabilities = new ApplicationCapabilities
{Messages = messagesCapability, Rtc = rtcCapability, Voice = voiceCapability, Vbc = vbcCapability};
{ Messages = messagesCapability, Rtc = rtcCapability, Voice = voiceCapability, Vbc = vbcCapability };
var keys = new Keys
{
PublicKey = PublicKey,
};
var application = new CreateApplicationRequest
{Capabilities = capabilities, Keys = keys, Name = "My Application"};
{ Capabilities = capabilities, Keys = keys, Name = "My Application" };
var creds = Credentials.FromApiKeyAndSecret(this.ApiKey, this.ApiSecret);
var client = this.BuildVonageClient(creds);
Application response;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"id": "78d335fa323d01149c3dd6f0d48968cf",
"name": "My Application",
"capabilities": {
"voice": {
"webhooks": {
"answer_url": {
"address": "https://example.com/webhooks/answer",
"http_method": "GET"
},
"fallback_answer_url": {
"address": "https://fallback.example.com/webhooks/answer",
"http_method": "GET"
},
"event_url": {
"address": "https://example.com/webhooks/event",
"http_method": "POST"
}
}
},
"messages": {
"webhooks": {
"inbound_url": {
"address": "https://example.com/webhooks/inbound",
"http_method": "POST"
},
"status_url": {
"address": "https://example.com/webhooks/status",
"http_method": "POST"
}
}
},
"rtc": {
"webhooks": {
"event_url": {
"address": "https://example.com/webhooks/event",
"http_method": "POST"
}
}
},
"vbc": {},
"meetings": {
"webhooks": {
"room_changed": {
"address": "http://example.com",
"http_method": "POST"
},
"session_changed": {
"address": "http://example.com",
"http_method": "POST"
},
"recording_changed": {
"address": "https://54eba990d025.ngrok.app/recordings",
"http_method": "POST"
}
}
}
},
"keys": {
"public_key": "some public key",
"private_key": "some private key"
}
}
3 changes: 3 additions & 0 deletions Vonage.Test.Unit/Vonage.Test.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,9 @@
<None Update="Users\UpdateUser\Data\ShouldSerializeEmpty-request.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Data\ApplicationTests\GetApplication-response.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="node ../.scripts/init.js"/>
Expand Down
9 changes: 9 additions & 0 deletions Vonage/Common/Webhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ public enum Type

[EnumMember(Value = "Unknown")]
Unknown = 6,

[EnumMember(Value = "room_changed")]
RoomChanged =7,

[EnumMember(Value = "session_changed")]
SessionChanged=8,

[EnumMember(Value = "recording_changed")]
RecordingChanged = 9,
}

}
Loading