Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request Options : Adds fix to support injecting/appending custom headers into request via RequestOptions. #2371

Merged
merged 15 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Microsoft.Azure.Cosmos/src/RequestOptions/RequestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public class RequestOptions
/// </summary>
public IReadOnlyDictionary<string, object> Properties { get; set; }

/// <summary>
/// Gets or sets a delegate which injects/appends a custom header in the request.
/// </summary>
public Action<Headers> AddRequestHeaders { get; set; }

/// <summary>
/// Gets or sets the boolean to use effective partition key routing in the cosmos db request.
/// </summary>
Expand Down Expand Up @@ -79,6 +84,17 @@ internal virtual void PopulateRequestOptions(RequestMessage request)
{
request.Headers.Add(HttpConstants.HttpHeaders.IfNoneMatch, this.IfNoneMatchEtag);
}

this.AddRequestHeaders?.Invoke(request.Headers);
}

/// <summary>
/// Clone RequestOptions.
/// </summary>
/// <returns> cloned RequestOptions. </returns>
public RequestOptions ShallowCopy()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it needed as explicit contract?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently required , so that we retain the original user passed RequestOptions ... MemberwiseClone method is protected in the Object class hence exposing ShallowCopy function and to be explicit about it being a shallow copy....

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the name: @j82w are there any guidelines from central SDK team for related such?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find any guideline in the central SDK team documentation.

{
return this.MemberwiseClone() as RequestOptions;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,42 @@ public async Task VerifyKnownHeaders()
}
}

[TestMethod]
public async Task VerifyRequestOptionCustomRequestHeaders()
{
CustomHeaderValidationHandler headerValidationHandler = new CustomHeaderValidationHandler();
using CosmosClient client = TestCommon.CreateCosmosClient(x => x.AddCustomHandlers(headerValidationHandler));
Database database = null;
try
{
database = await client.CreateDatabaseAsync(nameof(VerifyRequestOptionCustomRequestHeaders) + Guid.NewGuid().ToString());
Container container = await database.CreateContainerAsync(
Guid.NewGuid().ToString(),
"/pk");

ToDoActivity toDoActivity = ToDoActivity.CreateRandomToDoActivity();
ItemRequestOptions requestOptions = new ItemRequestOptions
{
AddRequestHeaders = (headers) => headers["x-ms-cosmos-database-rid"] = "databaseRidValue",
};

await container.CreateItemAsync(toDoActivity, new PartitionKey(toDoActivity.pk), requestOptions: requestOptions);

// null pass
requestOptions.AddRequestHeaders = null;

await container.ReadItemAsync<ToDoActivity>(toDoActivity.id, new PartitionKey(toDoActivity.pk), requestOptions: requestOptions);

}
finally
{
if (database != null)
{
await database.DeleteStreamAsync();
}
}
}

private class HeaderValidationHandler : RequestHandler
{
public override async Task<ResponseMessage> SendAsync(RequestMessage request, CancellationToken cancellationToken)
Expand Down Expand Up @@ -76,5 +112,28 @@ private void ValidateLazyHeadersAreNotCreated(CosmosMessageHeadersInternal inter
}
}
}

private class CustomHeaderValidationHandler : RequestHandler
{
public override async Task<ResponseMessage> SendAsync(RequestMessage request, CancellationToken cancellationToken)
{
if (request.ResourceType == Documents.ResourceType.Document)
{
this.ValidateCustomHeaders(request.Headers.CosmosMessageHeaders);
}

return await base.SendAsync(request, cancellationToken);
}

private void ValidateCustomHeaders(CosmosMessageHeadersInternal internalHeaders)
{
string customHeaderValue = internalHeaders.Get("x-ms-cosmos-database-rid");

if (!string.IsNullOrEmpty(customHeaderValue))
{
Assert.AreEqual("databaseRidValue", customHeaderValue);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5599,6 +5599,23 @@
}
},
"Members": {
"Microsoft.Azure.Cosmos.RequestOptions ShallowCopy()": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "Microsoft.Azure.Cosmos.RequestOptions ShallowCopy();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Action`1[Microsoft.Azure.Cosmos.Headers] AddRequestHeaders": {
"Type": "Property",
"Attributes": [],
"MethodInfo": "System.Action`1[Microsoft.Azure.Cosmos.Headers] AddRequestHeaders;CanRead:True;CanWrite:True;System.Action`1[Microsoft.Azure.Cosmos.Headers] get_AddRequestHeaders();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;Void set_AddRequestHeaders(System.Action`1[Microsoft.Azure.Cosmos.Headers]);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Action`1[Microsoft.Azure.Cosmos.Headers] get_AddRequestHeaders()[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
"CompilerGeneratedAttribute"
],
"MethodInfo": "System.Action`1[Microsoft.Azure.Cosmos.Headers] get_AddRequestHeaders();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Collections.Generic.IReadOnlyDictionary`2[System.String,System.Object] get_Properties()[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
Expand Down Expand Up @@ -5640,6 +5657,13 @@
"Attributes": [],
"MethodInfo": "[Void .ctor(), Void .ctor()]"
},
"Void set_AddRequestHeaders(System.Action`1[Microsoft.Azure.Cosmos.Headers])[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
"CompilerGeneratedAttribute"
],
"MethodInfo": "Void set_AddRequestHeaders(System.Action`1[Microsoft.Azure.Cosmos.Headers]);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"Void set_IfMatchEtag(System.String)[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
Expand Down