From 333146ea53af9bc372b731f5a223ef1cf7d44d91 Mon Sep 17 00:00:00 2001 From: Albie Spriddell Date: Fri, 9 Aug 2024 13:41:45 +0100 Subject: [PATCH 1/4] add baseaddress property --- DragonFruit.Data/ApiClient.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/DragonFruit.Data/ApiClient.cs b/DragonFruit.Data/ApiClient.cs index e103e9f..44d0e2e 100644 --- a/DragonFruit.Data/ApiClient.cs +++ b/DragonFruit.Data/ApiClient.cs @@ -30,12 +30,19 @@ namespace DragonFruit.Data public class ApiClient { private HttpClient _client; + private Uri _baseAddress; public ApiClient(ApiSerializer serializer) { Serializers = new SerializerResolver(serializer); } + public ApiClient(ApiSerializer serializer, Uri baseAddress) + : this(serializer) + { + _baseAddress = baseAddress; + } + ~ApiClient() { _client?.Dispose(); @@ -69,6 +76,23 @@ public string UserAgent } } + /// + /// Gets or sets the that should act as the base address for relative-URI requests + /// + public Uri BaseAddress + { + get => _client?.BaseAddress ?? _baseAddress; + set + { + _baseAddress = value; + + if (_client != null) + { + _client.BaseAddress = value; + } + } + } + /// /// The instance used to resolve serializers for requests. /// Caches and reused serializers where possible. @@ -327,6 +351,7 @@ protected virtual HttpClient CreateClient() client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher; #endif + client.BaseAddress = _baseAddress; return client; } From f40b0ca5d5a5723d8f056b816cad8ab80815d9d8 Mon Sep 17 00:00:00 2001 From: Albie Spriddell Date: Fri, 9 Aug 2024 13:42:15 +0100 Subject: [PATCH 2/4] refactor request building logic, add support check to socketshttphandler --- DragonFruit.Data/ApiClient.cs | 39 ++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/DragonFruit.Data/ApiClient.cs b/DragonFruit.Data/ApiClient.cs index 44d0e2e..9acc738 100644 --- a/DragonFruit.Data/ApiClient.cs +++ b/DragonFruit.Data/ApiClient.cs @@ -387,40 +387,41 @@ protected virtual async Task ValidateAndProcess(HttpResponseMessage respon /// The to send protected virtual async ValueTask BuildRequest(ApiRequest request, string expectedContentType) { - if (request is IRequestExecutingCallback callback) + switch (request) { - callback.OnRequestExecuting(this); - } + case IRequestExecutingCallback callback: + callback.OnRequestExecuting(this); + break; - if (request is IAsyncRequestExecutingCallback asyncCallback) - { - await asyncCallback.OnRequestExecuting(this); + case IAsyncRequestExecutingCallback asyncCallback: + await asyncCallback.OnRequestExecuting(this); + break; } - var requestMessage = request is IRequestBuilder rb - ? rb.BuildRequest(Serializers) - : ReflectionRequestMessageBuilder.CreateHttpRequestMessage(request, Serializers); - + var requestMessage = (request as IRequestBuilder)?.BuildRequest(Serializers) ?? ReflectionRequestMessageBuilder.CreateHttpRequestMessage(request, Serializers); requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(expectedContentType)); + return requestMessage; } public static HttpMessageHandler CreateDefaultHandler() { -#if NETSTANDARD2_0 +#if !NETSTANDARD2_0 + if (SocketsHttpHandler.IsSupported) + { + return new SocketsHttpHandler + { + UseCookies = false, + AutomaticDecompression = DecompressionMethods.All, + PooledConnectionLifetime = TimeSpan.FromMinutes(10) + }; + } +#endif return new HttpClientHandler { UseCookies = false, AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip }; -#else - return new SocketsHttpHandler - { - UseCookies = false, - AutomaticDecompression = DecompressionMethods.All, - PooledConnectionLifetime = TimeSpan.FromMinutes(10) - }; -#endif } } } From 5daba5405feba8aea3685fbd8b85ded2f6132beb Mon Sep 17 00:00:00 2001 From: Albie Spriddell Date: Fri, 9 Aug 2024 13:44:16 +0100 Subject: [PATCH 3/4] replace activator call with new T() --- DragonFruit.Data/ApiClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DragonFruit.Data/ApiClient.cs b/DragonFruit.Data/ApiClient.cs index 9acc738..eac053b 100644 --- a/DragonFruit.Data/ApiClient.cs +++ b/DragonFruit.Data/ApiClient.cs @@ -22,7 +22,7 @@ namespace DragonFruit.Data /// Represents a strongly-typed serializer version of /// /// The type of the - public class ApiClient() : ApiClient(Activator.CreateInstance()) where T : ApiSerializer, new(); + public class ApiClient() : ApiClient(new T()) where T : ApiSerializer, new(); /// /// The responsible for building, submitting and processing HTTP requests From 5815634ad91d4cd6aa24db99c9ab8e92926cace5 Mon Sep 17 00:00:00 2001 From: Albie Spriddell Date: Fri, 9 Aug 2024 13:45:44 +0100 Subject: [PATCH 4/4] add baseAddress ctor overload --- DragonFruit.Data/ApiClient.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/DragonFruit.Data/ApiClient.cs b/DragonFruit.Data/ApiClient.cs index eac053b..f03a6eb 100644 --- a/DragonFruit.Data/ApiClient.cs +++ b/DragonFruit.Data/ApiClient.cs @@ -22,7 +22,18 @@ namespace DragonFruit.Data /// Represents a strongly-typed serializer version of /// /// The type of the - public class ApiClient() : ApiClient(new T()) where T : ApiSerializer, new(); + public class ApiClient : ApiClient where T : ApiSerializer, new() + { + public ApiClient() + : base(new T()) + { + } + + public ApiClient(Uri baseAddress) + : base(new T(), baseAddress) + { + } + } /// /// The responsible for building, submitting and processing HTTP requests