Skip to content

Commit

Permalink
feat: add HTTP proxy and custom headers support
Browse files Browse the repository at this point in the history
  • Loading branch information
alespour committed Jul 31, 2023
1 parent 360f84c commit a187489
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
58 changes: 58 additions & 0 deletions Client.Test/InfluxDBClientWriteTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using InfluxDB3.Client.Config;
Expand Down Expand Up @@ -253,6 +254,63 @@ public async Task PrecisionBody()
Assert.That(requests[0].RequestMessage.BodyData?.BodyAsString, Is.EqualTo("h2o,location=europe level=2i 123"));
}

[Test]
public async Task Proxy()
{
_client = new InfluxDBClient(new InfluxDBClientConfigs
{
HostUrl = MockServerUrl,
Organization = "org",
Database = "database",
Proxy = new System.Net.WebProxy
{
Address = new Uri(MockProxyUrl),
BypassProxyOnLocal = false
}
});
MockProxy
.Given(Request.Create().WithPath("/api/v2/write").UsingPost())
.RespondWith(Response.Create().WithStatusCode(204));

var point = PointData.Measurement("h2o")
.AddTag("location", "europe")
.AddField("level", 2)
.SetTimestamp(123_000_000_000L);

await _client.WritePointAsync(point);

var requests = MockProxy.LogEntries.ToList();
Assert.That(requests[0].RequestMessage.BodyData?.BodyAsString, Is.EqualTo("h2o,location=europe level=2i 123000000000"));
}

[Test]
public async Task CustomHeader()
{
_client = new InfluxDBClient(new InfluxDBClientConfigs
{
HostUrl = MockServerUrl,
Organization = "org",
Database = "database",
Headers = new List<KeyValuePair<String, String>>
{
new KeyValuePair<string, string>("X-device", "ab-01"),
}
});
MockServer
.Given(Request.Create().WithPath("/api/v2/write").WithHeader("X-device", "ab-01").UsingPost())
.RespondWith(Response.Create().WithStatusCode(204));

var point = PointData.Measurement("h2o")
.AddTag("location", "europe")
.AddField("level", 2)
.SetTimestamp(123_000_000_000L);

await _client.WritePointAsync(point);

var requests = MockServer.LogEntries.ToList();
Assert.That(requests[0].RequestMessage.BodyData?.BodyAsString, Is.EqualTo("h2o,location=europe level=2i 123000000000"));
}

private async Task WriteData()
{
MockServer
Expand Down
18 changes: 16 additions & 2 deletions Client.Test/MockServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace InfluxDB3.Client.Test;

public class MockServerTest
{
internal WireMockServer MockServer;
internal string MockServerUrl;
internal WireMockServer MockServer, MockProxy;
internal string MockServerUrl, MockProxyUrl;

[SetUp]
public void SetUp()
Expand All @@ -22,17 +22,31 @@ public void SetUp()
});

MockServerUrl = MockServer.Urls[0];

MockProxy = WireMockServer.Start(new WireMockServerSettings
{
UseSSL = false,
Port = 8888,
ProxyAndRecordSettings = new ProxyAndRecordSettings
{
Url = MockServerUrl
}
});

MockProxyUrl = MockProxy.Urls[0];
}

[TearDown]
public void TearDown()
{
MockServer.Reset();
MockProxy.Reset();
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
MockServer?.Stop();
MockProxy?.Stop();
}
}
12 changes: 12 additions & 0 deletions Client/Config/InfluxDBClientConfigs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net;
using InfluxDB3.Client.Write;

namespace InfluxDB3.Client.Config;
Expand Down Expand Up @@ -38,6 +40,11 @@ public string HostUrl
/// </summary>
public string? Database { get; set; }

/// <summary>
/// The set of HTTP headers to be included in requests.
/// </summary>
public List<KeyValuePair<String, String>>? Headers { get; set; }

/// <summary>
/// Timeout to wait before the HTTP request times out. Default to '10 seconds'.
/// </summary>
Expand All @@ -53,6 +60,11 @@ public string HostUrl
/// </summary>
public bool DisableServerCertificateValidation { get; set; }

/// <summary>
/// The web proxy for HTTP communication.
/// </summary>
public WebProxy? Proxy { get; set; }

/// <summary>
/// Write options.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions Client/InfluxDBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ internal static HttpClient CreateAndConfigureHttpClient(InfluxDBClientConfigs co
{
handler.AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate;
}
if (handler.SupportsProxy && configs.Proxy != null)
{
handler.Proxy = configs.Proxy;
}
if (configs.DisableServerCertificateValidation)
{
handler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
Expand All @@ -336,6 +340,13 @@ internal static HttpClient CreateAndConfigureHttpClient(InfluxDBClientConfigs co
{
Timeout = configs.Timeout
};
if (configs.Headers != null)
{
foreach (var header in configs.Headers)
{
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
client.DefaultRequestHeaders.UserAgent.ParseAdd($"influxdb3-csharp/{AssemblyHelper.GetVersion()}");
if (!string.IsNullOrEmpty(configs.AuthToken))
{
Expand Down

0 comments on commit a187489

Please sign in to comment.