diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f36711a7..9d5e5b3a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +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 + ### Dependencies Update dependencies: diff --git a/Client.Test/ApiClientTest.cs b/Client.Test/ApiClientTest.cs index 4b4ffbcce..6f2e22176 100644 --- a/Client.Test/ApiClientTest.cs +++ b/Client.Test/ApiClientTest.cs @@ -1,9 +1,9 @@ -using System; -using System.Net; using InfluxDB.Client.Api.Client; using InfluxDB.Client.Core; using InfluxDB.Client.Core.Internal; using NUnit.Framework; +using System; +using System.Net; namespace InfluxDB.Client.Test { diff --git a/Client.Test/InfluxDbClientFactoryTest.cs b/Client.Test/InfluxDbClientFactoryTest.cs index 21fdbc5d5..3bb3dfcef 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,34 @@ 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 => + { + 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; + + 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 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 8b9779a1b..5243d0107 100644 --- a/Client/Internal/ApiClient.cs +++ b/Client/Internal/ApiClient.cs @@ -60,7 +60,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, 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