private static string SerializeWriteRequest(WriteRequest request) { var queryList = new List <string>(); foreach (Point point in request.Points) { string value = $" value={point.Value.ToString(new CultureInfo("en-US"))}"; string timestamp = point.Timestamp.HasValue ? $" { point.Timestamp.Value.ToUnixNanoseconds().ToString() }" : string.Empty; queryList.Add($"{ request.Name }{ value }{ timestamp }"); } return(string.Join("\n", queryList)); }
public async Task Write(WriteRequest writeRequest) { if (writeRequest == null) { throw new ArgumentNullException(nameof(writeRequest)); } var query = new Dictionary <string, string>(); query.Add("db", this._settings.DatabaseName); string queryAsString = BuildQuery(query); var uriBuilder = new UriBuilder(_settings.DatabaseAddress) { Path = "write", Query = queryAsString }; HttpRequestMessage request = PrepareRequest(HttpMethod.Post, uriBuilder.Uri); try { string serializedRequest = SerializeWriteRequest(writeRequest); _logger.LogDebug("Write HTTP request | Method: {0}, Uri: {1}, Content {2}", request.Method, request.RequestUri, serializedRequest); request.Content = new StringContent(serializedRequest, Encoding.UTF8, "text/plain"); HttpResponseMessage response = await this._httpClient.SendAsync(request); await response.EnsureSuccessStatusCodeAsync(); } catch (Exception ex) { throw new MeasurementsRepositoryException("Cannot write measurements into database.", ex); } }