Skip to content

Commit 0eba2c0

Browse files
authored
overloading EventParser.Parse method and adding EventParser.ParseMany. Add code examples to README.md (#30373)
1 parent 349a906 commit 0eba2c0

File tree

3 files changed

+145
-6
lines changed

3 files changed

+145
-6
lines changed

sdk/communication/Azure.Communication.CallingServer/README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ To make an outbound call, call the `CreateCall` or `CreateCallAsync` function fr
4949
CallSource callSource = new CallSource(
5050
new CommunicationUserIdentifier("<source-identifier>"), // Your Azure Communication Resource Guid Id used to make a Call
5151
);
52-
callSource.CallerId = new PhoneNumberIdentifier("<caller-id-phonenumber>") // E.164 formatted recipient phone number
52+
callSource.CallerId = new PhoneNumberIdentifier("<caller-id-phonenumber>") // E.164 formatted phone number that's associated to your Azure Communication Resource
5353
```
5454
```C#
5555
CreateCallResult createCallResult = await callAutomationClient.CreateCallAsync(
@@ -60,6 +60,60 @@ CreateCallResult createCallResult = await callAutomationClient.CreateCallAsync(
6060
Console.WriteLine($"Call connection id: {createCallResult.CallProperties.CallConnectionId}");
6161
```
6262

63+
### Handle Mid-Connection call back events
64+
Your app will receive mid-connection call back events via the callbackEndpoint you provided. You will need to write event handler controller to receive the events and direct your app flow based on your business logic.
65+
```C#
66+
/// <summary>
67+
/// Handle call back events.
68+
/// </summary>>
69+
[HttpPost]
70+
[Route("/CallBackEvent")]
71+
public IActionResult OnMidConnectionCallBackEvent([FromBody] CloudEvent[] events)
72+
{
73+
try
74+
{
75+
if (events != null)
76+
{
77+
// Helper function to parse CloudEvent to a CallingServer event.
78+
CallAutomationEventBase callBackEvent = EventParser.Parse(events.FirstOrDefault());
79+
80+
switch (callBackEvent)
81+
{
82+
case CallConnected ev:
83+
# logic to handle a CallConnected event
84+
break;
85+
case CallDisconnected ev:
86+
# logic to handle a CallDisConnected event
87+
break;
88+
case ParticipantsUpdated ev:
89+
# cast the event into a ParticipantUpdated event and do something with it. Eg. iterate through the participants
90+
ParticipantsUpdated updatedEvent = (ParticipantsUpdated)ev;
91+
break;
92+
case AddParticipantsSucceeded ev:
93+
# logic to handle an AddParticipantsSucceeded event
94+
break;
95+
case AddParticipantsFailed ev:
96+
# logic to handle an AddParticipantsFailed event
97+
break;
98+
case CallTransferAccepted ev:
99+
# logic to handle CallTransferAccepted event
100+
break;
101+
case CallTransferFailed ev:
102+
# logic to handle CallTransferFailed event
103+
break;
104+
default:
105+
break;
106+
}
107+
}
108+
}
109+
catch (Exception ex)
110+
{
111+
// handle exception
112+
}
113+
return Ok();
114+
}
115+
```
116+
63117
## Troubleshooting
64118
A `RequestFailedException` is thrown as a service response for any unsuccessful requests. The exception contains information about what response code was returned from the service.
65119

sdk/communication/Azure.Communication.CallingServer/api/Azure.Communication.CallingServer.netstandard2.0.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,12 @@ internal CreateCallResult() { }
263263
}
264264
public static partial class EventParser
265265
{
266+
public static Azure.Communication.CallingServer.CallAutomationEventBase Parse(Azure.Messaging.CloudEvent cloudEvent) { throw null; }
267+
public static Azure.Communication.CallingServer.CallAutomationEventBase Parse(System.BinaryData content) { throw null; }
266268
public static Azure.Communication.CallingServer.CallAutomationEventBase Parse(string content) { throw null; }
269+
public static Azure.Communication.CallingServer.CallAutomationEventBase[] ParseMany(Azure.Messaging.CloudEvent[] cloudEvents) { throw null; }
270+
public static Azure.Communication.CallingServer.CallAutomationEventBase[] ParseMany(System.BinaryData content) { throw null; }
271+
public static Azure.Communication.CallingServer.CallAutomationEventBase[] ParseMany(string content) { throw null; }
267272
}
268273
public partial class FileSource : Azure.Communication.CallingServer.PlaySource
269274
{

sdk/communication/Azure.Communication.CallingServer/src/Models/Events/EventParser.cs

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,97 @@ public static class EventParser
1414
private const string EventPrefix = "Microsoft.Communication.";
1515

1616
/// <summary>
17-
/// Parsing an event from json.
17+
/// Parsing an event from a string.
1818
/// </summary>
19-
/// <param name="content"></param>
20-
/// <returns></returns>
19+
/// <param name="content"> event json in string format.</param>
20+
/// <returns>A <see cref="CallAutomationEventBase"/> object.</returns>
2121
public static CallAutomationEventBase Parse(string content)
2222
{
2323
CloudEvent cloudEvent = CloudEvent.Parse(BinaryData.FromString(content));
24-
var eventType = cloudEvent.Type.Replace(EventPrefix, "");
24+
return Deserialize(cloudEvent);
25+
}
26+
27+
/// <summary>
28+
/// Parsing an event from BinaryData.
29+
/// </summary>
30+
/// <param name="content"> event json in BinaryData format.</param>
31+
/// <returns>A <see cref="CallAutomationEventBase"/> object.</returns>
32+
public static CallAutomationEventBase Parse(BinaryData content)
33+
{
34+
CloudEvent cloudEvent = CloudEvent.Parse(content);
35+
return Deserialize(cloudEvent);
36+
}
37+
38+
/// <summary>
39+
/// Parsing an event from a CloudEvent.
40+
/// </summary>
41+
/// <param name="cloudEvent"><see cref="CloudEvent"/>.</param>
42+
/// <returns>A <see cref="CallAutomationEventBase"/> object.</returns>
43+
public static CallAutomationEventBase Parse(CloudEvent cloudEvent)
44+
{
45+
return Deserialize(cloudEvent);
46+
}
47+
48+
/// <summary>
49+
/// Parsing events from a string.
50+
/// </summary>
51+
/// <param name="content"> events json in string format.</param>
52+
/// <returns>An array of <see cref="CallAutomationEventBase"/> object.</returns>
53+
public static CallAutomationEventBase[] ParseMany(string content)
54+
{
55+
CloudEvent[] cloudEvents = CloudEvent.ParseMany(BinaryData.FromString(content));
56+
var callAutomationEvents = new CallAutomationEventBase[cloudEvents.Length];
57+
for (int i = 0; i < cloudEvents.Length; i++)
58+
{
59+
var cloudEvent = cloudEvents[i];
60+
callAutomationEvents[i] = Deserialize(cloudEvent);
61+
}
62+
return callAutomationEvents;
63+
}
2564

65+
/// <summary>
66+
/// Parsing events from BinaryData.
67+
/// </summary>
68+
/// <param name="content"> events json in BinaryData format.</param>
69+
/// <returns>An array of <see cref="CallAutomationEventBase"/> object.</returns>
70+
public static CallAutomationEventBase[] ParseMany(BinaryData content)
71+
{
72+
CloudEvent[] cloudEvents = CloudEvent.ParseMany(content);
73+
var callAutomationEvents = new CallAutomationEventBase[cloudEvents.Length];
74+
for (int i = 0; i < cloudEvents.Length; i++)
75+
{
76+
var cloudEvent = cloudEvents[i];
77+
callAutomationEvents[i] = Deserialize(cloudEvent);
78+
}
79+
return callAutomationEvents;
80+
}
81+
82+
/// <summary>
83+
/// Parsing events from an array of CloudEvent.
84+
/// </summary>
85+
/// <param name="cloudEvents"><see cref="CloudEvent"/>.</param>
86+
/// <returns>An array of <see cref="CallAutomationEventBase"/> object.</returns>
87+
public static CallAutomationEventBase[] ParseMany(CloudEvent[] cloudEvents)
88+
{
89+
var callAutomationEvents = new CallAutomationEventBase[cloudEvents.Length];
90+
for (int i = 0; i < cloudEvents.Length; i++)
91+
{
92+
var cloudEvent = cloudEvents[i];
93+
callAutomationEvents[i] = Deserialize(cloudEvent);
94+
}
95+
return callAutomationEvents;
96+
}
97+
98+
/// <summary>
99+
/// Deserialize a CloudEvent to its corresponding CallingServer Event.
100+
/// </summary>
101+
/// <param name="cloudEvent"><see cref="CloudEvent"/>.</param>
102+
/// <returns>A <see cref="CallAutomationEventBase"/> object.</returns>
103+
private static CallAutomationEventBase Deserialize(CloudEvent cloudEvent)
104+
{
26105
if (cloudEvent != null && cloudEvent.Data != null)
27106
{
107+
var eventType = cloudEvent.Type.Replace(EventPrefix, "");
28108
switch (eventType)
29109
{
30110
case nameof(AddParticipantsFailed):
@@ -47,5 +127,5 @@ public static CallAutomationEventBase Parse(string content)
47127
}
48128
return null;
49129
}
50-
}
130+
}
51131
}

0 commit comments

Comments
 (0)