Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
30 changes: 22 additions & 8 deletions src/RestClient/RestClient/src/RestClient.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,6 +16,7 @@ public class RestClient
private readonly Uri? _baseAddress;
private readonly HttpClient _httpClient;
private readonly CoreRestClientOptions _options;
private readonly ProductInfoHeaderValue? _userAgent;

/// <summary>
/// Initializes a new instance of the <see cref="RestClient" /> class
Expand All @@ -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);
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -71,8 +76,6 @@ public async Task<TResponse> ExecuteAsync<TResponse>(BaseRestClientRequest<TResp
if (request == null)
throw new ArgumentNullException(nameof(request));

token.ThrowIfCancellationRequested();

if (_options.Authenticator != null)
await _options.Authenticator.AuthenticateAsync(request, token).ConfigureAwait(false);

Expand All @@ -93,7 +96,6 @@ public async Task<TResponse> ExecuteAsync<TResponse>(BaseRestClientRequest<TResp
{
// only do the following logic if the _httpClient doesn't have a base address already
// we should remove this if we update the ctor to always have a baseAddress

if (_httpClient.BaseAddress == null)
{
throw new InvalidOperationException(
Expand All @@ -105,10 +107,14 @@ public async Task<TResponse> ExecuteAsync<TResponse>(BaseRestClientRequest<TResp
}

using var httpRequest = new HttpRequestMessage(request.Method, requestUri);

// Add user agent if supplied
if (_userAgent is not null)
httpRequest.Headers.UserAgent.Add(_userAgent);

// Add all the headers from the request and validate them.
foreach (var h in request.Headers)
{
httpRequest.Headers.TryAddWithoutValidation(h.Key, h.Value);
}
httpRequest.Headers.Add(h.Key, h.Value);

httpRequest.Content = GetContent(request);

Expand Down Expand Up @@ -153,5 +159,13 @@ private static HttpClient CreateClient(RestClientOptions options)
Timeout = options.Timeout
};
}

private static ProductInfoHeaderValue? GetDefaultUserAgent(string? userAgent)
{
if (userAgent is null)
return null;

return ProductInfoHeaderValue.Parse(userAgent);
}
}
}