Skip to content

Commit 922c8bd

Browse files
alespourbednar
andauthored
feat: add HTTP proxy and custom headers support (#34)
* feat: add HTTP proxy and custom headers support * fix: uniform client API (#35) Co-authored-by: Jakub Bednář <[email protected]>
1 parent 360f84c commit 922c8bd

File tree

15 files changed

+320
-208
lines changed

15 files changed

+320
-208
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
### Features
44

5-
1. [#32](https://github.com/InfluxCommunity/influxdb3-csharp/pull/33): Add GZIP support
5+
1. [#33](https://github.com/InfluxCommunity/influxdb3-csharp/pull/33): Add GZIP support
6+
1. [#34](https://github.com/InfluxCommunity/influxdb3-csharp/pull/34): Add HTTP proxy and custom HTTP headers support
7+
8+
### Breaking Changes
9+
10+
1. [#35](https://github.com/InfluxCommunity/influxdb3-csharp/pull/35): Renamed config types and some options
611

712
## 0.1.0 [2023-06-09]
813

Client.Test.Integration/QueryWriteTest.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public class QueryWriteTest
1616
{
1717
private static readonly TraceListener ConsoleOutListener = new TextWriterTraceListener(Console.Out);
1818

19-
private readonly string _hostUrl = Environment.GetEnvironmentVariable("TESTING_INFLUXDB_URL") ??
19+
private readonly string _host = Environment.GetEnvironmentVariable("TESTING_INFLUXDB_URL") ??
2020
throw new InvalidOperationException("TESTING_INFLUXDB_URL environment variable is not set.");
21-
private readonly string _authToken = Environment.GetEnvironmentVariable("TESTING_INFLUXDB_TOKEN") ??
21+
private readonly string _token = Environment.GetEnvironmentVariable("TESTING_INFLUXDB_TOKEN") ??
2222
throw new InvalidOperationException("TESTING_INFLUXDB_TOKEN environment variable is not set.");
2323
private readonly string _database = Environment.GetEnvironmentVariable("TESTING_INFLUXDB_DATABASE") ??
2424
throw new InvalidOperationException("TESTING_INFLUXDB_DATABASE environment variable is not set.");
@@ -36,11 +36,11 @@ public void OneTimeSetUp()
3636
[Test]
3737
public async Task QueryWrite()
3838
{
39-
using var client = new InfluxDBClient(new InfluxDBClientConfigs
39+
using var client = new InfluxDBClient(new ClientConfig
4040
{
41-
HostUrl = _hostUrl,
42-
Database = _database,
43-
AuthToken = _authToken
41+
Host = _host,
42+
Token = _token,
43+
Database = _database
4444
});
4545

4646
const string measurement = "integration_test";
@@ -65,10 +65,10 @@ public async Task QueryWrite()
6565
[Test]
6666
public void QueryNotAuthorized()
6767
{
68-
using var client = new InfluxDBClient(new InfluxDBClientConfigs
68+
using var client = new InfluxDBClient(new ClientConfig
6969
{
70-
HostUrl = _hostUrl,
71-
Database = _database,
70+
Host = _host,
71+
Database = _database
7272
});
7373

7474
var ae = Assert.ThrowsAsync<RpcException>(async () =>
@@ -85,11 +85,11 @@ public void QueryNotAuthorized()
8585
[Test]
8686
public async Task WriteDontFailForEmptyData()
8787
{
88-
using var client = new InfluxDBClient(new InfluxDBClientConfigs
88+
using var client = new InfluxDBClient(new ClientConfig
8989
{
90-
HostUrl = _hostUrl,
90+
Host = _host,
9191
Database = _database,
92-
AuthToken = _authToken
92+
Token = _token
9393
});
9494

9595
await client.WritePointAsync(PointData.Measurement("cpu").AddTag("tag", "c"));
@@ -98,11 +98,11 @@ public async Task WriteDontFailForEmptyData()
9898
[Test]
9999
public async Task CanDisableCertificateValidation()
100100
{
101-
using var client = new InfluxDBClient(new InfluxDBClientConfigs
101+
using var client = new InfluxDBClient(new ClientConfig
102102
{
103-
HostUrl = _hostUrl,
103+
Host = _host,
104104
Database = _database,
105-
AuthToken = _authToken,
105+
Token = _token,
106106
DisableServerCertificateValidation = true
107107
});
108108

@@ -113,11 +113,11 @@ public async Task CanDisableCertificateValidation()
113113
[Test]
114114
public async Task WriteDataGzipped()
115115
{
116-
using var client = new InfluxDBClient(new InfluxDBClientConfigs
116+
using var client = new InfluxDBClient(new ClientConfig
117117
{
118-
HostUrl = _hostUrl,
118+
Host = _host,
119119
Database = _database,
120-
AuthToken = _authToken,
120+
Token = _token,
121121
WriteOptions = new WriteOptions
122122
{
123123
GzipThreshold = 1

Client.Test/InfluxDBClientQueryTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public void NotSpecifiedDatabase()
3535
var ae = Assert.Throws<InvalidOperationException>(() => { _client.QueryBatches("SELECT 1"); });
3636

3737
Assert.That(ae, Is.Not.Null);
38-
Assert.That(ae.Message, Is.EqualTo("Please specify the 'database' as a method parameter or use default configuration at 'InfluxDBClientConfigs.Database'."));
38+
Assert.That(ae.Message, Is.EqualTo("Please specify the 'database' as a method parameter or use default configuration at 'ClientConfig.Database'."));
3939
}
4040
}

Client.Test/InfluxDBClientTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public void Create()
1818
[Test]
1919
public void RequiredHost()
2020
{
21-
var ae = Assert.Throws<ArgumentException>(() => { new InfluxDBClient(hostUrl: null); });
21+
var ae = Assert.Throws<ArgumentException>(() => { new InfluxDBClient(host: null); });
2222

2323
Assert.That(ae, Is.Not.Null);
24-
Assert.That(ae.Message, Is.EqualTo("The hostname or IP address of the InfluxDB server has to be defined."));
24+
Assert.That(ae.Message, Is.EqualTo("The URL of the InfluxDB server has to be defined."));
2525
}
2626

2727
[Test]

Client.Test/InfluxDBClientWriteTest.cs

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading.Tasks;
45
using InfluxDB3.Client.Config;
@@ -82,9 +83,9 @@ public async Task BodyNonDefaultGzipped()
8283
.Given(Request.Create().WithPath("/api/v2/write").WithHeader("Content-Encoding", "gzip").UsingPost())
8384
.RespondWith(Response.Create().WithStatusCode(204));
8485

85-
_client = new InfluxDBClient(new InfluxDBClientConfigs
86+
_client = new InfluxDBClient(new ClientConfig
8687
{
87-
HostUrl = MockServerUrl,
88+
Host = MockServerUrl,
8889
Organization = "org",
8990
Database = "database",
9091
WriteOptions = new WriteOptions
@@ -126,20 +127,6 @@ public void AlreadyDisposed()
126127
Assert.That(ae.Message, Is.EqualTo("Cannot access a disposed object.\nObject name: 'InfluxDBClient'."));
127128
}
128129

129-
[Test]
130-
public async Task OrgCustom()
131-
{
132-
_client = new InfluxDBClient(MockServerUrl, organization: "org", database: "database");
133-
MockServer
134-
.Given(Request.Create().WithPath("/api/v2/write").UsingPost())
135-
.RespondWith(Response.Create().WithStatusCode(204));
136-
137-
await _client.WriteRecordAsync("mem,tag=a field=1", organization: "my-org");
138-
139-
var requests = MockServer.LogEntries.ToList();
140-
Assert.That(requests[0].RequestMessage.Query?["org"].First(), Is.EqualTo("my-org"));
141-
}
142-
143130
[Test]
144131
public async Task NotSpecifiedOrg()
145132
{
@@ -180,7 +167,7 @@ public void NotSpecifiedDatabase()
180167
Assert.That(ae, Is.Not.Null);
181168
Assert.That(ae.Message,
182169
Is.EqualTo(
183-
"Please specify the 'database' as a method parameter or use default configuration at 'InfluxDBClientConfigs.Database'."));
170+
"Please specify the 'database' as a method parameter or use default configuration at 'ClientConfig.Database'."));
184171
}
185172

186173
[Test]
@@ -200,9 +187,9 @@ public async Task PrecisionDefault()
200187
[Test]
201188
public async Task PrecisionOptions()
202189
{
203-
_client = new InfluxDBClient(new InfluxDBClientConfigs
190+
_client = new InfluxDBClient(new ClientConfig
204191
{
205-
HostUrl = MockServerUrl,
192+
Host = MockServerUrl,
206193
Organization = "org",
207194
Database = "database",
208195
WriteOptions = new WriteOptions
@@ -253,6 +240,63 @@ public async Task PrecisionBody()
253240
Assert.That(requests[0].RequestMessage.BodyData?.BodyAsString, Is.EqualTo("h2o,location=europe level=2i 123"));
254241
}
255242

243+
[Test]
244+
public async Task Proxy()
245+
{
246+
_client = new InfluxDBClient(new ClientConfig
247+
{
248+
Host = MockServerUrl,
249+
Organization = "org",
250+
Database = "database",
251+
Proxy = new System.Net.WebProxy
252+
{
253+
Address = new Uri(MockProxyUrl),
254+
BypassProxyOnLocal = false
255+
}
256+
});
257+
MockProxy
258+
.Given(Request.Create().WithPath("/api/v2/write").UsingPost())
259+
.RespondWith(Response.Create().WithStatusCode(204));
260+
261+
var point = PointData.Measurement("h2o")
262+
.AddTag("location", "europe")
263+
.AddField("level", 2)
264+
.SetTimestamp(123_000_000_000L);
265+
266+
await _client.WritePointAsync(point);
267+
268+
var requests = MockProxy.LogEntries.ToList();
269+
Assert.That(requests[0].RequestMessage.BodyData?.BodyAsString, Is.EqualTo("h2o,location=europe level=2i 123000000000"));
270+
}
271+
272+
[Test]
273+
public async Task CustomHeader()
274+
{
275+
_client = new InfluxDBClient(new ClientConfig
276+
{
277+
Host = MockServerUrl,
278+
Organization = "org",
279+
Database = "database",
280+
Headers = new Dictionary<string, string>
281+
{
282+
{ "X-device", "ab-01" },
283+
}
284+
});
285+
MockServer
286+
.Given(Request.Create().WithPath("/api/v2/write").WithHeader("X-device", "ab-01").UsingPost())
287+
.RespondWith(Response.Create().WithStatusCode(204));
288+
289+
var point = PointData.Measurement("h2o")
290+
.AddTag("location", "europe")
291+
.AddField("level", 2)
292+
.SetTimestamp(123_000_000_000L);
293+
294+
await _client.WritePointAsync(point);
295+
296+
var requests = MockServer.LogEntries.ToList();
297+
Assert.That(requests[0].RequestMessage.BodyData?.BodyAsString, Is.EqualTo("h2o,location=europe level=2i 123000000000"));
298+
}
299+
256300
private async Task WriteData()
257301
{
258302
MockServer

Client.Test/Internal/RestClientTest.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ public class RestClientTest : MockServerTest
2424
[Test]
2525
public async Task Authorization()
2626
{
27-
CreateAndConfigureRestClient(new InfluxDBClientConfigs
27+
CreateAndConfigureRestClient(new ClientConfig
2828
{
29-
HostUrl = MockServerUrl,
30-
AuthToken = "my-token"
29+
Host = MockServerUrl,
30+
Token = "my-token"
3131
});
3232
await DoRequest();
3333

@@ -39,9 +39,9 @@ public async Task Authorization()
3939
[Test]
4040
public async Task UserAgent()
4141
{
42-
CreateAndConfigureRestClient(new InfluxDBClientConfigs
42+
CreateAndConfigureRestClient(new ClientConfig
4343
{
44-
HostUrl = MockServerUrl,
44+
Host = MockServerUrl,
4545
});
4646
await DoRequest();
4747

@@ -54,9 +54,9 @@ public async Task UserAgent()
5454
[Test]
5555
public async Task Url()
5656
{
57-
CreateAndConfigureRestClient(new InfluxDBClientConfigs
57+
CreateAndConfigureRestClient(new ClientConfig
5858
{
59-
HostUrl = MockServerUrl,
59+
Host = MockServerUrl,
6060
});
6161
await DoRequest();
6262

@@ -67,9 +67,9 @@ public async Task Url()
6767
[Test]
6868
public async Task UrlWithBackslash()
6969
{
70-
CreateAndConfigureRestClient(new InfluxDBClientConfigs
70+
CreateAndConfigureRestClient(new ClientConfig
7171
{
72-
HostUrl = $"{MockServerUrl}/",
72+
Host = $"{MockServerUrl}/",
7373
});
7474
await DoRequest();
7575

@@ -89,9 +89,9 @@ private async Task DoRequest()
8989
[Test]
9090
public void ErrorHeader()
9191
{
92-
CreateAndConfigureRestClient(new InfluxDBClientConfigs
92+
CreateAndConfigureRestClient(new ClientConfig
9393
{
94-
HostUrl = MockServerUrl,
94+
Host = MockServerUrl,
9595
});
9696

9797
MockServer
@@ -116,9 +116,9 @@ public void ErrorHeader()
116116
[Test]
117117
public void ErrorBody()
118118
{
119-
CreateAndConfigureRestClient(new InfluxDBClientConfigs
119+
CreateAndConfigureRestClient(new ClientConfig
120120
{
121-
HostUrl = MockServerUrl,
121+
Host = MockServerUrl,
122122
});
123123

124124
MockServer
@@ -143,9 +143,9 @@ public void ErrorBody()
143143
[Test]
144144
public void ErrorJsonBody()
145145
{
146-
CreateAndConfigureRestClient(new InfluxDBClientConfigs
146+
CreateAndConfigureRestClient(new ClientConfig
147147
{
148-
HostUrl = MockServerUrl,
148+
Host = MockServerUrl,
149149
});
150150

151151
MockServer
@@ -171,9 +171,9 @@ public void ErrorJsonBody()
171171
[Test]
172172
public void ErrorReason()
173173
{
174-
CreateAndConfigureRestClient(new InfluxDBClientConfigs
174+
CreateAndConfigureRestClient(new ClientConfig
175175
{
176-
HostUrl = MockServerUrl,
176+
Host = MockServerUrl,
177177
});
178178

179179
MockServer
@@ -197,9 +197,9 @@ public void ErrorReason()
197197
[Test]
198198
public void AllowHttpRedirects()
199199
{
200-
CreateAndConfigureRestClient(new InfluxDBClientConfigs
200+
CreateAndConfigureRestClient(new ClientConfig
201201
{
202-
HostUrl = MockServerUrl,
202+
Host = MockServerUrl,
203203
AllowHttpRedirects = true
204204
});
205205

@@ -209,20 +209,20 @@ public void AllowHttpRedirects()
209209
[Test]
210210
public void Timeout()
211211
{
212-
CreateAndConfigureRestClient(new InfluxDBClientConfigs
212+
CreateAndConfigureRestClient(new ClientConfig
213213
{
214-
HostUrl = MockServerUrl,
214+
Host = MockServerUrl,
215215
Timeout = TimeSpan.FromSeconds(45)
216216
});
217217

218218
var httpClient = GetDeclaredField<HttpClient>(_client.GetType(), _client, "_httpClient");
219219
Assert.That(httpClient.Timeout, Is.EqualTo(TimeSpan.FromSeconds(45)));
220220
}
221221

222-
private void CreateAndConfigureRestClient(InfluxDBClientConfigs configs)
222+
private void CreateAndConfigureRestClient(ClientConfig config)
223223
{
224-
_httpClient = InfluxDBClient.CreateAndConfigureHttpClient(configs);
225-
_client = new RestClient(configs, _httpClient);
224+
_httpClient = InfluxDBClient.CreateAndConfigureHttpClient(config);
225+
_client = new RestClient(config, _httpClient);
226226
}
227227

228228
private static T GetDeclaredField<T>(IReflect type, object instance, string fieldName)

0 commit comments

Comments
 (0)