From f3b258e9b873f841a18f61d6b6933fc99b219328 Mon Sep 17 00:00:00 2001 From: tangbao <1427522930@qq.com> Date: Wed, 7 Dec 2022 11:01:33 +0800 Subject: [PATCH 01/10] feat: add HttpClient as a part of InfluxDBClientOptions --- Client/InfluxDBClientOptions.cs | 31 +++++++++++++++++++++++++++++++ Client/Internal/ApiClient.cs | 5 ++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Client/InfluxDBClientOptions.cs b/Client/InfluxDBClientOptions.cs index 539e2d986..2e090dd43 100644 --- a/Client/InfluxDBClientOptions.cs +++ b/Client/InfluxDBClientOptions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Configuration; using System.Net; +using System.Net.Http; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Text.RegularExpressions; @@ -34,6 +35,7 @@ public class InfluxDBClientOptions private bool _allowHttpRedirects; private bool _verifySsl; private X509CertificateCollection _clientCertificates; + private HttpClient _httpClient; /// /// Set the url to connect the InfluxDB. @@ -251,6 +253,15 @@ public void AddDefaultTags(Dictionary tags) } } + /// + /// Add a HttpClient as a part of InfluxDBClientOptions + /// + public HttpClient HttpClient + { + get => _httpClient; + set => _httpClient = value; + } + /// /// Create an instance of InfluxDBClientOptions. The url could be a connection string with various configurations. /// @@ -428,6 +439,11 @@ private InfluxDBClientOptions(Builder builder) { ClientCertificates = builder.CertificateCollection; } + + if (builder.HttpClient != null) + { + HttpClient = builder.HttpClient; + } } private static TimeSpan ToTimeout(string value) @@ -506,6 +522,7 @@ public sealed class Builder internal bool VerifySslCertificates = true; internal RemoteCertificateValidationCallback VerifySslCallback; internal X509CertificateCollection CertificateCollection; + internal HttpClient HttpClient; internal PointSettings PointSettings = new PointSettings(); @@ -828,6 +845,20 @@ public InfluxDBClientOptions Build() return new InfluxDBClientOptions(this); } + + /// + /// Configure HttpClient + /// + /// + /// + public Builder SetHttpClient(HttpClient httpClient) + { + Arguments.CheckNotNull(httpClient, nameof(httpClient)); + + HttpClient = httpClient; + + return this; + } } } } \ No newline at end of file diff --git a/Client/Internal/ApiClient.cs b/Client/Internal/ApiClient.cs index 696cc114d..5ee9cacb4 100644 --- a/Client/Internal/ApiClient.cs +++ b/Client/Internal/ApiClient.cs @@ -58,7 +58,10 @@ public ApiClient(InfluxDBClientOptions options, LoggingHandler loggingHandler, G RestClientOptions.ClientCertificates.AddRange(options.ClientCertificates); } - RestClient = new RestClient(RestClientOptions); + RestClient = options.HttpClient == null + ? new RestClient(RestClientOptions) + : new RestClient(options.HttpClient, RestClientOptions); + Configuration = new Configuration { ApiClient = this, From 7995cfdce4e81eaf6475ba8b20ff568afb7a5f6a Mon Sep 17 00:00:00 2001 From: tangbao <1427522930@qq.com> Date: Wed, 31 May 2023 16:06:22 +0800 Subject: [PATCH 02/10] test: add test for allowing injection of custom HttpMessageHandler --- Client.Test/ApiClientTest.cs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Client.Test/ApiClientTest.cs b/Client.Test/ApiClientTest.cs index 4b4ffbcce..890c09153 100644 --- a/Client.Test/ApiClientTest.cs +++ b/Client.Test/ApiClientTest.cs @@ -1,8 +1,11 @@ using System; using System.Net; +using System.Net.Http; using InfluxDB.Client.Api.Client; using InfluxDB.Client.Core; using InfluxDB.Client.Core.Internal; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using NUnit.Framework; namespace InfluxDB.Client.Test @@ -61,5 +64,35 @@ public void ProxyDefaultConfigured() Assert.AreEqual(webProxy, _apiClient.RestClientOptions.Proxy); } + + [Test] + public void InjectHttpClient() + { + var webProxy = new WebProxy("my-proxy", 8088); + + var options = new InfluxDBClientOptions("http://localhost:8086") + { + Token = "my-token", + WebProxy = webProxy + }; + var services = new ServiceCollection(); + + services.AddHttpClient(); + + services.AddTransient(p => + { + var client = p.GetService(); + + options.HttpClient = client; + + return new ApiClient(options, new LoggingHandler(LogLevel.Body), new GzipHandler()); + }); + + var builder = services.BuildServiceProvider(); + + var apiClient = builder.GetRequiredService(); + + Assert.NotNull(apiClient); + } } } \ No newline at end of file From adbff88835af15e1b77f3270ec1f81b0e25f55d5 Mon Sep 17 00:00:00 2001 From: tangbao <1427522930@qq.com> Date: Wed, 31 May 2023 17:07:12 +0800 Subject: [PATCH 03/10] docs: edit CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 128fe4033..f366109e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +### Features +1. [#528](https://github.com/influxdata/influxdb-client-csharp/pull/528): Add HttpClient as a part of InfluxDBClientOptions + ## 4.10.0 [unreleased] ## 4.9.0 [2020-12-06] From 0f6f24f07152dbae843de48dd40943b47d3a193c Mon Sep 17 00:00:00 2001 From: tangbao <1427522930@qq.com> Date: Thu, 1 Jun 2023 09:12:23 +0800 Subject: [PATCH 04/10] test: modify the test for allowing injection of custom HttpMessageHandler --- Client.Test/ApiClientTest.cs | 37 ++---------------------- Client.Test/InfluxDbClientFactoryTest.cs | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 35 deletions(-) diff --git a/Client.Test/ApiClientTest.cs b/Client.Test/ApiClientTest.cs index 890c09153..6f2e22176 100644 --- a/Client.Test/ApiClientTest.cs +++ b/Client.Test/ApiClientTest.cs @@ -1,12 +1,9 @@ -using System; -using System.Net; -using System.Net.Http; using InfluxDB.Client.Api.Client; using InfluxDB.Client.Core; using InfluxDB.Client.Core.Internal; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; using NUnit.Framework; +using System; +using System.Net; namespace InfluxDB.Client.Test { @@ -64,35 +61,5 @@ public void ProxyDefaultConfigured() Assert.AreEqual(webProxy, _apiClient.RestClientOptions.Proxy); } - - [Test] - public void InjectHttpClient() - { - var webProxy = new WebProxy("my-proxy", 8088); - - var options = new InfluxDBClientOptions("http://localhost:8086") - { - Token = "my-token", - WebProxy = webProxy - }; - var services = new ServiceCollection(); - - services.AddHttpClient(); - - services.AddTransient(p => - { - var client = p.GetService(); - - options.HttpClient = client; - - return new ApiClient(options, new LoggingHandler(LogLevel.Body), new GzipHandler()); - }); - - var builder = services.BuildServiceProvider(); - - var apiClient = builder.GetRequiredService(); - - Assert.NotNull(apiClient); - } } } \ No newline at end of file diff --git a/Client.Test/InfluxDbClientFactoryTest.cs b/Client.Test/InfluxDbClientFactoryTest.cs index 21fdbc5d5..84a8d24a6 100644 --- a/Client.Test/InfluxDbClientFactoryTest.cs +++ b/Client.Test/InfluxDbClientFactoryTest.cs @@ -2,11 +2,14 @@ using System.Collections.Generic; using System.Configuration; using System.IO; +using System.Net; +using System.Net.Http; using System.Reflection; using System.Security.Cryptography.X509Certificates; using InfluxDB.Client.Api.Client; using InfluxDB.Client.Core; using InfluxDB.Client.Core.Exceptions; +using Microsoft.Extensions.DependencyInjection; using NUnit.Framework; namespace InfluxDB.Client.Test @@ -546,6 +549,33 @@ public void CertificatesFactory() Assert.AreEqual(certificateCollection, apiClient.RestClientOptions.ClientCertificates); } + [Test] + public void InjectHttpClient() + { + var options = new InfluxDBClientOptions("http://localhost:8086") + { + Token = "my-token" + }; + var services = new ServiceCollection(); + + services.AddHttpClient(); + + services.AddTransient(p => + { + options.HttpClient = p.GetService(); + + return new InfluxDBClient(options); + }); + var builder = services.BuildServiceProvider(); + + _client = builder.GetRequiredService(); + + var restClient = GetDeclaredField(_client.GetType(), _client, "_apiClient").RestClient; + + Assert.AreEqual(options.HttpClient, + GetDeclaredField(restClient.GetType(), restClient, "k__BackingField")); + } + private static T GetDeclaredField(IReflect type, object instance, string fieldName) { const BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic From c52af0a3be1f5b19affb00492a93d684329844f7 Mon Sep 17 00:00:00 2001 From: tangbao <1427522930@qq.com> Date: Thu, 1 Jun 2023 10:01:46 +0800 Subject: [PATCH 05/10] test: modify test errors. --- Client.Test/InfluxDbClientFactoryTest.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Client.Test/InfluxDbClientFactoryTest.cs b/Client.Test/InfluxDbClientFactoryTest.cs index 84a8d24a6..4968b44ea 100644 --- a/Client.Test/InfluxDbClientFactoryTest.cs +++ b/Client.Test/InfluxDbClientFactoryTest.cs @@ -562,7 +562,9 @@ public void InjectHttpClient() services.AddTransient(p => { - options.HttpClient = p.GetService(); + var httpClientFactory = p.GetService(); + + options.HttpClient = httpClientFactory.CreateClient(); return new InfluxDBClient(options); }); @@ -572,7 +574,7 @@ public void InjectHttpClient() var restClient = GetDeclaredField(_client.GetType(), _client, "_apiClient").RestClient; - Assert.AreEqual(options.HttpClient, + Assert.AreEqual(options.HttpClient, GetDeclaredField(restClient.GetType(), restClient, "k__BackingField")); } From 1a94e43ed6d624f80e5b0811506ba6ce61155ebd Mon Sep 17 00:00:00 2001 From: tangbao <1427522930@qq.com> Date: Thu, 1 Jun 2023 10:09:05 +0800 Subject: [PATCH 06/10] style: formatting code --- Client.Test/InfluxDbClientFactoryTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Client.Test/InfluxDbClientFactoryTest.cs b/Client.Test/InfluxDbClientFactoryTest.cs index 4968b44ea..4b18541cf 100644 --- a/Client.Test/InfluxDbClientFactoryTest.cs +++ b/Client.Test/InfluxDbClientFactoryTest.cs @@ -564,9 +564,9 @@ public void InjectHttpClient() { var httpClientFactory = p.GetService(); - options.HttpClient = httpClientFactory.CreateClient(); + options.HttpClient = httpClientFactory.CreateClient(); - return new InfluxDBClient(options); + return new InfluxDBClient(options); }); var builder = services.BuildServiceProvider(); From 7cbe7ed5bb4405b7e96c4af5b4518ce05e931e16 Mon Sep 17 00:00:00 2001 From: tangbao <1427522930@qq.com> Date: Thu, 1 Jun 2023 10:43:04 +0800 Subject: [PATCH 07/10] style: formatting code --- Client.Test/InfluxDbClientFactoryTest.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Client.Test/InfluxDbClientFactoryTest.cs b/Client.Test/InfluxDbClientFactoryTest.cs index 4b18541cf..c10dc1d1a 100644 --- a/Client.Test/InfluxDbClientFactoryTest.cs +++ b/Client.Test/InfluxDbClientFactoryTest.cs @@ -569,13 +569,14 @@ public void InjectHttpClient() return new InfluxDBClient(options); }); var builder = services.BuildServiceProvider(); - + _client = builder.GetRequiredService(); var restClient = GetDeclaredField(_client.GetType(), _client, "_apiClient").RestClient; - Assert.AreEqual(options.HttpClient, - GetDeclaredField(restClient.GetType(), restClient, "k__BackingField")); + var httpClient = GetDeclaredField(restClient.GetType(), restClient, "k__BackingField"); + + Assert.AreEqual(options.HttpClient, httpClient); } private static T GetDeclaredField(IReflect type, object instance, string fieldName) From 3809d31752260792ddc79f3bbe087e0c1f64cfe9 Mon Sep 17 00:00:00 2001 From: tangbao <1427522930@qq.com> Date: Thu, 1 Jun 2023 11:14:15 +0800 Subject: [PATCH 08/10] style: formatting code --- Client.Test/InfluxDbClientFactoryTest.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Client.Test/InfluxDbClientFactoryTest.cs b/Client.Test/InfluxDbClientFactoryTest.cs index c10dc1d1a..557135157 100644 --- a/Client.Test/InfluxDbClientFactoryTest.cs +++ b/Client.Test/InfluxDbClientFactoryTest.cs @@ -574,7 +574,8 @@ public void InjectHttpClient() var restClient = GetDeclaredField(_client.GetType(), _client, "_apiClient").RestClient; - var httpClient = GetDeclaredField(restClient.GetType(), restClient, "k__BackingField"); + var httpClient = + GetDeclaredField(restClient.GetType(), restClient, "k__BackingField"); Assert.AreEqual(options.HttpClient, httpClient); } From d253cc7cd242ce2001925062fb18a74dea4bac3e Mon Sep 17 00:00:00 2001 From: tangbao <1427522930@qq.com> Date: Thu, 1 Jun 2023 11:31:00 +0800 Subject: [PATCH 09/10] style: formatting code --- Client.Test/InfluxDbClientFactoryTest.cs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Client.Test/InfluxDbClientFactoryTest.cs b/Client.Test/InfluxDbClientFactoryTest.cs index 557135157..3bb3dfcef 100644 --- a/Client.Test/InfluxDbClientFactoryTest.cs +++ b/Client.Test/InfluxDbClientFactoryTest.cs @@ -556,28 +556,25 @@ public void InjectHttpClient() { Token = "my-token" }; + var services = new ServiceCollection(); services.AddHttpClient(); - services.AddTransient(p => { var httpClientFactory = p.GetService(); - options.HttpClient = httpClientFactory.CreateClient(); - return new InfluxDBClient(options); }); + var builder = services.BuildServiceProvider(); _client = builder.GetRequiredService(); var restClient = GetDeclaredField(_client.GetType(), _client, "_apiClient").RestClient; - var httpClient = - GetDeclaredField(restClient.GetType(), restClient, "k__BackingField"); - - Assert.AreEqual(options.HttpClient, httpClient); + Assert.AreEqual(options.HttpClient, + GetDeclaredField(restClient.GetType(), restClient, "k__BackingField")); } private static T GetDeclaredField(IReflect type, object instance, string fieldName) From 38cca5aca54fca7974f919225c9c1dc879e55610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bedn=C3=A1=C5=99?= Date: Mon, 5 Jun 2023 08:06:54 +0200 Subject: [PATCH 10/10] docs: Update CHANGELOG.md --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c4895e4d..9d5e5b3a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ +## 4.13.0 [unreleased] + ### Features 1. [#528](https://github.com/influxdata/influxdb-client-csharp/pull/528): Add HttpClient as a part of InfluxDBClientOptions -## 4.13.0 [unreleased] - ### Dependencies Update dependencies: pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy