Skip to content

Commit

Permalink
refactor: Refactor ClientHelper construction to be more extensible
Browse files Browse the repository at this point in the history
  • Loading branch information
jskeet committed Mar 12, 2024
1 parent bfdd7aa commit a1d7cf1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
6 changes: 4 additions & 2 deletions Google.Api.Gax.Grpc.Tests/ClientHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class ClientHelperTest
public void BuildApiCall_ClientSettings()
{
var clientSettings = CallSettings.FromCancellationToken(new CancellationTokenSource().Token);
var helper = new ClientHelper(new SimpleSettings { CallSettings = clientSettings }, logger: null);
var options = new ClientHelper.Options { Settings = new SimpleSettings { CallSettings = clientSettings } };
var helper = new ClientHelper(options);
var server = new TestServer();

var unaryCall = helper.BuildApiCall<SimpleRequest, SimpleResponse>(
Expand All @@ -43,7 +44,8 @@ public void BuildApiCall_ClientSettings()
public void BuildApiCall_PerMethodSettings()
{
var perMethodSettings = CallSettings.FromCancellationToken(new CancellationTokenSource().Token);
var helper = new ClientHelper(new SimpleSettings(), logger: null);
var options = new ClientHelper.Options { Settings = new SimpleSettings() };
var helper = new ClientHelper(options);
var server = new TestServer();

var unaryCall = helper.BuildApiCall<SimpleRequest, SimpleResponse>(
Expand Down
42 changes: 39 additions & 3 deletions Google.Api.Gax.Grpc/ClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace Google.Api.Gax.Grpc
{
/// <summary>
/// Common helper code shared by clients.
/// Common helper code shared by clients. This class is primarily expected to be used from generated code.
/// </summary>
public class ClientHelper
{
Expand All @@ -24,12 +24,27 @@ public class ClientHelper
/// Constructs a helper from the given settings.
/// Behavior is undefined if settings are changed after construction.
/// </summary>
/// <remarks>
/// This constructor will be removed in the next major version of GAX.
/// </remarks>
/// <param name="settings">The service settings.</param>
/// <param name="logger">The logger to use for API calls</param>
public ClientHelper(ServiceSettingsBase settings, ILogger logger)
: this(new Options { Settings = GaxPreconditions.CheckNotNull(settings, nameof(settings)), Logger = logger })
{
GaxPreconditions.CheckNotNull(settings, nameof(settings));
Logger = logger;
}

/// <summary>
/// Constructs a helper from the given options. See the properties in <see cref="Options"/>
/// for validity constraints.
/// </summary>
/// <param name="options">The options for the helper.</param>
public ClientHelper(Options options)
{
GaxPreconditions.CheckNotNull(options, nameof(options));
var settings = options.Settings;
GaxPreconditions.CheckArgument(settings is not null, nameof(options), "{0} in options must not be null", nameof(Options.Settings));
Logger = options.Logger;
Clock = settings.Clock ?? SystemClock.Instance;
Scheduler = settings.Scheduler ?? SystemScheduler.Instance;
_clientCallSettings = settings.CallSettings;
Expand Down Expand Up @@ -152,5 +167,26 @@ public ApiClientStreamingCall<TRequest, TResponse> BuildApiCall<TRequest, TRespo
.WithLogging(Logger)
.WithMergedBaseCallSettings(_versionCallSettings);
}

/// <summary>
/// The options used to construct a <see cref="ClientHelper"/>.
/// </summary>
/// <remarks>
/// This class is designed to allow additional configuration to be introduced without
/// either overloading the ClientHelper constructor or making breaking changes.
/// </remarks>
public sealed class Options
{
/// <summary>
/// The service settings. This must not be null when the options
/// are passed to the <see cref="ClientHelper"/> constructor.
/// </summary>
public ServiceSettingsBase Settings { get; set; }

/// <summary>
/// The logger to use, if any. This may be null.
/// </summary>
public ILogger Logger { get; set; }
}
}
}

0 comments on commit a1d7cf1

Please sign in to comment.