using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Api.Service;
using InfluxDB.Client.Core;
using Task = System.Threading.Tasks.Task;
namespace InfluxDB.Client
{
public interface ILabelsApi
{
///
/// Create a label
///
/// label to create
/// Cancellation token
/// Added label
Task CreateLabelAsync(LabelCreateRequest request,
CancellationToken cancellationToken = default);
///
/// Create a label
///
/// name of a label
/// properties of a label
/// owner of a label
/// Cancellation token
/// Added label
Task CreateLabelAsync(string name, Dictionary properties,
string orgId, CancellationToken cancellationToken = default);
///
/// Update a single label
///
/// label to update
/// Cancellation token
/// Updated label
Task UpdateLabelAsync(Label label, CancellationToken cancellationToken = default);
///
/// Update a single label
///
/// ID of label to update
/// label update
/// Cancellation token
/// Updated label
Task UpdateLabelAsync(string labelId, LabelUpdate labelUpdate,
CancellationToken cancellationToken = default);
///
/// Delete a label.
///
/// label to delete
/// Cancellation token
/// delete has been accepted
Task DeleteLabelAsync(Label label, CancellationToken cancellationToken = default);
///
/// Delete a label.
///
/// ID of label to delete
/// Cancellation token
/// delete has been accepted
Task DeleteLabelAsync(string labelId, CancellationToken cancellationToken = default);
///
/// Clone a label.
///
/// name of cloned label
/// ID of label to clone
/// Cancellation token
/// cloned label
Task CloneLabelAsync(string clonedName, string labelId,
CancellationToken cancellationToken = default);
///
/// Clone a label.
///
/// name of cloned label
/// label to clone
/// Cancellation token
/// cloned label
Task CloneLabelAsync(string clonedName, Label label,
CancellationToken cancellationToken = default);
///
/// Retrieve a label.
///
/// ID of a label to get
/// Cancellation token
/// Label detail
Task FindLabelByIdAsync(string labelId, CancellationToken cancellationToken = default);
///
/// List all labels.
/// Cancellation token
///
/// List all labels.
Task> FindLabelsAsync(CancellationToken cancellationToken = default);
///
/// Get all labels.
///
/// specifies the organization of the resource
/// Cancellation token
/// all labels
Task> FindLabelsByOrgAsync(Organization organization,
CancellationToken cancellationToken = default);
///
/// Get all labels.
///
/// specifies the organization ID of the resource
/// Cancellation token
/// all labels
Task> FindLabelsByOrgIdAsync(string orgId,
CancellationToken cancellationToken = default);
}
public class LabelsApi : ILabelsApi
{
private readonly LabelsService _service;
protected internal LabelsApi(LabelsService service)
{
Arguments.CheckNotNull(service, nameof(service));
_service = service;
}
///
/// Create a label
///
/// label to create
/// Cancellation token
/// Added label
public async Task CreateLabelAsync(LabelCreateRequest request,
CancellationToken cancellationToken = default)
{
Arguments.CheckNotNull(request, nameof(request));
var response = await _service.PostLabelsAsync(request, cancellationToken).ConfigureAwait(false);
return response.Label;
}
///
/// Create a label
///
/// name of a label
/// properties of a label
/// owner of a label
/// Cancellation token
/// Added label
public Task CreateLabelAsync(string name, Dictionary properties,
string orgId, CancellationToken cancellationToken = default)
{
Arguments.CheckNonEmptyString(name, nameof(name));
Arguments.CheckNonEmptyString(orgId, nameof(orgId));
Arguments.CheckNotNull(properties, nameof(properties));
return CreateLabelAsync(new LabelCreateRequest(orgId, name, properties), cancellationToken);
}
///
/// Update a single label
///
/// label to update
/// Cancellation token
/// Updated label
public Task UpdateLabelAsync(Label label, CancellationToken cancellationToken = default)
{
Arguments.CheckNotNull(label, nameof(label));
var labelUpdate = new LabelUpdate { Properties = label.Properties };
return UpdateLabelAsync(label.Id, labelUpdate, cancellationToken);
}
///
/// Update a single label
///
/// ID of label to update
/// label update
/// Cancellation token
/// Updated label
public async Task UpdateLabelAsync(string labelId, LabelUpdate labelUpdate,
CancellationToken cancellationToken = default)
{
var response = await _service.PatchLabelsIDAsync(labelId, labelUpdate, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return response.Label;
}
///
/// Delete a label.
///
/// label to delete
/// Cancellation token
/// delete has been accepted
public Task DeleteLabelAsync(Label label, CancellationToken cancellationToken = default)
{
Arguments.CheckNotNull(label, nameof(label));
return DeleteLabelAsync(label.Id, cancellationToken);
}
///
/// Delete a label.
///
/// ID of label to delete
/// Cancellation token
/// delete has been accepted
public Task DeleteLabelAsync(string labelId, CancellationToken cancellationToken = default)
{
Arguments.CheckNonEmptyString(labelId, nameof(labelId));
return _service.DeleteLabelsIDAsync(labelId, cancellationToken: cancellationToken);
}
///
/// Clone a label.
///
/// name of cloned label
/// ID of label to clone
/// Cancellation token
/// cloned label
public async Task CloneLabelAsync(string clonedName, string labelId,
CancellationToken cancellationToken = default)
{
Arguments.CheckNonEmptyString(clonedName, nameof(clonedName));
Arguments.CheckNonEmptyString(labelId, nameof(labelId));
var label = await FindLabelByIdAsync(labelId).ConfigureAwait(false);
return await CloneLabelAsync(clonedName, label, cancellationToken).ConfigureAwait(false);
}
///
/// Clone a label.
///
/// name of cloned label
/// label to clone
/// Cancellation token
/// cloned label
public Task CloneLabelAsync(string clonedName, Label label,
CancellationToken cancellationToken = default)
{
Arguments.CheckNonEmptyString(clonedName, nameof(clonedName));
Arguments.CheckNotNull(label, nameof(label));
var cloned =
new LabelCreateRequest(label.OrgID, clonedName, new Dictionary(label.Properties));
return CreateLabelAsync(cloned, cancellationToken);
}
///
/// Retrieve a label.
///
/// ID of a label to get
/// Cancellation token
/// Label detail
public async Task FindLabelByIdAsync(string labelId, CancellationToken cancellationToken = default)
{
Arguments.CheckNonEmptyString(labelId, nameof(labelId));
var response = await _service.GetLabelsIDAsync(labelId, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return response.Label;
}
///
/// List all labels.
/// Cancellation token
///
/// List all labels.
public async Task> FindLabelsAsync(CancellationToken cancellationToken = default)
{
var response = await _service.GetLabelsAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
return response.Labels;
}
///
/// Get all labels.
///
/// specifies the organization of the resource
/// Cancellation token
/// all labels
public Task> FindLabelsByOrgAsync(Organization organization,
CancellationToken cancellationToken = default)
{
Arguments.CheckNotNull(organization, nameof(organization));
return FindLabelsByOrgIdAsync(organization.Id, cancellationToken);
}
///
/// Get all labels.
///
/// specifies the organization ID of the resource
/// Cancellation token
/// all labels
public async Task> FindLabelsByOrgIdAsync(string orgId,
CancellationToken cancellationToken = default)
{
var response = await _service.GetLabelsAsync(null, orgId, cancellationToken).ConfigureAwait(false);
return response.Labels;
}
}
}