From d2a1266dd22719a6cb043abe6cbcac9f7e2106f0 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Wed, 20 Apr 2022 09:30:23 +0200 Subject: [PATCH 1/2] fix: use default org and bucket --- Client.Test/WriteApiAsyncTest.cs | 24 ++++++++++++++++++++++++ Client/WriteApiAsync.cs | 9 +++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Client.Test/WriteApiAsyncTest.cs b/Client.Test/WriteApiAsyncTest.cs index 6480cf7a5..81302b508 100644 --- a/Client.Test/WriteApiAsyncTest.cs +++ b/Client.Test/WriteApiAsyncTest.cs @@ -239,6 +239,30 @@ await writeApi.WriteRecordAsync( ae.Message); } + [Test] + public async Task UseDefaultOrganizationAndBucket() + { + MockServer + .Given(Request.Create().WithPath("/api/v2/write").UsingPost()) + .RespondWith(CreateResponse("{}")); + + _influxDbClient.Dispose(); + + var options = InfluxDBClientOptions.Builder + .CreateNew() + .Url(MockServerUrl) + .AuthenticateToken("token") + .Bucket("my-bucket") + .Org("my-org") + .Build(); + + _influxDbClient = InfluxDBClientFactory.Create(options); + + var writeApi = _influxDbClient.GetWriteApiAsync(); + await writeApi.WriteRecordsAsyncWithIRestResponse(new[] { "mem,location=a level=1.0 1" }); + await writeApi.WritePointsAsyncWithIRestResponse(new[] { PointData.Measurement("h2o").Field("water_level", 9.0D) }); + } + private string GetRequestBody(RestResponse restResponse) { var bytes = (byte[])restResponse.Request?.Parameters diff --git a/Client/WriteApiAsync.cs b/Client/WriteApiAsync.cs index e2898a394..a42223404 100644 --- a/Client/WriteApiAsync.cs +++ b/Client/WriteApiAsync.cs @@ -95,10 +95,11 @@ public Task WriteRecordsAsyncWithIRestResponse(IEnumerable WritePrecision precision = WritePrecision.Ns, string bucket = null, string org = null, CancellationToken cancellationToken = default) { + var options = new BatchWriteOptions(bucket ?? _options.Bucket, org ?? _options.Org, precision); var batch = records - .Select(it => new BatchWriteRecord(new BatchWriteOptions(bucket, org, precision), it)); + .Select(it => new BatchWriteRecord(options, it)); - return WriteDataAsyncWithIRestResponse(batch, bucket, org, precision, cancellationToken); + return WriteDataAsyncWithIRestResponse(batch, options.Bucket, options.OrganizationId, precision, cancellationToken); } /// @@ -169,12 +170,12 @@ public Task WritePointsAsyncWithIRestResponse(IEnumerable>(); foreach (var grouped in points.GroupBy(it => it.Precision)) { - var options = new BatchWriteOptions(bucket, org, grouped.Key); + var options = new BatchWriteOptions(bucket ?? _options.Bucket, org ?? _options.Org, grouped.Key); var groupedPoints = grouped .Select(it => new BatchWritePoint(options, _options, it)) .ToList(); - tasks.Add(WriteDataAsyncWithIRestResponse(groupedPoints, bucket, org, grouped.Key, cancellationToken)); + tasks.Add(WriteDataAsyncWithIRestResponse(groupedPoints, options.Bucket, options.OrganizationId, grouped.Key, cancellationToken)); } return Task.WhenAll(tasks); From c71fc366155b16425ea65ebda8183a430116f4e1 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Wed, 20 Apr 2022 09:39:32 +0200 Subject: [PATCH 2/2] docs: update CHANGELOG.md --- CHANGELOG.md | 3 +++ Client.Test/WriteApiAsyncTest.cs | 9 +++++---- Client/WriteApiAsync.cs | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a48b2d035..7e5fb28b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## 4.2.0 [unreleased] +### Bug Fixes +1. [#313](https://github.com/influxdata/influxdb-client-csharp/pull/313): Using default `org` and `bucket` in `WriteApiAsync` + ## 4.1.0 [2022-04-19] ### Features diff --git a/Client.Test/WriteApiAsyncTest.cs b/Client.Test/WriteApiAsyncTest.cs index 81302b508..47f3dc9d4 100644 --- a/Client.Test/WriteApiAsyncTest.cs +++ b/Client.Test/WriteApiAsyncTest.cs @@ -245,9 +245,9 @@ public async Task UseDefaultOrganizationAndBucket() MockServer .Given(Request.Create().WithPath("/api/v2/write").UsingPost()) .RespondWith(CreateResponse("{}")); - + _influxDbClient.Dispose(); - + var options = InfluxDBClientOptions.Builder .CreateNew() .Url(MockServerUrl) @@ -257,10 +257,11 @@ public async Task UseDefaultOrganizationAndBucket() .Build(); _influxDbClient = InfluxDBClientFactory.Create(options); - + var writeApi = _influxDbClient.GetWriteApiAsync(); await writeApi.WriteRecordsAsyncWithIRestResponse(new[] { "mem,location=a level=1.0 1" }); - await writeApi.WritePointsAsyncWithIRestResponse(new[] { PointData.Measurement("h2o").Field("water_level", 9.0D) }); + await writeApi.WritePointsAsyncWithIRestResponse(new[] + { PointData.Measurement("h2o").Field("water_level", 9.0D) }); } private string GetRequestBody(RestResponse restResponse) diff --git a/Client/WriteApiAsync.cs b/Client/WriteApiAsync.cs index a42223404..5727529fc 100644 --- a/Client/WriteApiAsync.cs +++ b/Client/WriteApiAsync.cs @@ -99,7 +99,8 @@ public Task WriteRecordsAsyncWithIRestResponse(IEnumerable var batch = records .Select(it => new BatchWriteRecord(options, it)); - return WriteDataAsyncWithIRestResponse(batch, options.Bucket, options.OrganizationId, precision, cancellationToken); + return WriteDataAsyncWithIRestResponse(batch, options.Bucket, options.OrganizationId, precision, + cancellationToken); } /// @@ -175,7 +176,8 @@ public Task WritePointsAsyncWithIRestResponse(IEnumerable new BatchWritePoint(options, _options, it)) .ToList(); - tasks.Add(WriteDataAsyncWithIRestResponse(groupedPoints, options.Bucket, options.OrganizationId, grouped.Key, cancellationToken)); + tasks.Add(WriteDataAsyncWithIRestResponse(groupedPoints, options.Bucket, options.OrganizationId, + grouped.Key, cancellationToken)); } return Task.WhenAll(tasks);