-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add metadata header on requests #5190
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
409150a
Initial work to implement meta header
stevejgordon 76ae2fc
PR feedback, refactoring and tests
stevejgordon 9cc7149
Simplification after PR feedback
stevejgordon 71b1aa7
Cleanup
stevejgordon 3600d9a
Refactoring and simplifying
stevejgordon 55e0401
Add XML and update doc gen
stevejgordon 66dc611
Tweak test
stevejgordon 0dd55aa
Update observables
stevejgordon 91ca35f
Add meta header tests
stevejgordon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/Elasticsearch.Net/Api/RequestParameters/RequestParametersExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using System; | ||
|
|
||
| namespace Elasticsearch.Net | ||
| { | ||
| internal static class RequestParametersExtensions | ||
| { | ||
| internal static void SetRequestMetaData(this IRequestParameters parameters, RequestMetaData requestMetaData) | ||
| { | ||
| if (parameters is null) | ||
| throw new ArgumentNullException(nameof(parameters)); | ||
|
|
||
| if (requestMetaData is null) | ||
| throw new ArgumentNullException(nameof(requestMetaData)); | ||
|
|
||
| if (parameters.RequestConfiguration is null) | ||
| parameters.RequestConfiguration = new RequestConfiguration(); | ||
|
|
||
| parameters.RequestConfiguration.RequestMetaData = requestMetaData; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/Elasticsearch.Net/Configuration/RequestConfigurationExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using System; | ||
|
|
||
| namespace Elasticsearch.Net | ||
| { | ||
| internal static class RequestConfigurationExtensions | ||
| { | ||
| internal static void SetRequestMetaData(this IRequestConfiguration requestConfiguration, RequestMetaData requestMetaData) | ||
| { | ||
| if (requestConfiguration is null) | ||
| throw new ArgumentNullException(nameof(requestConfiguration)); | ||
|
|
||
| if (requestMetaData is null) | ||
| throw new ArgumentNullException(nameof(requestMetaData)); | ||
|
|
||
| requestConfiguration.RequestMetaData = requestMetaData; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Elasticsearch.Net | ||
| { | ||
| /// <summary> | ||
| /// Holds meta data about a client request. | ||
| /// </summary> | ||
| public sealed class RequestMetaData | ||
| { | ||
| /// <summary> | ||
| /// Reserved key for a meta data entry which identifies the helper which produced the request. | ||
| /// </summary> | ||
| internal const string HelperKey = "helper"; | ||
|
|
||
| private Dictionary<string, string> _metaDataItems; | ||
|
|
||
| internal bool TryAddMetaData (string key, string value) | ||
| { | ||
| if (_metaDataItems is null) | ||
| _metaDataItems = new Dictionary<string, string>(); | ||
|
|
||
| #if NETSTANDARD2_1 | ||
| return _metaDataItems.TryAdd(key, value); | ||
| #else | ||
| if (_metaDataItems.ContainsKey(key)) | ||
| return false; | ||
|
|
||
| _metaDataItems.Add(key, value); | ||
| return true; | ||
| #endif | ||
| } | ||
|
|
||
| public IReadOnlyDictionary<string, string> Items => _metaDataItems is null ? EmptyReadOnly<string, string>.Dictionary : _metaDataItems; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using System; | ||
| #if DOTNETCORE | ||
| using System.Net.Http; | ||
| #endif | ||
|
|
||
| namespace Elasticsearch.Net | ||
| { | ||
| public static class ConnectionInfo | ||
| { | ||
| public static bool UsingCurlHandler | ||
| { | ||
| get | ||
| { | ||
| #if !DOTNETCORE | ||
| return false; | ||
| #else | ||
| var curlHandlerExists = typeof(HttpClientHandler).Assembly.GetType("System.Net.Http.CurlHandler") != null; | ||
| if (!curlHandlerExists) | ||
| return false; | ||
|
|
||
| var socketsHandlerExists = typeof(HttpClientHandler).Assembly.GetType("System.Net.Http.SocketsHttpHandler") != null; | ||
| // running on a .NET core version with CurlHandler, before the existence of SocketsHttpHandler. | ||
| // Must be using CurlHandler. | ||
| if (!socketsHandlerExists) | ||
| return true; | ||
|
|
||
| if (AppContext.TryGetSwitch("System.Net.Http.UseSocketsHttpHandler", out var isEnabled)) | ||
| return !isEnabled; | ||
|
|
||
| var environmentVariable = | ||
| Environment.GetEnvironmentVariable("DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER"); | ||
|
|
||
| // SocketsHandler exists and no environment variable exists to disable it. | ||
| // Must be using SocketsHandler and not CurlHandler | ||
| if (environmentVariable == null) | ||
| return false; | ||
|
|
||
| return environmentVariable.Equals("false", StringComparison.OrdinalIgnoreCase) || | ||
| environmentVariable.Equals("0"); | ||
| #endif | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| namespace Elasticsearch.Net | ||
| { | ||
| /// <summary> | ||
| /// A provider for additional HTTP request headers. | ||
| /// </summary> | ||
| public interface IHeaderProvider | ||
| { | ||
| /// <summary> | ||
| /// The name of the header produced by this provider. | ||
| /// </summary> | ||
| string HeaderName { get; } | ||
|
|
||
| /// <summary> | ||
| /// Produces the value for the header using information from the <see cref="RequestData"/>. | ||
| /// </summary> | ||
| /// <param name="requestData">Data about the request which may be used to produce the header value.</param> | ||
| /// <returns></returns> | ||
| string ProduceHeaderValue(RequestData requestData); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.