Skip to content

Commit

Permalink
feat: Initialize C# client without InfluxDBClientFactory (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelahojna authored Nov 16, 2022
1 parent 5400a84 commit 9006c29
Show file tree
Hide file tree
Showing 60 changed files with 1,527 additions and 651 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## 4.8.0 [unreleased]

:warning: The client can be created without `InfluxDBClientFactory`:

using var client = new InfluxDBClient("http://localhost:8086", "my-token");

### Features
1. [#388](https://github.com/influxdata/influxdb-client-csharp/pull/388): Initialize C# client without `InfluxDBClientFactory`

### CI
1. [#416](https://github.com/influxdata/influxdb-client-csharp/pull/416): Add build for `.NET 7.0`

Expand Down
2 changes: 1 addition & 1 deletion Client.Legacy.Test/AbstractFluxClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class AbstractFluxClientTest : AbstractMockServerTest
[SetUp]
public new void SetUp()
{
FluxClient = FluxClientFactory.Create(MockServerUrl);
FluxClient = new FluxClient(MockServerUrl);
}

[TearDown]
Expand Down
2 changes: 1 addition & 1 deletion Client.Legacy.Test/AbstractItFluxClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private async Task SetUpAsync()

var options = new FluxConnectionOptions(influxUrl);

FluxClient = FluxClientFactory.Create(options);
FluxClient = new FluxClient(options);

await InfluxDbQuery("CREATE DATABASE " + DatabaseName, DatabaseName);
}
Expand Down
4 changes: 2 additions & 2 deletions Client.Legacy.Test/FluxClientPingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task NotRunningServer()
public async Task WithAuthentication()
{
FluxClient =
FluxClientFactory.Create(new FluxConnectionOptions(MockServerUrl, "my-user",
new FluxClient(new FluxConnectionOptions(MockServerUrl, "my-user",
"my-password".ToCharArray()));

MockServer.Given(Request.Create()
Expand All @@ -56,7 +56,7 @@ public async Task WithAuthentication()
[Test]
public async Task WithBasicAuthentication()
{
FluxClient = FluxClientFactory.Create(new FluxConnectionOptions(MockServerUrl, "my-user",
FluxClient = new FluxClient(new FluxConnectionOptions(MockServerUrl, "my-user",
"my-password".ToCharArray(), FluxConnectionOptions.AuthenticationType.BasicAuthentication));

var auth = System.Text.Encoding.UTF8.GetBytes("my-user:my-password");
Expand Down
4 changes: 2 additions & 2 deletions Client.Legacy.Test/FluxClientQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public async Task UserAgentHeader()
public async Task WithAuthentication()
{
FluxClient =
FluxClientFactory.Create(new FluxConnectionOptions(MockServerUrl, "my-user",
new FluxClient(new FluxConnectionOptions(MockServerUrl, "my-user",
"my-password".ToCharArray()));

MockServer.Given(Request.Create()
Expand All @@ -248,7 +248,7 @@ public async Task WithAuthentication()
[Test]
public async Task WithBasicAuthentication()
{
FluxClient = FluxClientFactory.Create(new FluxConnectionOptions(MockServerUrl, "my-user",
FluxClient = new FluxClient(new FluxConnectionOptions(MockServerUrl, "my-user",
"my-password".ToCharArray(), FluxConnectionOptions.AuthenticationType.BasicAuthentication));

var auth = System.Text.Encoding.UTF8.GetBytes("my-user:my-password");
Expand Down
6 changes: 3 additions & 3 deletions Client.Legacy.Test/FluxClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class FluxClientTest
[SetUp]
public void SetUp()
{
_fluxClient = FluxClientFactory.Create("http://localhost:8093");
_fluxClient = new FluxClient("http://localhost:8093");
}

[Test]
Expand All @@ -42,9 +42,9 @@ public void ProxyDefaultConfigured()
TimeSpan.FromSeconds(60),
webProxy: webProxy);

var fluxClient = FluxClientFactory.Create(options);
var client = new FluxClient(options);

Assert.AreEqual(webProxy, GetRestClient(fluxClient).Options.Proxy);
Assert.AreEqual(webProxy, GetRestClient(client).Options.Proxy);
}

[Test]
Expand Down
4 changes: 2 additions & 2 deletions Client.Legacy.Test/ItFluxClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ public async Task CallbackWhenConnectionRefuse()
{
var options = new FluxConnectionOptions("http://localhost:8003");

var fluxClient = FluxClientFactory.Create(options);
var client = new FluxClient(options);

await fluxClient.QueryAsync(FromFluxDatabase + " |> last()",
await client.QueryAsync(FromFluxDatabase + " |> last()",
record => { },
error => CountdownEvent.Signal());

Expand Down
32 changes: 32 additions & 0 deletions Client.Legacy/FluxClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,38 @@ public class FluxClient : AbstractQueryClient, IFluxClient
{
private readonly LoggingHandler _loggingHandler;

/// <summary>
/// Create a instance of the Flux client. The url could be a connection string with various configurations.
/// <para>
/// e.g.: "http://localhost:8086?timeout=5000&amp;logLevel=BASIC
/// The following options are supported:
/// <list type="bullet">
/// <item>org - default destination organization for writes and queries</item>
/// <item>bucket - default destination bucket for writes</item>
/// <item>token - the token to use for the authorization</item>
/// <item>logLevel (default - NONE) - rest client verbosity level</item>
/// <item>timeout (default - 10000) - The timespan to wait before the HTTP request times out in milliseconds</item>
/// <item>allowHttpRedirects (default - false) - Configure automatically following HTTP 3xx redirects</item>
/// <item>verifySsl (default - true) - Ignore Certificate Validation Errors when false</item>
/// </list>
/// Options for logLevel:
/// <list type="bullet">
/// <item>Basic - Logs request and response lines.</item>
/// <item>Body - Logs request and response lines including headers and body (if present). Note that applying the `Body` LogLevel will disable chunking while streaming and will load the whole response into memory.</item>
/// <item>Headers - Logs request and response lines including headers.</item>
/// <item>None - Disable logging.</item>
/// </list>
/// </para>
/// </summary>
/// <param name="connectionString">the connectionString to connect to InfluxDB</param>
public FluxClient(string connectionString) : this(new FluxConnectionOptions(connectionString))
{
}

/// <summary>
/// Create a instance of the Flux client.
/// </summary>
/// <param name="options">the connection configuration</param>
public FluxClient(FluxConnectionOptions options) : base(new FluxResultMapper())
{
_loggingHandler = new LoggingHandler(LogLevel.None);
Expand Down
6 changes: 6 additions & 0 deletions Client.Legacy/FluxClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace InfluxDB.Client.Flux
{
/// <summary>
Expand All @@ -10,6 +12,8 @@ public class FluxClientFactory
/// </summary>
/// <param name="connectionString">the connectionString to connect to InfluxDB</param>
/// <returns>client</returns>
/// <remarks>Deprecated - please use use object initializer <see cref="FluxClient(string)"/></remarks>
[Obsolete("This method is deprecated. Call 'FluxClient' initializer instead.", false)]
public static FluxClient Create(string connectionString)
{
var options = new FluxConnectionOptions(connectionString);
Expand All @@ -22,6 +26,8 @@ public static FluxClient Create(string connectionString)
/// </summary>
/// <param name="options">the connection configuration</param>
/// <returns></returns>
/// <remarks>Deprecated - please use use object initializer <see cref="FluxClient(FluxConnectionOptions)"/></remarks>
[Obsolete("This method is deprecated. Call 'FluxClient' initializer instead.", false)]
public static FluxClient Create(FluxConnectionOptions options)
{
return new FluxClient(options);
Expand Down
14 changes: 7 additions & 7 deletions Client.Legacy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ The `FluxClientFactory` creates an instance of a `FluxClient` client that can be
// client creation
var options = new FluxConnectionOptions("http://127.0.0.1:8086");

using var fluxClient = FluxClientFactory.Create(options);
using var client = new FluxClient(options);

fluxClient.QueryAsync(...)
client.QueryAsync(...)
...
```
#### Authenticate requests
Expand All @@ -43,9 +43,9 @@ fluxClient.QueryAsync(...)
// client creation
var options = new FluxConnectionOptions("http://127.0.0.1:8086", "my-user", "my-password".ToCharArray());

using var fluxClient = FluxClientFactory.Create(options);
using var client = new FluxClient(options);

fluxClient.QueryAsync(...)
client.QueryAsync(...)
...
```

Expand All @@ -55,9 +55,9 @@ fluxClient.QueryAsync(...)
var options = new FluxConnectionOptions("http://127.0.0.1:8086", "my-user", "my-password".ToCharArray(),
FluxConnectionOptions.AuthenticationType.BasicAuthentication);

using var fluxClient = FluxClientFactory.Create(options);
using var client = new FluxClient(options);

fluxClient.QueryAsync(...)
client.QueryAsync(...)
...
```

Expand Down Expand Up @@ -123,7 +123,7 @@ The Requests and Responses can be logged by changing the LogLevel. LogLevel valu
applying the `Body` LogLevel will disable chunking while streaming and will load the whole response into memory.

```c#
fluxClient.SetLogLevel(LogLevel.Body)
client.SetLogLevel(LogLevel.Body)
```

## Version
Expand Down
4 changes: 2 additions & 2 deletions Client.Linq.Test/InfluxDBQueryVariableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public class InfluxDbQueryVariableTest : AbstractTest
[SetUp]
public new Task SetUp()
{
_client = InfluxDBClientFactory.Create(GetInfluxDb2Url(), "my-token");
_client = new InfluxDBClient(GetInfluxDb2Url(), "my-token");
_client.SetLogLevel(LogLevel.Body);
return Task.CompletedTask;
}

[OneTimeSetUp]
public async Task OneTimeSetUp()
{
_client = InfluxDBClientFactory.Create(GetInfluxDb2Url(), "my-token");
_client = new InfluxDBClient(GetInfluxDb2Url(), "my-token");
_client.SetLogLevel(LogLevel.Body);

_dateTime = DateTime.UtcNow;
Expand Down
5 changes: 1 addition & 4 deletions Client.Linq.Test/InfluxDBQueryVisitorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ public class InfluxDBQueryVisitorTest : AbstractTest
[OneTimeSetUp]
public void InitQueryApi()
{
var options = new InfluxDBClientOptions.Builder()
.Url("http://localhost:8086")
.AuthenticateToken("my-token")
.Build();
var options = new InfluxDBClientOptions("http://localhost:8086") { Token = "my-token" };
var queryService = new Mock<QueryService>("http://localhost:8086/api/v2");
_queryApi = new Mock<QueryApiSync>(options, queryService.Object, new FluxResultMapper()).Object;
}
Expand Down
2 changes: 1 addition & 1 deletion Client.Linq.Test/ItInfluxDBQueryableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ItInfluxDBQueryableTest : AbstractTest
[SetUp]
public new async Task SetUp()
{
_client = InfluxDBClientFactory.Create(GetInfluxDb2Url(), "my-token");
_client = new InfluxDBClient(GetInfluxDb2Url(), "my-token");
_client.SetLogLevel(LogLevel.Body);

// DateTime(2020, 10, 15, 8, 20, 15, DateTimeKind.Utc)
Expand Down
4 changes: 2 additions & 2 deletions Client.Linq/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ using InfluxDB.Client.Linq;
The LINQ query depends on `QueryApiSync`, you could create an instance of `QueryApiSync` by:

```c#
var client = InfluxDBClientFactory.Create("http://localhost:8086", "my-token");
var client = new InfluxDBClient("http://localhost:8086", "my-token");
var queryApi = client.GetQueryApiSync();
```

Expand Down Expand Up @@ -1170,7 +1170,7 @@ private class InfluxPoint
The LINQ driver also supports asynchronous querying. For asynchronous queries you have to initialize `InfluxDBQueryable` with asynchronous version of [QueryApi](/Client/QueryApi.cs) and transform `IQueryable<T>` to `IAsyncEnumerable<T>`:

```c#
var client = InfluxDBClientFactory.Create("http://localhost:8086", "my-token");
var client = new InfluxDBClient("http://localhost:8086", "my-token");
var queryApi = client.GetQueryApi();

var query = from s in InfluxDBQueryable<Sensor>.Queryable("my-bucket", "my-org", queryApi)
Expand Down
4 changes: 2 additions & 2 deletions Client.Test/AbstractItClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class AbstractItClientTest : AbstractTest

if (!TestContext.CurrentContext.Test.Properties.ContainsKey("basic_auth"))
{
Client = InfluxDBClientFactory.Create(InfluxDbUrl, "my-token");
Client = new InfluxDBClient(InfluxDbUrl, "my-token");
}
else
{
Client = InfluxDBClientFactory.Create(InfluxDbUrl, "my-user", "my-password".ToCharArray());
Client = new InfluxDBClient(InfluxDbUrl, "my-user", "my-password");
}
}

Expand Down
18 changes: 9 additions & 9 deletions Client.Test/ApiClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public class ApiClientTest
[SetUp]
public void SetUp()
{
var options = new InfluxDBClientOptions.Builder()
.Url("http://localhost:8086")
.AuthenticateToken("my-token".ToCharArray())
.Build();
var options = new InfluxDBClientOptions("http://localhost:8086")
{
Token = "my-token"
};

_apiClient = new ApiClient(options, new LoggingHandler(LogLevel.Body), new GzipHandler());
}
Expand Down Expand Up @@ -51,11 +51,11 @@ public void ProxyDefaultConfigured()
{
var webProxy = new WebProxy("my-proxy", 8088);

var options = new InfluxDBClientOptions.Builder()
.Url("http://localhost:8086")
.AuthenticateToken("my-token".ToCharArray())
.Proxy(webProxy)
.Build();
var options = new InfluxDBClientOptions("http://localhost:8086")
{
Token = "my-token",
WebProxy = webProxy
};

_apiClient = new ApiClient(options, new LoggingHandler(LogLevel.Body), new GzipHandler());

Expand Down
2 changes: 1 addition & 1 deletion Client.Test/GzipHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class GzipHandlerTest : AbstractMockServerTest
[SetUp]
public new void SetUp()
{
_client = InfluxDBClientFactory.Create(MockServerUrl);
_client = new InfluxDBClient(MockServerUrl);
}

[Test]
Expand Down
Loading

0 comments on commit 9006c29

Please sign in to comment.