Skip to content

Commit 1b38f43

Browse files
melneubertMelissa Neubertzihzhan-msft
authored
Communication: Add new CallingServerClient constructor to support cre… (Azure#23299)
* Communication: Add new CallingServerClient constructor to support creation with TokenCredential * Update Tests with local Record and generated apis. (Azure#23301) * Fix test cases for different CallingServerClient auth types. Update CallingServerClient snippets. Co-authored-by: Melissa Neubert <mneubert@microsoft.com> Co-authored-by: Zihan Zhang <43592314+zihzhan-msft@users.noreply.github.com>
1 parent 7aa356f commit 1b38f43

15 files changed

+1378
-557
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ var connectionString = "<connection_string>"; // Find your Communication Service
3535
CallingServerClient callingServerClient = new CallingServerClient(connectionString);
3636
```
3737

38+
Or alternatively using a valid Active Directory token.
39+
```C# Snippet:Azure_Communication_CallingServer_Tests_Samples_CreateCallingServerClientWithToken
40+
var endpoint = new Uri("https://my-resource.communication.azure.com");
41+
TokenCredential tokenCredential = new DefaultAzureCredential();
42+
var client = new CallingServerClient(endpoint, tokenCredential);
43+
```
44+
3845
## Examples
3946
### Make a call to a phone number recipient
4047
To make an outbound call, call the `CreateCallConnection` or `CreateCallConnectionAsync` function from the `CallingServerClient`.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public partial class CallingServerClient
6464
protected CallingServerClient() { }
6565
public CallingServerClient(string connectionString) { }
6666
public CallingServerClient(string connectionString, Azure.Communication.CallingServer.CallingServerClientOptions options) { }
67+
public CallingServerClient(System.Uri endpoint, Azure.Core.TokenCredential tokenCredential, Azure.Communication.CallingServer.CallingServerClientOptions options = null) { }
6768
public virtual Azure.Response<Azure.Communication.CallingServer.CallConnection> CreateCallConnection(Azure.Communication.CommunicationIdentifier source, System.Collections.Generic.IEnumerable<Azure.Communication.CommunicationIdentifier> targets, Azure.Communication.CallingServer.CreateCallOptions options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
6869
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Communication.CallingServer.CallConnection>> CreateCallConnectionAsync(Azure.Communication.CommunicationIdentifier source, System.Collections.Generic.IEnumerable<Azure.Communication.CommunicationIdentifier> targets, Azure.Communication.CallingServer.CreateCallOptions options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
6970
public virtual Azure.Response<System.IO.Stream> DownloadStreaming(System.Uri sourceEndpoint, Azure.HttpRange range = default(Azure.HttpRange), System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }

sdk/communication/Azure.Communication.CallingServer/src/CallingServerClient.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ public CallingServerClient(string connectionString, CallingServerClientOptions o
4444
Argument.CheckNotNull(options, nameof(options)))
4545
{ }
4646

47+
/// <summary> Initializes a new instance of <see cref="CallingServerClient"/>.</summary>
48+
/// <param name="endpoint">The URI of the Azure Communication Services resource.</param>
49+
/// <param name="tokenCredential">The TokenCredential used to authenticate requests, such as DefaultAzureCredential.</param>
50+
/// <param name="options">Client option exposing <see cref="ClientOptions.Diagnostics"/>, <see cref="ClientOptions.Retry"/>, <see cref="ClientOptions.Transport"/>, etc.</param>
51+
public CallingServerClient(Uri endpoint, TokenCredential tokenCredential, CallingServerClientOptions options = default)
52+
: this(
53+
Argument.CheckNotNull(endpoint, nameof(endpoint)).AbsoluteUri,
54+
Argument.CheckNotNull(tokenCredential, nameof(tokenCredential)),
55+
options ?? new CallingServerClientOptions())
56+
{ }
57+
4758
#endregion
4859

4960
#region private constructors
@@ -52,6 +63,10 @@ private CallingServerClient(ConnectionString connectionString, CallingServerClie
5263
: this(connectionString.GetRequired("endpoint"), options.BuildHttpPipeline(connectionString), options)
5364
{ }
5465

66+
private CallingServerClient(string endpoint, TokenCredential tokenCredential, CallingServerClientOptions options)
67+
: this(endpoint, options.BuildHttpPipeline(tokenCredential), options)
68+
{ }
69+
5570
private CallingServerClient(string endpoint, HttpPipeline httpPipeline, CallingServerClientOptions options)
5671
{
5772
_pipeline = httpPipeline;

sdk/communication/Azure.Communication.CallingServer/tests/CallConnection/CallConnectionLiveTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async Task RunCreatePlayCancelHangupScenarioTests()
3636
if (SkipCallingServerInteractionLiveTests)
3737
Assert.Ignore("Skip callingserver interaction live tests flag is on.");
3838

39-
CallingServerClient client = CreateInstrumentedCallingServerClient();
39+
CallingServerClient client = CreateInstrumentedCallingServerClientWithConnectionString();
4040
try
4141
{
4242
// Establish a Call
@@ -71,7 +71,7 @@ public async Task RunCreateAddRemoveHangupScenarioTests()
7171
if (SkipCallingServerInteractionLiveTests)
7272
Assert.Ignore("Skip callingserver interaction live tests flag is on.");
7373

74-
CallingServerClient client = CreateInstrumentedCallingServerClient();
74+
CallingServerClient client = CreateInstrumentedCallingServerClientWithConnectionString();
7575
try
7676
{
7777
// Establish a call

sdk/communication/Azure.Communication.CallingServer/tests/Infrastructure/CallingServerLiveTestBase.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
using System.Collections.Generic;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Azure.Core;
89
using Azure.Communication.Identity;
910
using Azure.Core.TestFramework;
11+
using Azure.Identity;
1012
using NUnit.Framework;
1113

1214
namespace Azure.Communication.CallingServer.Tests
@@ -23,7 +25,7 @@ public class CallingServerLiveTestBase : RecordedTestBase<CallingServerTestEnvir
2325
protected const string RESOURCE_IDENTIFIER = "016a7064-0581-40b9-be73-6dde64d69d72";
2426

2527
// Random Gen Guid
26-
protected const string GROUP_IDENTIFIER = "3500789f-e11b-4ceb-85cb-bc8df2a01768";
28+
protected const string GROUP_IDENTIFIER = "3500769f-e11b-4ceb-85cb-bc8df2a01768";
2729

2830
protected string GetResourceId()
2931
{
@@ -102,7 +104,7 @@ protected CommunicationIdentityClient CreateInstrumentedCommunicationIdentityCli
102104
/// Creates a <see cref="CallingServerClient" />
103105
/// </summary>
104106
/// <returns>The instrumented <see cref="CallingServerClient" />.</returns>
105-
protected CallingServerClient CreateInstrumentedCallingServerClient()
107+
protected CallingServerClient CreateInstrumentedCallingServerClientWithConnectionString()
106108
{
107109
var connectionString = TestEnvironment.LiveTestStaticConnectionString;
108110
CallingServerClient callingServerClient = new CallingServerClient(connectionString, CreateServerCallingClientOptionsWithCorrelationVectorLogs());
@@ -115,6 +117,33 @@ protected CallingServerClient CreateInstrumentedCallingServerClient()
115117
return InstrumentClient(callingServerClient);
116118
}
117119

120+
/// <summary>
121+
/// Creates a <see cref="CallingServerClient" />.
122+
/// </summary>
123+
/// <returns>The instrumented <see cref="CallingServerClient"/>.</returns>
124+
protected CallingServerClient CreateInstrumentedCallingServerClientWithToken()
125+
{
126+
Uri endpoint = TestEnvironment.LiveTestStaticEndpoint;
127+
TokenCredential tokenCredential;
128+
129+
if (Mode == RecordedTestMode.Playback)
130+
{
131+
tokenCredential = new MockCredential();
132+
}
133+
else
134+
{
135+
tokenCredential = new DefaultAzureCredential();
136+
#region Snippet:Azure_Communication_CallingServer_Tests_Samples_CreateCallingServerClientWithToken
137+
//@@ var endpoint = new Uri("https://my-resource.communication.azure.com");
138+
//@@ TokenCredential tokenCredential = new DefaultAzureCredential();
139+
//@@ var client = new CallingServerClient(endpoint, tokenCredential);
140+
#endregion Snippet:Azure_Communication_CallingServer_Tests_Samples_CreateCallingServerClientWithToken
141+
}
142+
143+
CallingServerClient client = new CallingServerClient(endpoint, tokenCredential, CreateServerCallingClientOptionsWithCorrelationVectorLogs());
144+
return InstrumentClient(client);
145+
}
146+
118147
#region Api operation functions
119148
#region Snippet:Azure_Communication_ServerCalling_Tests_CreateGroupCallOperation
120149
internal async Task<IEnumerable<CallConnection>> CreateGroupCallOperation(CallingServerClient callingServerClient, string groupId, string from, string to, string callBackUri)
@@ -292,7 +321,7 @@ internal async Task<string> AddParticipantOperation(ServerCall serverCall)
292321
{
293322
Console.WriteLine("Performing add participant operation to add a participant");
294323

295-
string invitedUser = GetFixedUserId("0000000a-b200-7a0d-570c-113a0d00288d");
324+
string invitedUser = GetFixedUserId("0000000b-d8d8-aaa3-3ef0-8b3a0d002f3f");
296325

297326
var response = await serverCall.AddParticipantAsync(
298327
new CommunicationUserIdentifier(invitedUser),

sdk/communication/Azure.Communication.CallingServer/tests/ServerCall/ServerCallLiveTests.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Linq;
66
using System.Threading.Tasks;
7+
using Azure.Communication.Tests;
78
using NUnit.Framework;
89

910
namespace Azure.Communication.CallingServer.Tests
@@ -26,9 +27,17 @@ public ServerCallLiveTests(bool isAsync) : base(isAsync)
2627
}
2728

2829
[Test]
29-
public async Task RunAllRecordingFunctionsScenarioTests()
30+
[TestCase(AuthMethod.ConnectionString, TestName = "RunAllRecordingFunctionsWithConnectionString")]
31+
[TestCase(AuthMethod.TokenCredential, TestName = "RunAllRecordingFunctionsWithTokenCredential")]
32+
public async Task RunAllRecordingFunctionsScenarioTests(AuthMethod authMethod)
3033
{
31-
CallingServerClient callingServerClient = CreateInstrumentedCallingServerClient();
34+
CallingServerClient callingServerClient = authMethod switch
35+
{
36+
AuthMethod.ConnectionString => CreateInstrumentedCallingServerClientWithConnectionString(),
37+
AuthMethod.TokenCredential => CreateInstrumentedCallingServerClientWithToken(),
38+
_ => throw new ArgumentOutOfRangeException(nameof(authMethod)),
39+
};
40+
3241
var groupId = GetGroupId();
3342
try
3443
{
@@ -73,7 +82,7 @@ public async Task RunAllRecordingFunctionsScenarioTests()
7382
[Test]
7483
public async Task RunCreatePlayCancelHangupScenarioTests()
7584
{
76-
CallingServerClient callingServerClient = CreateInstrumentedCallingServerClient();
85+
CallingServerClient callingServerClient = CreateInstrumentedCallingServerClientWithConnectionString();
7786
var groupId = GetGroupId();
7887
try
7988
{
@@ -110,7 +119,7 @@ public async Task RunCreateAddRemoveHangupScenarioTests()
110119
if (SkipCallingServerInteractionLiveTests)
111120
Assert.Ignore("Skip callingserver interaction live tests flag is on.");
112121

113-
CallingServerClient callingServerClient = CreateInstrumentedCallingServerClient();
122+
CallingServerClient callingServerClient = CreateInstrumentedCallingServerClientWithConnectionString();
114123
var groupId = GetGroupId();
115124
try
116125
{

0 commit comments

Comments
 (0)