Skip to content

Commit

Permalink
feat: Support for API version header
Browse files Browse the repository at this point in the history
  • Loading branch information
jskeet committed Mar 12, 2024
1 parent a1d7cf1 commit 1cecc8f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Google.Api.Gax.Grpc.Tests/ClientHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,68 @@ public void BuildApiCall_PerMethodSettings()
Assert.Equal(perMethodSettings.CancellationToken, server.ClientStreamingCallOptions.CancellationToken);
}

[Fact]
public void WithApiVersionHeader()
{
string apiVersion = "abc";
var options = new ClientHelper.Options { Settings = new SimpleSettings(), ApiVersion = apiVersion };
var helper = new ClientHelper(options);

var server = new TestServer();

var unaryCall = helper.BuildApiCall<SimpleRequest, SimpleResponse>(
"Method", server.UnaryAsync, server.UnarySync, null);
unaryCall.Sync(null, null);
AssertVersionHeader(server.UnaryCallOptions);

var serverStreamingCall = helper.BuildApiCall<SimpleRequest, SimpleResponse>("Method", server.ServerStreaming, null);
serverStreamingCall.Call(null, null);
AssertVersionHeader(server.ServerStreamingCallOptions);

var bidiStreamingCall = helper.BuildApiCall("Method", server.BidiStreaming, null, null);
bidiStreamingCall.Call(null);
AssertVersionHeader(server.BidiStreamingCallOptions);

var clientStreamingCall = helper.BuildApiCall("Method", server.ClientStreaming, null, null);
clientStreamingCall.Call(null);
AssertVersionHeader(server.ClientStreamingCallOptions);

void AssertVersionHeader(CallOptions callOptions)
{
var entry = Assert.Single(callOptions.Headers, entry => entry.Key == ClientHelper.ApiVersionHeaderName);
Assert.Equal(apiVersion, entry.Value);
}
}

[Fact]
public void NoApiVersionHeader()
{
var options = new ClientHelper.Options { Settings = new SimpleSettings() };
var helper = new ClientHelper(options);

var server = new TestServer();

var unaryCall = helper.BuildApiCall<SimpleRequest, SimpleResponse>(
"Method", server.UnaryAsync, server.UnarySync, null);
unaryCall.Sync(null, null);
AssertNoVersionHeader(server.UnaryCallOptions);

var serverStreamingCall = helper.BuildApiCall<SimpleRequest, SimpleResponse>("Method", server.ServerStreaming, null);
serverStreamingCall.Call(null, null);
AssertNoVersionHeader(server.ServerStreamingCallOptions);

var bidiStreamingCall = helper.BuildApiCall("Method", server.BidiStreaming, null, null);
bidiStreamingCall.Call(null);
AssertNoVersionHeader(server.BidiStreamingCallOptions);

var clientStreamingCall = helper.BuildApiCall("Method", server.ClientStreaming, null, null);
clientStreamingCall.Call(null);
AssertNoVersionHeader(server.ClientStreamingCallOptions);

void AssertNoVersionHeader(CallOptions callOptions) =>
Assert.DoesNotContain(callOptions.Headers, entry => entry.Key == ClientHelper.ApiVersionHeaderName);
}

private class SimpleSettings: ServiceSettingsBase
{
public SimpleSettings() { }
Expand Down
17 changes: 17 additions & 0 deletions Google.Api.Gax.Grpc/ClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ namespace Google.Api.Gax.Grpc
/// </summary>
public class ClientHelper
{
// Visible for testing
internal const string ApiVersionHeaderName = "x-goog-api-version";

private readonly CallSettings _clientCallSettings;

/// <summary>
/// Call settings specifying headers for the client version (x-goog-api-client) and
/// optionally the API version (x-goog-api-version).
/// </summary>
private readonly CallSettings _versionCallSettings;

/// <summary>
Expand Down Expand Up @@ -49,6 +57,10 @@ public ClientHelper(Options options)
Scheduler = settings.Scheduler ?? SystemScheduler.Instance;
_clientCallSettings = settings.CallSettings;
_versionCallSettings = CallSettings.FromHeader(VersionHeaderBuilder.HeaderName, settings.VersionHeader);
if (options.ApiVersion is not null)
{
_versionCallSettings = _versionCallSettings.WithHeader(ApiVersionHeaderName, options.ApiVersion);
}
}

/// <summary>
Expand Down Expand Up @@ -187,6 +199,11 @@ public sealed class Options
/// The logger to use, if any. This may be null.
/// </summary>
public ILogger Logger { get; set; }

/// <summary>
/// The API version to send in the x-goog-api-version header, if any. This may be null.
/// </summary>
public string ApiVersion { get; set; }
}
}
}

0 comments on commit 1cecc8f

Please sign in to comment.