Skip to content

Commit a29f9a7

Browse files
authored
feat: add User-Agent request header (influxdata#62)
* chore: fix tests (beta vs nightly)
1 parent b824b86 commit a29f9a7

File tree

10 files changed

+103
-4
lines changed

10 files changed

+103
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## 1.6.0 [unreleased]
22

3+
### Features
4+
1. [#61](https://github.com/influxdata/influxdb-client-csharp/issues/61): Set User-Agent to influxdb-client-csharp/VERSION for all requests
5+
36
## 1.5.0 [2020-02-14]
47

58
### Features
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Reflection;
3+
4+
namespace InfluxDB.Client.Core.Internal
5+
{
6+
public static class AssemblyHelper
7+
{
8+
public static string GetVersion(Type type)
9+
{
10+
try
11+
{
12+
return type.GetTypeInfo()
13+
.Assembly
14+
.GetCustomAttribute<AssemblyFileVersionAttribute>()
15+
.Version;
16+
}
17+
catch (Exception)
18+
{
19+
return "unknown";
20+
}
21+
}
22+
}
23+
}

Client.Legacy.Test/FluxClientQueryTest.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using System.Threading;
34
using System.Threading.Tasks;
45
using InfluxDB.Client.Core;
@@ -15,7 +16,6 @@ public class FluxClientQueryTest : AbstractFluxClientTest
1516
[Test]
1617
public async Task Query()
1718
{
18-
1919
MockServer.Given(Request.Create().WithPath("/api/v2/query").UsingPost())
2020
.RespondWith(CreateResponse());
2121

@@ -182,6 +182,19 @@ await FluxClient.QueryAsync("from(bucket:\"telegraf\")",
182182
WaitToCallback();
183183
}
184184

185+
[Test]
186+
public async Task UserAgentHeader()
187+
{
188+
MockServer.Given(Request.Create().WithPath("/api/v2/query").UsingPost())
189+
.RespondWith(CreateResponse());
190+
191+
await FluxClient.QueryAsync("from(bucket:\"telegraf\")");
192+
193+
var request= MockServer.LogEntries.Last();
194+
StringAssert.StartsWith("influxdb-client-csharp/1.", request.RequestMessage.Headers["User-Agent"].First());
195+
StringAssert.EndsWith(".0.0", request.RequestMessage.Headers["User-Agent"].First());
196+
}
197+
185198
private void AssertSuccessResult(List<FluxTable> tables)
186199
{
187200
Assert.IsNotNull(tables);

Client.Legacy/FluxClient.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ public FluxClient(FluxConnectionOptions options) : base(new RestClient())
2020
{
2121
_loggingHandler = new LoggingHandler(LogLevel.None);
2222

23+
var version = AssemblyHelper.GetVersion(typeof(FluxClient));
24+
2325
RestClient.BaseUrl = new Uri(options.Url);
2426
RestClient.Timeout = options.Timeout.Milliseconds;
2527
RestClient.AddDefaultHeader("Accept", "application/json");
28+
RestClient.UserAgent = $"influxdb-client-csharp/{version}";
2629
}
2730

2831
/// <summary>

Client.Test/AssemblyHelperTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using InfluxDB.Client.Core.Internal;
3+
using NUnit.Framework;
4+
5+
namespace InfluxDB.Client.Test
6+
{
7+
[TestFixture]
8+
public class AssemblyHelperTest
9+
{
10+
[Test]
11+
public void GetAssemblyVersion()
12+
{
13+
var version = AssemblyHelper.GetVersion(typeof(InfluxDBClient));
14+
Assert.AreEqual(1, Version.Parse(version).Major);
15+
Assert.Greater(Version.Parse(version).Minor, 5);
16+
Assert.AreEqual(0, Version.Parse(version).Build);
17+
Assert.AreEqual(0, Version.Parse(version).Revision);
18+
}
19+
}
20+
}

Client.Test/InfluxDbClientTest.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System.Diagnostics;
22
using System.IO;
3+
using System.Linq;
4+
using System.Threading.Tasks;
35
using InfluxDB.Client.Api.Client;
46
using InfluxDB.Client.Api.Domain;
57
using InfluxDB.Client.Core;
68
using InfluxDB.Client.Core.Test;
79
using NUnit.Framework;
810
using WireMock.RequestBuilders;
9-
using Task = System.Threading.Tasks.Task;
1011

1112
namespace InfluxDB.Client.Test
1213
{
@@ -139,5 +140,19 @@ public void LogLevelWithoutQueryString()
139140
StringAssert.DoesNotContain("bucket=b1", writer.ToString());
140141
StringAssert.DoesNotContain("precision=ns", writer.ToString());
141142
}
143+
144+
[Test]
145+
public async Task UserAgentHeader()
146+
{
147+
MockServer
148+
.Given(Request.Create().UsingGet())
149+
.RespondWith(CreateResponse("{\"status\":\"active\"}", "application/json"));
150+
151+
await _client.GetAuthorizationsApi().FindAuthorizationByIdAsync("id");
152+
153+
var request= MockServer.LogEntries.Last();
154+
StringAssert.StartsWith("influxdb-client-csharp/1.", request.RequestMessage.Headers["User-Agent"].First());
155+
StringAssert.EndsWith(".0.0", request.RequestMessage.Headers["User-Agent"].First());
156+
}
142157
}
143158
}

Client.Test/ItChecksApiTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace InfluxDB.Client.Test
1111
{
1212
[TestFixture]
13-
[Ignore("https://github.com/influxdata/influxdb/issues/16462")]
1413
public class ItChecksApiTest : AbstractItClientTest
1514
{
1615
private string _orgId;

Client.Test/ItTasksApiTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,13 @@ public async Task CancelRunNotExist()
8484
}
8585

8686
[Test]
87+
[Ignore("TODO uncomment after beta")]
8788
public void CancelRunTaskNotExist()
8889
{
8990
var message = Assert.ThrowsAsync<HttpException>(async () =>
9091
await _tasksApi.CancelRunAsync("020f755c3c082000", "020f755c3c082000")).Message;
9192

92-
Assert.AreEqual("failed to cancel run: run not found", message);
93+
Assert.AreEqual("failed to cancel run: task not found", message);
9394
}
9495

9596
[Test]

Client.Test/WriteApiTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,5 +313,24 @@ public void WaitToCondition()
313313
StringAssert.Contains("The WriteApi can't be gracefully dispose! - 1ms", writer.ToString());
314314
StringAssert.DoesNotContain("The WriteApi can't be gracefully dispose! - 30000ms", writer.ToString());
315315
}
316+
317+
[Test]
318+
public void UserAgentHeader()
319+
{
320+
var listener = new EventListener(_writeApi);
321+
322+
MockServer
323+
.Given(Request.Create().WithPath("/api/v2/write").UsingPost())
324+
.RespondWith(CreateResponse("{}"));
325+
326+
_writeApi.WriteRecord("b1", "org1", WritePrecision.Ns,
327+
"h2o_feet,location=coyote_creek level\\ description=\"feet 1\",water_level=1.0 1");
328+
329+
listener.Get<WriteSuccessEvent>();
330+
331+
var request= MockServer.LogEntries.Last();
332+
StringAssert.StartsWith("influxdb-client-csharp/1.", request.RequestMessage.Headers["User-Agent"].First());
333+
StringAssert.EndsWith(".0.0", request.RequestMessage.Headers["User-Agent"].First());
334+
}
316335
}
317336
}

Client/InfluxDBClient.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ protected internal InfluxDBClient(InfluxDBClientOptions options)
3636
_loggingHandler = new LoggingHandler(options.LogLevel);
3737
_gzipHandler = new GzipHandler();
3838

39+
var version = AssemblyHelper.GetVersion(typeof(InfluxDBClient));
40+
3941
_apiClient = new ApiClient(options, _loggingHandler, _gzipHandler);
42+
_apiClient.RestClient.UserAgent = $"influxdb-client-csharp/{version}";
4043

4144
_exceptionFactory = (methodName, response) =>
4245
!response.IsSuccessful ? HttpException.Create(response) : null;

0 commit comments

Comments
 (0)