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

Add UserAgent to IRequestConfiguration #140

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/Elastic.Transport/Components/Pipeline/RequestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public RequestData(ITransportConfiguration global, IRequestConfiguration? local
ProxyUsername = global.ProxyUsername;
ProxyPassword = global.ProxyPassword;
DisableAutomaticProxyDetection = global.DisableAutomaticProxyDetection;
UserAgent = global.UserAgent;
UserAgent = global.UserAgent ?? local?.UserAgent ?? RequestConfiguration.DefaultUserAgent;
KeepAliveInterval = (int)(global.KeepAliveInterval?.TotalMilliseconds ?? 2000);
KeepAliveTime = (int)(global.KeepAliveTime?.TotalMilliseconds ?? 2000);
RunAs = local?.RunAs ?? global.RunAs;
Expand Down Expand Up @@ -135,7 +135,7 @@ public RequestData(ITransportConfiguration global, IRequestConfiguration? local
public string? ProxyUsername { get; }
/// <inheritdoc cref="ITransportConfiguration.SkipDeserializationForStatusCodes"/>
public IReadOnlyCollection<int> SkipDeserializationForStatusCodes { get; }
/// <inheritdoc cref="ITransportConfiguration.UserAgent"/>
/// <inheritdoc cref="IRequestConfiguration.UserAgent"/>
public UserAgent UserAgent { get; }
/// <inheritdoc cref="ITransportConfiguration.DnsRefreshTimeout"/>
public TimeSpan DnsRefreshTimeout { get; }
Expand Down
6 changes: 6 additions & 0 deletions src/Elastic.Transport/Configuration/IRequestConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,10 @@ public interface IRequestConfiguration
/// Holds additional meta data about the request.
/// </summary>
RequestMetaData? RequestMetaData { get; }

/// <summary>
/// The user agent string to send with requests. Useful for debugging purposes to understand client and framework
/// versions that initiate requests to Elasticsearch
/// </summary>
UserAgent? UserAgent { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,6 @@ public interface ITransportConfiguration : IRequestConfiguration, IDisposable
/// </summary>
UrlFormatter UrlFormatter { get; }

/// <summary>
/// The user agent string to send with requests. Useful for debugging purposes to understand client and framework
/// versions that initiate requests to Elasticsearch
/// </summary>
UserAgent UserAgent { get; }

/// <summary>
/// Allow you to override the status code inspection that sets <see cref="ApiCallDetails.HasSuccessfulStatusCode"/>
/// <para>
Expand Down
7 changes: 7 additions & 0 deletions src/Elastic.Transport/Configuration/RequestConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public record RequestConfiguration : IRequestConfiguration
/// <summary> The default ping timeout when the connection is over HTTPS. Defaults to 5 seconds </summary>
public static readonly TimeSpan DefaultPingTimeoutOnSsl = TimeSpan.FromSeconds(5);

/// <summary> The default user-agent.</summary>
public static readonly UserAgent DefaultUserAgent = UserAgent.Create("elastic-transport-net");

/// <inheritdoc cref="IRequestConfiguration"/>
public RequestConfiguration()
{
Expand Down Expand Up @@ -63,6 +66,7 @@ public RequestConfiguration(IRequestConfiguration config)
ParseAllHeaders = config.ParseAllHeaders;
RequestMetaData = config.RequestMetaData;
ResponseBuilders = config.ResponseBuilders;
UserAgent = config.UserAgent;
}

/// <inheritdoc />
Expand Down Expand Up @@ -145,4 +149,7 @@ public RequestConfiguration(IRequestConfiguration config)

/// <inheritdoc />
public RequestMetaData? RequestMetaData { get; init; }

/// <inheritdoc />
public UserAgent? UserAgent { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public RequestConfigurationDescriptor(IRequestConfiguration config)
_parseAllHeaders = config.ParseAllHeaders;
_requestMetaData = config.RequestMetaData;
_responseBuilders = [.. config.ResponseBuilders];
_userAgent = config.UserAgent;
}

private string? _accept;
Expand Down Expand Up @@ -83,6 +84,7 @@ public RequestConfigurationDescriptor(IRequestConfiguration config)
private bool? _enableThreadPoolStats;
private RequestMetaData? _requestMetaData;
private List<IResponseBuilder>? _responseBuilders;
private UserAgent? _userAgent;

/// <inheritdoc cref="IRequestConfiguration.RunAs"/>
public RequestConfigurationDescriptor RunAs(string username)
Expand Down Expand Up @@ -289,6 +291,13 @@ public RequestConfigurationDescriptor ResponseBuilder(IResponseBuilder responseB
return this;
}

/// <inheritdoc cref="IRequestConfiguration.UserAgent" />
public RequestConfigurationDescriptor UserAgent(UserAgent userAgent)
{
_userAgent = userAgent;
return this;
}

string? IRequestConfiguration.Accept => _accept;

IReadOnlyCollection<int>? IRequestConfiguration.AllowedStatusCodes => _allowedStatusCodes;
Expand Down Expand Up @@ -342,4 +351,6 @@ public RequestConfigurationDescriptor ResponseBuilder(IResponseBuilder responseB
RequestMetaData? IRequestConfiguration.RequestMetaData => _requestMetaData;

IReadOnlyCollection<IResponseBuilder> IRequestConfiguration.ResponseBuilders => _responseBuilders ?? [];

UserAgent? IRequestConfiguration.UserAgent => _userAgent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ protected TransportConfigurationDescriptorBase(ITransportConfiguration config)
bool ITransportConfiguration.SniffsOnConnectionFault => _sniffsOnConnectionFault;
bool ITransportConfiguration.SniffsOnStartup => _sniffsOnStartup;
UrlFormatter ITransportConfiguration.UrlFormatter => _urlFormatter;
UserAgent ITransportConfiguration.UserAgent => _userAgent;
UserAgent IRequestConfiguration.UserAgent => _userAgent;
Func<HttpMethod, int, bool> ITransportConfiguration.StatusCodeToResponseSuccess => _statusCodeToResponseSuccess;
TimeSpan ITransportConfiguration.DnsRefreshTimeout => _dnsRefreshTimeout;
bool ITransportConfiguration.PrettyJson => _prettyJson;
Expand Down Expand Up @@ -513,7 +513,7 @@ public T ClientCertificate(string certificatePath) =>
public T SkipDeserializationForStatusCodes(params int[] statusCodes) =>
Assign(new ReadOnlyCollection<int>(statusCodes), static (a, v) => a._skipDeserializationForStatusCodes = v);

/// <inheritdoc cref="ITransportConfiguration.UserAgent"/>
/// <inheritdoc cref="IRequestConfiguration.UserAgent"/>
public T UserAgent(UserAgent userAgent) => Assign(userAgent, static (a, v) => a._userAgent = v);

/// <inheritdoc cref="IRequestConfiguration.TransferEncodingChunked"/>
Expand Down
4 changes: 2 additions & 2 deletions src/Elastic.Transport/Configuration/UserAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public sealed class UserAgent
{
private readonly string _toString;

private UserAgent(string reposName, Type typeVersionLookup, string[] metadata = null)
private UserAgent(string reposName, Type typeVersionLookup, string[]? metadata = null)
{
var version = typeVersionLookup.Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;

var meta = string.Join("; ", metadata ?? Array.Empty<string>());
var meta = string.Join("; ", metadata ?? []);
var assemblyName = typeVersionLookup.Assembly.GetName().Name;

_toString = $"{reposName}/{version} ({RuntimeInformation.OSDescription}; {RuntimeInformation.FrameworkDescription}; {assemblyName}{meta.Trim()})";
Expand Down
Loading