Skip to content

Commit 89e46f7

Browse files
authored
feat(write): add possibility to handle HTTP response from InfluxDB server (influxdata#194)
1 parent d6f02f2 commit 89e46f7

File tree

3 files changed

+229
-34
lines changed

3 files changed

+229
-34
lines changed

CHANGELOG.md

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

3+
### Features
4+
1. [#194](https://github.com/influxdata/influxdb-client-csharp/pull/194): Add possibility to handle HTTP response from InfluxDB server [write]
5+
36
### Bug Fixes
47
1. [#193](https://github.com/influxdata/influxdb-client-csharp/pull/193): Create services without API implementation
58

Client.Test/WriteApiAsyncTest.cs

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
4+
using System.Net;
35
using System.Threading.Tasks;
46
using InfluxDB.Client.Api.Domain;
7+
using InfluxDB.Client.Core;
58
using InfluxDB.Client.Core.Test;
69
using InfluxDB.Client.Writes;
710
using NUnit.Framework;
11+
using RestSharp;
812
using WireMock.RequestBuilders;
913

1014
namespace InfluxDB.Client.Test
@@ -88,9 +92,9 @@ public async Task SplitPointList()
8892
MockServer
8993
.Given(Request.Create().WithPath("/api/v2/write").UsingPost())
9094
.RespondWith(CreateResponse("{}"));
91-
95+
9296
var writeApi = _influxDbClient.GetWriteApiAsync();
93-
97+
9498
var points = new List<PointData>
9599
{
96100
PointData.Measurement("h2o").Tag("location", "coyote_creek").Field("water_level", 10.0D)
@@ -120,7 +124,7 @@ public async Task SplitPointList()
120124
};
121125

122126
var batches = points
123-
.Select((x, i) => new { Index = i, Value = x })
127+
.Select((x, i) => new {Index = i, Value = x})
124128
.GroupBy(x => x.Index / 5)
125129
.Select(x => x.Select(v => v.Value).ToList())
126130
.ToList();
@@ -129,8 +133,91 @@ public async Task SplitPointList()
129133
{
130134
await writeApi.WritePointsAsync("my-bucket", "my-org", batch);
131135
}
132-
136+
133137
Assert.AreEqual(3, MockServer.LogEntries.Count());
134138
}
139+
140+
[Test]
141+
public async Task WriteRecordsWithIRestResponse()
142+
{
143+
MockServer
144+
.Given(Request.Create().WithPath("/api/v2/write").UsingPost())
145+
.RespondWith(CreateResponse("{}"));
146+
147+
var writeApi = _influxDbClient.GetWriteApiAsync();
148+
var response = await writeApi.WriteRecordsAsyncWithIRestResponse(
149+
new[] {"h2o,location=coyote_creek water_level=9 1"},
150+
"my-bucket",
151+
"my-org",
152+
WritePrecision.Ms);
153+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
154+
155+
var request = MockServer.LogEntries.ToList()[0];
156+
StringAssert.EndsWith("/api/v2/write?org=my-org&bucket=my-bucket&precision=ms",
157+
request.RequestMessage.AbsoluteUrl);
158+
Assert.AreEqual("h2o,location=coyote_creek water_level=9 1", GetRequestBody(response));
159+
}
160+
161+
[Test]
162+
public async Task WritePointsWithIRestResponse()
163+
{
164+
MockServer
165+
.Given(Request.Create().WithPath("/api/v2/write").UsingPost())
166+
.RespondWith(CreateResponse("{}"));
167+
168+
var writeApi = _influxDbClient.GetWriteApiAsync();
169+
var responses = await writeApi.WritePointsAsyncWithIRestResponse(
170+
new[]
171+
{
172+
PointData.Measurement("h2o")
173+
.Tag("location", "coyote_creek")
174+
.Field("water_level", 9.0D)
175+
.Timestamp(9L, WritePrecision.S),
176+
PointData.Measurement("h2o")
177+
.Tag("location", "coyote_creek")
178+
.Field("water_level", 10.0D)
179+
.Timestamp(10L, WritePrecision.Ms)
180+
},
181+
"my-bucket",
182+
"my-org");
183+
184+
Assert.AreEqual(2, responses.Length);
185+
Assert.AreEqual(HttpStatusCode.OK, responses[0].StatusCode);
186+
Assert.AreEqual("h2o,location=coyote_creek water_level=9 9", GetRequestBody(responses[0]));
187+
Assert.AreEqual(HttpStatusCode.OK, responses[1].StatusCode);
188+
Assert.AreEqual("h2o,location=coyote_creek water_level=10 10", GetRequestBody(responses[1]));
189+
}
190+
191+
[Test]
192+
public async Task WriteMeasurementsWithIRestResponse()
193+
{
194+
MockServer
195+
.Given(Request.Create().WithPath("/api/v2/write").UsingPost())
196+
.RespondWith(CreateResponse("{}"));
197+
198+
var writeApi = _influxDbClient.GetWriteApiAsync();
199+
var response = await writeApi.WriteMeasurementsAsyncWithIRestResponse(
200+
new[]
201+
{
202+
new SimpleModel
203+
{
204+
Time = new DateTime(2020, 11, 15, 8, 20, 15, DateTimeKind.Utc),
205+
Device = "id-1",
206+
Value = 16
207+
}
208+
},
209+
"my-bucket",
210+
"my-org");
211+
212+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
213+
Assert.AreEqual("m,device=id-1 value=16i 1605428415000000000", GetRequestBody(response));
214+
}
215+
216+
private string GetRequestBody(IRestResponse restResponse)
217+
{
218+
var bytes = (byte[]) restResponse.Request.Body?.Value ??
219+
throw new AssertionException("The body is required.");
220+
return System.Text.Encoding.Default.GetString(bytes);
221+
}
135222
}
136223
}

0 commit comments

Comments
 (0)