Skip to content

feat: add HttpClient as a part of InfluxDBClientOptions #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 6, 2023
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:

Expand Down
4 changes: 2 additions & 2 deletions Client.Test/ApiClientTest.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
31 changes: 31 additions & 0 deletions Client.Test/InfluxDbClientFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<IHttpClientFactory>();
options.HttpClient = httpClientFactory.CreateClient();
return new InfluxDBClient(options);
});

var builder = services.BuildServiceProvider();

_client = builder.GetRequiredService<InfluxDBClient>();

var restClient = GetDeclaredField<ApiClient>(_client.GetType(), _client, "_apiClient").RestClient;

Assert.AreEqual(options.HttpClient,
GetDeclaredField<HttpClient>(restClient.GetType(), restClient, "<HttpClient>k__BackingField"));
}

private static T GetDeclaredField<T>(IReflect type, object instance, string fieldName)
{
const BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
Expand Down
31 changes: 31 additions & 0 deletions Client/InfluxDBClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,6 +35,7 @@ public class InfluxDBClientOptions
private bool _allowHttpRedirects;
private bool _verifySsl;
private X509CertificateCollection _clientCertificates;
private HttpClient _httpClient;

/// <summary>
/// Set the url to connect the InfluxDB.
Expand Down Expand Up @@ -251,6 +253,15 @@ public void AddDefaultTags(Dictionary<string, string> tags)
}
}

/// <summary>
/// Add a HttpClient as a part of InfluxDBClientOptions
/// </summary>
public HttpClient HttpClient
{
get => _httpClient;
set => _httpClient = value;
}

/// <summary>
/// Create an instance of InfluxDBClientOptions. The url could be a connection string with various configurations.
///<para>
Expand Down Expand Up @@ -428,6 +439,11 @@ private InfluxDBClientOptions(Builder builder)
{
ClientCertificates = builder.CertificateCollection;
}

if (builder.HttpClient != null)
{
HttpClient = builder.HttpClient;
}
}

private static TimeSpan ToTimeout(string value)
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -828,6 +845,20 @@ public InfluxDBClientOptions Build()

return new InfluxDBClientOptions(this);
}

/// <summary>
/// Configure HttpClient
/// </summary>
/// <param name="httpClient"></param>
/// <returns></returns>
public Builder SetHttpClient(HttpClient httpClient)
{
Arguments.CheckNotNull(httpClient, nameof(httpClient));

HttpClient = httpClient;

return this;
}
}
}
}
5 changes: 4 additions & 1 deletion Client/Internal/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
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