Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 25 additions & 0 deletions Client.Test/WriteApiAsyncTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,31 @@ 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
Expand Down
11 changes: 7 additions & 4 deletions Client/WriteApiAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ public Task<RestResponse> WriteRecordsAsyncWithIRestResponse(IEnumerable<string>
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);
}

/// <summary>
Expand Down Expand Up @@ -169,12 +171,13 @@ public Task<RestResponse[]> WritePointsAsyncWithIRestResponse(IEnumerable<PointD
var tasks = new List<Task<RestResponse>>();
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);
Expand Down