using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Api.Service;
using InfluxDB.Client.Core;
using InfluxDB.Client.Core.Flux.Domain;
using InfluxDB.Client.Core.Flux.Internal;
using InfluxDB.Client.Core.Internal;
using RestSharp;
namespace InfluxDB.Client
{
public interface IQueryApiSync
{
///
/// Executes the Flux query against the InfluxDB 2.x and synchronously map whole response
/// to list of object with given type.
///
///
/// NOTE: This method is not intended for large query results.
///
///
/// the flux query to execute
/// specifies the source organization. If the org is not specified then is used config from .
/// Token that enables callers to cancel the request.
/// the type of measurement
/// Measurements which are matched the query
List QuerySync(string query, string org = null, CancellationToken cancellationToken = default);
///
/// Executes the Flux query against the InfluxDB 2.x and synchronously map whole response
/// to list of object with given type.
///
///
/// NOTE: This method is not intended for large query results.
///
///
/// the flux query to execute
/// specifies the source organization. If the org is not specified then is used config from .
/// Token that enables callers to cancel the request.
/// the type of measurement
/// Measurements which are matched the query
List QuerySync(Query query, string org = null, CancellationToken cancellationToken = default);
///
/// Executes the Flux query against the InfluxDB 2.x and synchronously map whole response
/// to s.
///
///
/// NOTE: This method is not intended for large query results.
///
///
/// the flux query to execute
/// specifies the source organization. If the org is not specified then is used config from .
/// Token that enables callers to cancel the request.
/// FluxTables that are matched the query
List QuerySync(string query, string org = null, CancellationToken cancellationToken = default);
///
/// Executes the Flux query against the InfluxDB 2.x and synchronously map whole response
/// to s.
///
///
/// NOTE: This method is not intended for large query results.
///
///
/// the flux query to execute
/// specifies the source organization. If the org is not specified then is used config from .
/// Token that enables callers to cancel the request.
/// FluxTables that are matched the query
List QuerySync(Query query, string org = null, CancellationToken cancellationToken = default);
}
///
/// The synchronous version of QueryApi.
///
public class QueryApiSync : AbstractQueryClient, IQueryApiSync
{
private readonly InfluxDBClientOptions _options;
private readonly QueryService _service;
protected internal QueryApiSync(InfluxDBClientOptions options, QueryService service, IFluxResultMapper mapper) :
base(mapper)
{
Arguments.CheckNotNull(options, nameof(options));
Arguments.CheckNotNull(service, nameof(service));
_options = options;
_service = service;
RestClient = service.Configuration.ApiClient.RestClient;
}
///
/// Executes the Flux query against the InfluxDB 2.x and synchronously map whole response
/// to list of object with given type.
///
///
/// NOTE: This method is not intended for large query results.
///
///
/// the flux query to execute
/// specifies the source organization. If the org is not specified then is used config from .
/// Token that enables callers to cancel the request.
/// the type of measurement
/// Measurements which are matched the query
public List QuerySync(string query, string org = null, CancellationToken cancellationToken = default)
{
Arguments.CheckNonEmptyString(query, nameof(query));
return QuerySync(QueryApi.CreateQuery(query, QueryApi.Dialect), org, cancellationToken);
}
///
/// Executes the Flux query against the InfluxDB 2.x and synchronously map whole response
/// to list of object with given type.
///
///
/// NOTE: This method is not intended for large query results.
///
///
/// the flux query to execute
/// specifies the source organization. If the org is not specified then is used config from .
/// Token that enables callers to cancel the request.
/// the type of measurement
/// Measurements which are matched the query
public List QuerySync(Query query, string org = null, CancellationToken cancellationToken = default)
{
Arguments.CheckNotNull(query, nameof(query));
var optionsOrg = org ?? _options.Org;
Arguments.CheckNonEmptyString(optionsOrg, OrgArgumentValidation);
var measurements = new List();
var consumer = new FluxResponseConsumerPoco(poco => { measurements.Add(poco); }, Mapper);
RestRequest QueryFn(Func advancedResponseWriter)
{
return _service
.PostQueryWithRestRequest(null, "application/json", null, optionsOrg, null, query)
.AddAdvancedResponseHandler(advancedResponseWriter);
}
QuerySync(QueryFn, consumer, ErrorConsumer, EmptyAction, cancellationToken);
return measurements;
}
///
/// Executes the Flux query against the InfluxDB 2.x and synchronously map whole response
/// to s.
///
///
/// NOTE: This method is not intended for large query results.
///
///
/// the flux query to execute
/// specifies the source organization. If the org is not specified then is used config from .
/// Token that enables callers to cancel the request.
/// FluxTables that are matched the query
public List QuerySync(string query, string org = null, CancellationToken cancellationToken = default)
{
Arguments.CheckNonEmptyString(query, nameof(query));
return QuerySync(QueryApi.CreateQuery(query, QueryApi.Dialect), org, cancellationToken);
}
///
/// Executes the Flux query against the InfluxDB 2.x and synchronously map whole response
/// to s.
///
///
/// NOTE: This method is not intended for large query results.
///
///
/// the flux query to execute
/// specifies the source organization. If the org is not specified then is used config from .
/// Token that enables callers to cancel the request.
/// FluxTables that are matched the query
public List QuerySync(Query query, string org = null, CancellationToken cancellationToken = default)
{
Arguments.CheckNotNull(query, nameof(query));
var consumer = new FluxCsvParser.FluxResponseConsumerTable();
var optionsOrg = org ?? _options.Org;
Arguments.CheckNonEmptyString(optionsOrg, OrgArgumentValidation);
RestRequest QueryFn(Func advancedResponseWriter)
{
return _service
.PostQueryWithRestRequest(null, "application/json", null, optionsOrg, null, query)
.AddAdvancedResponseHandler(advancedResponseWriter);
}
QuerySync(QueryFn, consumer, ErrorConsumer, EmptyAction, cancellationToken);
return consumer.Tables;
}
protected override void BeforeIntercept(RestRequest request)
{
_service.Configuration.ApiClient.BeforeIntercept(request);
}
protected override T AfterIntercept(int statusCode, Func> headers, T body)
{
return _service.Configuration.ApiClient.AfterIntercept(statusCode, headers, body);
}
}
}