Skip to content

Commit

Permalink
[Communication] - Ensure RestClients are created based on ClientOptio…
Browse files Browse the repository at this point in the history
…ns.ApiVersion (Azure#18464)
  • Loading branch information
RezaJooyandeh authored and jongio committed Feb 9, 2021
1 parent 2517826 commit 2629915
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,34 @@ public class CommunicationIdentityClient
private readonly ClientDiagnostics _clientDiagnostics;
internal CommunicationIdentityRestClient RestClient { get; }

/// <summary> Initializes a new instance of <see cref="CommunicationIdentityClient"/>.</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 CommunicationIdentityClient(Uri endpoint, AzureKeyCredential keyCredential, CommunicationIdentityClientOptions options = default)
: this(
AssertNotNull(endpoint, nameof(endpoint)),
options ?? new CommunicationIdentityClientOptions(),
AssertNotNull(keyCredential, nameof(keyCredential)))
{ }
#region public constructors - all argument need null check

/// <summary> Initializes a new instance of <see cref="CommunicationIdentityClient"/>.</summary>
/// <param name="connectionString">Connection string acquired from the Azure Communication Services resource.</param>
public CommunicationIdentityClient(string connectionString)
: this(
new CommunicationIdentityClientOptions(),
ConnectionString.Parse(AssertNotNull(connectionString, nameof(connectionString))))
ConnectionString.Parse(AssertNotNullOrEmpty(connectionString, nameof(connectionString))),
new CommunicationIdentityClientOptions())
{ }

/// <summary> Initializes a new instance of <see cref="CommunicationIdentityClient"/>.</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 CommunicationIdentityClient(string connectionString, CommunicationIdentityClientOptions options)
: this(
options ?? new CommunicationIdentityClientOptions(),
ConnectionString.Parse(AssertNotNull(connectionString, nameof(connectionString))))
ConnectionString.Parse(AssertNotNullOrEmpty(connectionString, nameof(connectionString))),
options ?? new CommunicationIdentityClientOptions())
{ }

/// <summary> Initializes a new instance of <see cref="CommunicationIdentityClient"/>.</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 CommunicationIdentityClient(Uri endpoint, AzureKeyCredential keyCredential, CommunicationIdentityClientOptions options = default)
: this(
AssertNotNull(endpoint, nameof(endpoint)).AbsoluteUri,
AssertNotNull(keyCredential, nameof(keyCredential)),
options ?? new CommunicationIdentityClientOptions())
{ }

/// <summary> Initializes a new instance of <see cref="CommunicationIdentityClient"/>.</summary>
Expand All @@ -54,43 +56,40 @@ public CommunicationIdentityClient(string connectionString, CommunicationIdentit
/// <param name="options">Client option exposing <see cref="ClientOptions.Diagnostics"/>, <see cref="ClientOptions.Retry"/>, <see cref="ClientOptions.Transport"/>, etc.</param>
public CommunicationIdentityClient(Uri endpoint, TokenCredential tokenCredential, CommunicationIdentityClientOptions options = default)
: this(
AssertNotNull(endpoint, nameof(endpoint)),
options ?? new CommunicationIdentityClientOptions(),
AssertNotNull(tokenCredential, nameof(tokenCredential)))
AssertNotNull(endpoint, nameof(endpoint)).AbsoluteUri,
AssertNotNull(tokenCredential, nameof(tokenCredential)),
options ?? new CommunicationIdentityClientOptions())
{ }

private CommunicationIdentityClient(CommunicationIdentityClientOptions options, ConnectionString connectionString)
{
_clientDiagnostics = new ClientDiagnostics(options);
RestClient = new CommunicationIdentityRestClient(
_clientDiagnostics,
options.BuildHttpPipeline(connectionString),
connectionString.GetRequired("endpoint"));
}
#endregion

private CommunicationIdentityClient(Uri endpoint, CommunicationIdentityClientOptions options, AzureKeyCredential credential)
{
_clientDiagnostics = new ClientDiagnostics(options);
RestClient = new CommunicationIdentityRestClient(
_clientDiagnostics,
options.BuildHttpPipeline(credential),
endpoint.AbsoluteUri);
}
#region private constructors

private CommunicationIdentityClient(ConnectionString connectionString, CommunicationIdentityClientOptions options)
: this(connectionString.GetRequired("endpoint"), options.BuildHttpPipeline(connectionString), options)
{ }

private CommunicationIdentityClient(string endpoint, AzureKeyCredential keyCredential, CommunicationIdentityClientOptions options)
: this(endpoint, options.BuildHttpPipeline(keyCredential), options)
{ }

private CommunicationIdentityClient(string endpoint, TokenCredential tokenCredential, CommunicationIdentityClientOptions options)
: this(endpoint, options.BuildHttpPipeline(tokenCredential), options)
{ }

private CommunicationIdentityClient(Uri endpoint, CommunicationIdentityClientOptions options, TokenCredential tokenCredential)
private CommunicationIdentityClient(string endpoint, HttpPipeline httpPipeline, CommunicationIdentityClientOptions options)
{
_clientDiagnostics = new ClientDiagnostics(options);
RestClient = new CommunicationIdentityRestClient(
_clientDiagnostics,
options.BuildHttpPipeline(tokenCredential),
endpoint.AbsoluteUri);
RestClient = new CommunicationIdentityRestClient(_clientDiagnostics, httpPipeline, endpoint, options.ApiVersion);
}

#endregion

/// <summary>Initializes a new instance of <see cref="CommunicationIdentityClient"/> for mocking.</summary>
protected CommunicationIdentityClient()
{
_clientDiagnostics = null!;
RestClient = null!;
_clientDiagnostics = null;
RestClient = null;
}

/// <summary>Creates a new <see cref="CommunicationUserIdentifier"/>.</summary>
Expand Down Expand Up @@ -296,5 +295,11 @@ private static T AssertNotNull<T>(T argument, string argumentName)
Argument.AssertNotNull(argument, argumentName);
return argument;
}

private static string AssertNotNullOrEmpty(string argument, string argumentName)
{
Argument.AssertNotNullOrEmpty(argument, argumentName);
return argument;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ namespace Azure.Communication.PhoneNumbers
public partial class PhoneNumberAdministrationClient
{
protected PhoneNumberAdministrationClient() { }
public PhoneNumberAdministrationClient(string connectionString, Azure.Communication.PhoneNumbers.PhoneNumberAdministrationClientOptions? options = null) { }
public PhoneNumberAdministrationClient(string connectionString) { }
public PhoneNumberAdministrationClient(string connectionString, Azure.Communication.PhoneNumbers.PhoneNumberAdministrationClientOptions options) { }
public PhoneNumberAdministrationClient(System.Uri endpoint, Azure.AzureKeyCredential keyCredential, Azure.Communication.PhoneNumbers.PhoneNumberAdministrationClientOptions? options = null) { }
public PhoneNumberAdministrationClient(System.Uri endpoint, Azure.Core.TokenCredential tokenCredential, Azure.Communication.PhoneNumbers.PhoneNumberAdministrationClientOptions? options = null) { }
public virtual Azure.Response CancelReservation(string reservationId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,35 @@ public class PhoneNumberAdministrationClient
internal ClientDiagnostics ClientDiagnostics { get; private set; }
internal PhoneNumberAdministrationRestClient RestClient { get; }

/// <summary> Initializes a new instance of <see cref="PhoneNumberAdministrationClient"/>.</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 PhoneNumberAdministrationClient(Uri endpoint, AzureKeyCredential keyCredential, PhoneNumberAdministrationClientOptions? options = default)
#region public constructors - all arguments need null check

/// <summary>
/// Initializes a phone number administration client with an Azure resource connection string and client options.
/// </summary>
public PhoneNumberAdministrationClient(string connectionString)
: this(
AssertNotNull(endpoint, nameof(endpoint)),
options ?? new PhoneNumberAdministrationClientOptions(),
AssertNotNull(keyCredential, nameof(keyCredential)))
ConnectionString.Parse(AssertNotNullOrEmpty(connectionString, nameof(connectionString))),
new PhoneNumberAdministrationClientOptions())
{ }

/// <summary>
/// Initializes a phone number administration client with an Azure resource connection string and client options.
/// </summary>
public PhoneNumberAdministrationClient(string connectionString, PhoneNumberAdministrationClientOptions? options = default)
public PhoneNumberAdministrationClient(string connectionString, PhoneNumberAdministrationClientOptions options)
: this(
ConnectionString.Parse(AssertNotNullOrEmpty(connectionString, nameof(connectionString))),
options ?? new PhoneNumberAdministrationClientOptions())
{ }

/// <summary> Initializes a new instance of <see cref="PhoneNumberAdministrationClient"/>.</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 PhoneNumberAdministrationClient(Uri endpoint, AzureKeyCredential keyCredential, PhoneNumberAdministrationClientOptions? options = default)
: this(
options ?? new PhoneNumberAdministrationClientOptions(),
ConnectionString.Parse(AssertNotNull(connectionString, nameof(connectionString))))
AssertNotNull(endpoint, nameof(endpoint)).AbsoluteUri,
AssertNotNull(keyCredential, nameof(keyCredential)),
options ?? new PhoneNumberAdministrationClientOptions())
{ }

/// <summary>
Expand All @@ -49,42 +60,35 @@ public PhoneNumberAdministrationClient(string connectionString, PhoneNumberAdmin
/// </summary>
public PhoneNumberAdministrationClient(Uri endpoint, TokenCredential tokenCredential, PhoneNumberAdministrationClientOptions? options = default)
: this(
endpoint,
options ?? new PhoneNumberAdministrationClientOptions(),
tokenCredential)
AssertNotNull(endpoint, nameof(endpoint)).AbsoluteUri,
AssertNotNull(tokenCredential, nameof(tokenCredential)),
options ?? new PhoneNumberAdministrationClientOptions())
{ }

internal PhoneNumberAdministrationClient(PhoneNumberAdministrationClientOptions options, ConnectionString connectionString)
: this(new ClientDiagnostics(options), options.BuildHttpPipeline(connectionString), connectionString.GetRequired("endpoint"))
#endregion

#region private constructors

private PhoneNumberAdministrationClient(ConnectionString connectionString, PhoneNumberAdministrationClientOptions options)
: this(connectionString.GetRequired("endpoint"), options.BuildHttpPipeline(connectionString), options)
{ }

internal PhoneNumberAdministrationClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, string endpointUrl)
{
RestClient = new PhoneNumberAdministrationRestClient(clientDiagnostics, pipeline, endpointUrl);
ClientDiagnostics = clientDiagnostics;
}
private PhoneNumberAdministrationClient(string endpoint, AzureKeyCredential keyCredential, PhoneNumberAdministrationClientOptions options)
: this(endpoint, options.BuildHttpPipeline(keyCredential), options)
{ }

internal PhoneNumberAdministrationClient(Uri endpoint, PhoneNumberAdministrationClientOptions options, AzureKeyCredential credential)
{
ClientDiagnostics = new ClientDiagnostics(options);
RestClient = new PhoneNumberAdministrationRestClient(
ClientDiagnostics,
options.BuildHttpPipeline(credential),
endpoint.AbsoluteUri);
}
private PhoneNumberAdministrationClient(string endpoint, TokenCredential tokenCredential, PhoneNumberAdministrationClientOptions options)
: this(endpoint, options.BuildHttpPipeline(tokenCredential), options)
{ }

internal PhoneNumberAdministrationClient(Uri endpoint, PhoneNumberAdministrationClientOptions options, TokenCredential tokenCredential)
private PhoneNumberAdministrationClient(string endpoint, HttpPipeline httpPipeline, PhoneNumberAdministrationClientOptions options)
{
Argument.AssertNotNull(endpoint, nameof(endpoint));
Argument.AssertNotNull(tokenCredential, nameof(tokenCredential));

ClientDiagnostics = new ClientDiagnostics(options);
RestClient = new PhoneNumberAdministrationRestClient(
ClientDiagnostics,
options.BuildHttpPipeline(tokenCredential),
endpoint.AbsoluteUri);
RestClient = new PhoneNumberAdministrationRestClient(ClientDiagnostics, httpPipeline, endpoint, options.ApiVersion);
}

#endregion

/// <summary>Initializes a new instance of <see cref="PhoneNumberAdministrationClient"/> for mocking.</summary>
protected PhoneNumberAdministrationClient()
{
Expand Down Expand Up @@ -989,5 +993,11 @@ private static T AssertNotNull<T>(T argument, string argumentName)
Argument.AssertNotNull(argument, argumentName);
return argument;
}

private static string AssertNotNullOrEmpty(string argument, string argumentName)
{
Argument.AssertNotNullOrEmpty(argument, argumentName);
return argument;
}
}
}
Loading

0 comments on commit 2629915

Please sign in to comment.