From 15994393deb646f69c3f20394c7bad47f3f26afe Mon Sep 17 00:00:00 2001 From: MrSmoke <709976+MrSmoke@users.noreply.github.com> Date: Mon, 20 Oct 2025 17:22:30 +1100 Subject: [PATCH 1/2] Add DefaultUserAgent option Sets the default user agent for the client --- .../src/Http/CoreRestClientOptions.cs | 1 + src/RestClient/RestClient/src/RestClient.cs | 27 +++++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/RestClient/RestClient/src/Http/CoreRestClientOptions.cs b/src/RestClient/RestClient/src/Http/CoreRestClientOptions.cs index a5e648a..e7fbbad 100644 --- a/src/RestClient/RestClient/src/Http/CoreRestClientOptions.cs +++ b/src/RestClient/RestClient/src/Http/CoreRestClientOptions.cs @@ -6,5 +6,6 @@ public class CoreRestClientOptions { public CompressionMethod CompressionMethod { get; set; } = CompressionMethod.None; public IAuthenticator? Authenticator { get; set; } = null; + public string? DefaultUserAgent { get; set; } } } diff --git a/src/RestClient/RestClient/src/RestClient.cs b/src/RestClient/RestClient/src/RestClient.cs index 0d356f7..bc1ceb6 100644 --- a/src/RestClient/RestClient/src/RestClient.cs +++ b/src/RestClient/RestClient/src/RestClient.cs @@ -1,7 +1,8 @@ -namespace ClickView.Extensions.RestClient +namespace ClickView.Extensions.RestClient { using System; using System.Net.Http; + using System.Net.Http.Headers; using System.Threading; using System.Threading.Tasks; using Exceptions; @@ -15,6 +16,7 @@ public class RestClient private readonly Uri? _baseAddress; private readonly HttpClient _httpClient; private readonly CoreRestClientOptions _options; + private readonly ProductInfoHeaderValue? _userAgent; /// /// Initializes a new instance of the class @@ -26,8 +28,9 @@ public RestClient(Uri baseAddress, RestClientOptions? options = null) _baseAddress = baseAddress ?? throw new ArgumentNullException(nameof(baseAddress)); var o = options ?? RestClientOptions.Default; - _options = o; + _options = o; + _userAgent = GetDefaultUserAgent(_options.DefaultUserAgent); _httpClient = CreateClient(o); } @@ -40,6 +43,8 @@ public RestClient(HttpClient httpClient, CoreRestClientOptions? options = null) { _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); _options = options ?? RestClientOptions.Default; + _userAgent = GetDefaultUserAgent(_options.DefaultUserAgent); + _baseAddress = null; } @@ -71,8 +76,6 @@ public async Task ExecuteAsync(BaseRestClientRequest ExecuteAsync(BaseRestClientRequest ExecuteAsync(BaseRestClientRequest Date: Mon, 20 Oct 2025 17:29:56 +1100 Subject: [PATCH 2/2] Simplify GetDefaultUserAgent check --- src/RestClient/RestClient/src/RestClient.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/RestClient/RestClient/src/RestClient.cs b/src/RestClient/RestClient/src/RestClient.cs index bc1ceb6..af0514b 100644 --- a/src/RestClient/RestClient/src/RestClient.cs +++ b/src/RestClient/RestClient/src/RestClient.cs @@ -162,7 +162,10 @@ private static HttpClient CreateClient(RestClientOptions options) private static ProductInfoHeaderValue? GetDefaultUserAgent(string? userAgent) { - return string.IsNullOrEmpty(userAgent) ? null : ProductInfoHeaderValue.Parse(userAgent); + if (userAgent is null) + return null; + + return ProductInfoHeaderValue.Parse(userAgent); } } }