-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
CommunicationRelayClient.cs
132 lines (115 loc) · 7.29 KB
/
CommunicationRelayClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Threading;
using System.Threading.Tasks;
using Azure.Communication.Pipeline;
using Azure.Core;
using Azure.Core.Pipeline;
namespace Azure.Communication.NetworkTraversal
{
/// <summary>
/// The Azure Communication Services Networking client.
/// </summary>
public class CommunicationRelayClient
{
private readonly ClientDiagnostics _clientDiagnostics;
internal CommunicationNetworkTraversalRestClient RestClient { get; }
#region public constructors - all argument need null check
/// <summary> Initializes a new instance of <see cref="CommunicationRelayClient"/>.</summary>
/// <param name="connectionString"> Connection string acquired from the Azure Communication Services resource. </param>
public CommunicationRelayClient(string connectionString)
: this(
ConnectionString.Parse(Argument.CheckNotNullOrEmpty(connectionString, nameof(connectionString))),
new CommunicationRelayClientOptions())
{ }
/// <summary> Initializes a new instance of <see cref="CommunicationRelayClient"/>.</summary>
/// <param name="connectionString"> Connection string acquired from the Azure Communication Services resource. </param>
/// <param name="options"> Client option exposing <see cref="ClientOptions.Diagnostics"/>, <see cref="ClientOptions.Retry"/>, <see cref="ClientOptions.Transport"/>, etc.</param>
public CommunicationRelayClient(string connectionString, CommunicationRelayClientOptions options)
: this(
ConnectionString.Parse(Argument.CheckNotNullOrEmpty(connectionString, nameof(connectionString))),
options ?? new CommunicationRelayClientOptions())
{ }
/// <summary> Initializes a new instance of <see cref="CommunicationRelayClient"/>.</summary>
/// <param name="endpoint"> The URI of the Azure Communication Services resource. </param>
/// <param name="keyCredential"> The <see cref="AzureKeyCredential"/> used to authenticate requests. </param>
/// <param name="options"> Client option exposing <see cref="ClientOptions.Diagnostics"/>, <see cref="ClientOptions.Retry"/>, <see cref="ClientOptions.Transport"/>, etc.</param>
public CommunicationRelayClient(Uri endpoint, AzureKeyCredential keyCredential, CommunicationRelayClientOptions options = default)
: this(
Argument.CheckNotNull(endpoint, nameof(endpoint)).AbsoluteUri,
Argument.CheckNotNull(keyCredential, nameof(keyCredential)),
options ?? new CommunicationRelayClientOptions())
{ }
/// <summary> Initializes a new instance of <see cref="CommunicationRelayClient"/>.</summary>
/// <param name="endpoint"> The URI of the Azure Communication Services resource. </param>
/// <param name="tokenCredential"> The <see cref="TokenCredential"/> used to authenticate requests, such as DefaultAzureCredential. </param>
/// <param name="options">Client option exposing <see cref="ClientOptions.Diagnostics"/>, <see cref="ClientOptions.Retry"/>, <see cref="ClientOptions.Transport"/>, etc.</param>
public CommunicationRelayClient(Uri endpoint, TokenCredential tokenCredential, CommunicationRelayClientOptions options = default)
: this(
Argument.CheckNotNull(endpoint, nameof(endpoint)).AbsoluteUri,
Argument.CheckNotNull(tokenCredential, nameof(tokenCredential)),
options ?? new CommunicationRelayClientOptions())
{ }
#endregion
#region private constructors
private CommunicationRelayClient(ConnectionString connectionString, CommunicationRelayClientOptions options)
: this(connectionString.GetRequired("endpoint"), options.BuildHttpPipeline(connectionString), options)
{ }
private CommunicationRelayClient(string endpoint, AzureKeyCredential keyCredential, CommunicationRelayClientOptions options)
: this(endpoint, options.BuildHttpPipeline(keyCredential), options)
{ }
private CommunicationRelayClient(string endpoint, TokenCredential tokenCredential, CommunicationRelayClientOptions options)
: this(endpoint, options.BuildHttpPipeline(tokenCredential), options)
{ }
private CommunicationRelayClient(string endpoint, HttpPipeline httpPipeline, CommunicationRelayClientOptions options)
{
_clientDiagnostics = new ClientDiagnostics(options);
RestClient = new CommunicationNetworkTraversalRestClient(_clientDiagnostics, httpPipeline, endpoint, options.ApiVersion);
}
#endregion
/// <summary>Initializes a new instance of <see cref="CommunicationRelayClient"/> for mocking.</summary>
protected CommunicationRelayClient()
{
_clientDiagnostics = null;
RestClient = null;
}
/// <summary>Gets a Relay Configuration for a <see cref="CommunicationUserIdentifier"/>.</summary>
/// <param name="communicationUser">The <see cref="CommunicationUserIdentifier"/> for whom to issue a token.</param>
/// <param name="routeType"> The specified <see cref="RouteType"/> for the relay request </param>
/// <param name="cancellationToken">The cancellation token to use.</param>
/// <exception cref="RequestFailedException">The server returned an error.</exception>
public virtual Response<CommunicationRelayConfiguration> GetRelayConfiguration(CommunicationUserIdentifier communicationUser = null, RouteType? routeType = null, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(CommunicationRelayClient)}.{nameof(GetRelayConfiguration)}");
scope.Start();
try
{
return RestClient.IssueRelayConfiguration(communicationUser?.Id, routeType, cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>Asynchronously gets a Relay Configuration for a <see cref="CommunicationUserIdentifier"/>.</summary>
/// <param name="communicationUser">The <see cref="CommunicationUserIdentifier"/> for whom to issue a token.</param>
/// <param name="routeType"> The specified <see cref="RouteType"/> for the relay request </param>
/// <param name="cancellationToken">The cancellation token to use.</param>
public virtual async Task<Response<CommunicationRelayConfiguration>> GetRelayConfigurationAsync(CommunicationUserIdentifier communicationUser = null, RouteType? routeType = null, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(CommunicationRelayClient)}.{nameof(GetRelayConfiguration)}");
scope.Start();
try
{
return await RestClient.IssueRelayConfigurationAsync(communicationUser?.Id, routeType, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
}
}