From 868079a2b14330790e5e2ddd6d6b9a97e4075341 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Fri, 10 Jun 2022 11:37:17 +0200 Subject: [PATCH 1/3] feat: add interfaces to our APIs --- Client.Legacy/FluxClient.cs | 111 ++++++- Client.Test/InfluxDbClientTest.cs | 25 +- Client/AuthorizationsApi.cs | 129 +++++++- Client/BucketsApi.cs | 328 ++++++++++++++++++- Client/ChecksApi.cs | 172 +++++++++- Client/DeleteApi.cs | 39 ++- Client/InfluxDBClient.cs | 405 +++++++++++++++++++++++- Client/InvokableScriptsApi.cs | 96 +++++- Client/LabelsApi.cs | 112 ++++++- Client/NotificationEndpointsApi.cs | 320 ++++++++++++++++++- Client/NotificationRulesApi.cs | 191 +++++++++++- Client/OrganizationsApi.cs | 280 ++++++++++++++++- Client/QueryApi.cs | 290 ++++++++++++++++- Client/QueryApiSync.cs | 63 +++- Client/ScraperTargetsApi.cs | 291 ++++++++++++++++- Client/SourcesApi.cs | 105 ++++++- Client/TasksApi.cs | 486 ++++++++++++++++++++++++++++- Client/TelegrafsApi.cs | 382 ++++++++++++++++++++++- Client/UsersApi.cs | 124 +++++++- Client/WriteApi.cs | 100 +++++- Client/WriteApiAsync.cs | 143 ++++++++- 21 files changed, 4170 insertions(+), 22 deletions(-) diff --git a/Client.Legacy/FluxClient.cs b/Client.Legacy/FluxClient.cs index 609c25da3..edd82055a 100644 --- a/Client.Legacy/FluxClient.cs +++ b/Client.Legacy/FluxClient.cs @@ -13,7 +13,116 @@ namespace InfluxDB.Client.Flux { - public class FluxClient : AbstractQueryClient, IDisposable + public interface IFluxClient : IDisposable + { + /// + /// Executes the Flux query against the InfluxDB and asynchronously map whole response to . + /// + /// + /// NOTE: This method is not intended for large query results. + /// Use for large data streaming. + /// + /// the flux query to execute + /// Token that enables callers to cancel the request. + /// which are matched the query + Task> QueryAsync(string query, CancellationToken cancellationToken = default); + + /// + /// Executes the Flux query against the InfluxDB and asynchronously map whole response to list of object with + /// given type. + /// + /// NOTE: This method is not intended for large query results. + /// Use for large data streaming. + /// + /// + /// the flux query to execute + /// Token that enables callers to cancel the request. + /// the type of measurement + /// which are matched the query + Task> QueryAsync(string query, CancellationToken cancellationToken = default); + + /// + /// Executes the Flux query against the InfluxDB and asynchronously stream to consumer. + /// + /// the flux query to execute + /// the callback to consume the FluxRecord result + /// the callback to consume any error notification + /// the callback to consume a notification about successfully end of stream + /// Token that enables callers to cancel the request. + /// async task + Task QueryAsync(string query, Action onNext, Action onError = null, + Action onComplete = null, CancellationToken cancellationToken = default); + + /// + /// Executes the Flux query against the InfluxDB and asynchronously stream result as POCO. + /// + /// the flux query to execute + /// the callback to consume the FluxRecord result + /// the callback to consume any error notification + /// the callback to consume a notification about successfully end of stream + /// Token that enables callers to cancel the request. + /// the type of measurement + /// async task + Task QueryAsync(string query, Action onNext, Action onError = null, + Action onComplete = null, CancellationToken cancellationToken = default); + + /// + /// Executes the Flux query against the InfluxDB and synchronously map whole response to result. + /// + /// NOTE: This method is not intended for large responses, that do not fit into memory. + /// Use + /// + /// + /// the flux query to execute> + /// Dialect is an object defining the options to use when encoding the response. + /// See dialect SPEC. + /// Token that enables callers to cancel the request. + /// the raw response that matched the query + Task QueryRawAsync(string query, string dialect = null, + CancellationToken cancellationToken = default); + + /// + /// Executes the Flux query against the InfluxDB and asynchronously stream response (line by line) to . + /// + /// the flux query to execute + /// the callback to consume the response line by line + /// Dialect is an object defining the options to use when encoding the response. See dialect SPEC. + /// the callback to consume any error notification + /// the callback to consume a notification about successfully end of stream + /// Token that enables callers to cancel the request. + /// async task + Task QueryRawAsync(string query, Action onResponse, string dialect = null, + Action onError = null, Action onComplete = null, CancellationToken cancellationToken = default); + + /// + /// Check the status of InfluxDB Server. + /// + /// Cancellation token + /// true if server is healthy otherwise return false + Task PingAsync(CancellationToken cancellationToken = default); + + /// + /// Return the version of the connected InfluxDB Server. + /// + /// Cancellation token + /// the version String, otherwise unknown + /// throws when request did not succesfully ends + Task VersionAsync(CancellationToken cancellationToken = default); + + /// + /// Set the log level for the request and response information. + /// + /// the log level to set + void SetLogLevel(LogLevel logLevel); + + /// + /// Set the that is used for logging requests and responses. + /// + /// Log Level + LogLevel GetLogLevel(); + } + + public class FluxClient : AbstractQueryClient, IFluxClient { private readonly LoggingHandler _loggingHandler; diff --git a/Client.Test/InfluxDbClientTest.cs b/Client.Test/InfluxDbClientTest.cs index 3cb06dcb2..f03b735ac 100644 --- a/Client.Test/InfluxDbClientTest.cs +++ b/Client.Test/InfluxDbClientTest.cs @@ -1,15 +1,19 @@ +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Net.Http; using System.Reflection; +using System.Threading; using System.Threading.Tasks; using InfluxDB.Client.Api.Client; using InfluxDB.Client.Api.Domain; using InfluxDB.Client.Api.Service; using InfluxDB.Client.Core; using InfluxDB.Client.Core.Exceptions; +using InfluxDB.Client.Core.Flux.Domain; using InfluxDB.Client.Core.Test; +using Moq; using NUnit.Framework; using WireMock.RequestBuilders; using WireMock.ResponseBuilders; @@ -21,7 +25,7 @@ namespace InfluxDB.Client.Test [TestFixture] public class InfluxDbClientTest : AbstractMockServerTest { - private InfluxDBClient _client; + private IInfluxDBClient _client; [SetUp] public new void SetUp() @@ -393,5 +397,24 @@ public async Task CustomCertificateValidationCallback() Assert.IsTrue(reached); } + + [Test] + public void TestMocking() + { + var mockClient = new Mock(); + var mockQueryApi = new Mock(); + + mockClient + .Setup(library => library.GetQueryApiSync(null)) + .Returns(mockQueryApi.Object); + + var mockTables = new List { new FluxTable() }; + mockQueryApi + .Setup(api => api.QuerySync("from(...", "my-org", CancellationToken.None)) + .Returns(mockTables); + + var tables = mockClient.Object.GetQueryApiSync().QuerySync("from(...", "my-org"); + Assert.AreEqual(mockTables, tables); + } } } \ No newline at end of file diff --git a/Client/AuthorizationsApi.cs b/Client/AuthorizationsApi.cs index 4c4ab1454..e291ed419 100644 --- a/Client/AuthorizationsApi.cs +++ b/Client/AuthorizationsApi.cs @@ -9,7 +9,134 @@ namespace InfluxDB.Client { - public class AuthorizationsApi + public interface IAuthorizationsApi + { + /// + /// Create an authorization with defined permissions. + /// + /// the owner of the authorization + /// the permissions for the authorization + /// Cancellation token + /// the created authorization + Task CreateAuthorizationAsync(Organization organization, List permissions, + CancellationToken cancellationToken = default); + + /// + /// Create an authorization with defined permissions. + /// + /// the owner id of the authorization + /// the permissions for the authorization + /// Cancellation token + /// the created authorization + Task CreateAuthorizationAsync(string orgId, List permissions, + CancellationToken cancellationToken = default); + + /// + /// Create an authorization with defined permissions. + /// + /// authorization to create + /// Cancellation token + /// the created authorization + Task CreateAuthorizationAsync(Authorization authorization, + CancellationToken cancellationToken = default); + + /// + /// Create an authorization with defined permissions. + /// + /// authorization to create + /// Cancellation token + /// the created authorization + Task CreateAuthorizationAsync(AuthorizationPostRequest authorization, + CancellationToken cancellationToken = default); + + /// + /// Updates the status of the authorization. Useful for setting an authorization to inactive or active. + /// + /// the authorization with updated status + /// Cancellation token + /// the updated authorization + Task UpdateAuthorizationAsync(Authorization authorization, + CancellationToken cancellationToken = default); + + /// + /// Delete an authorization. + /// + /// authorization to delete + /// Cancellation token + /// authorization deleted + Task DeleteAuthorizationAsync(Authorization authorization, CancellationToken cancellationToken = default); + + /// + /// Delete an authorization. + /// + /// ID of authorization to delete + /// Cancellation token + /// authorization deleted + Task DeleteAuthorizationAsync(string authorizationId, CancellationToken cancellationToken = default); + + /// + /// Clone an authorization. + /// + /// ID of authorization to clone + /// Cancellation token + /// cloned authorization + Task CloneAuthorizationAsync(string authorizationId, + CancellationToken cancellationToken = default); + + /// + /// Clone an authorization. + /// + /// authorization to clone + /// Cancellation token + /// cloned authorization + Task CloneAuthorizationAsync(Authorization authorization, + CancellationToken cancellationToken = default); + + /// + /// Retrieve an authorization. + /// + /// ID of authorization to get + /// Cancellation token + /// authorization details + Task FindAuthorizationByIdAsync(string authorizationId, + CancellationToken cancellationToken = default); + + /// + /// List all authorizations. + /// + /// Cancellation token + /// List all authorizations. + Task> FindAuthorizationsAsync(CancellationToken cancellationToken = default); + + /// + /// List all authorizations belonging to specified user. + /// + /// user + /// Cancellation token + /// A list of authorizations + Task> FindAuthorizationsByUserAsync(User user, + CancellationToken cancellationToken = default); + + /// + /// List all authorizations belonging to specified user. + /// + /// ID of user + /// Cancellation token + /// A list of authorizations + Task> FindAuthorizationsByUserIdAsync(string userId, + CancellationToken cancellationToken = default); + + /// + /// List all authorizations belonging to specified user. + /// + /// Name of User + /// Cancellation token + /// A list of authorizations + Task> FindAuthorizationsByUserNameAsync(string userName, + CancellationToken cancellationToken = default); + } + + public class AuthorizationsApi : IAuthorizationsApi { private readonly AuthorizationsService _service; diff --git a/Client/BucketsApi.cs b/Client/BucketsApi.cs index b3cdc6ad1..8c19b5797 100644 --- a/Client/BucketsApi.cs +++ b/Client/BucketsApi.cs @@ -10,7 +10,333 @@ namespace InfluxDB.Client { - public class BucketsApi + public interface IBucketsApi + { + /// + /// Creates a new bucket and sets with the new identifier. + /// + /// bucket to create + /// Cancellation token + /// created Bucket + Task CreateBucketAsync(Bucket bucket, CancellationToken cancellationToken = default); + + /// + /// Creates a new bucket and sets with the new identifier. + /// + /// bucket to create + /// Cancellation token + /// created Bucket + Task CreateBucketAsync(PostBucketRequest bucket, CancellationToken cancellationToken = default); + + /// + /// Creates a new bucket and sets with the new identifier. + /// + /// name of the bucket + /// owner of the bucket + /// Cancellation token + /// created Bucket + Task CreateBucketAsync(string name, Organization organization, + CancellationToken cancellationToken = default); + + /// + /// Creates a new bucket and sets with the new identifier. + /// + /// name of the bucket + /// retention rule of the bucket + /// owner of the bucket + /// Cancellation token + /// created Bucket + Task CreateBucketAsync(string name, BucketRetentionRules bucketRetentionRules, + Organization organization, CancellationToken cancellationToken = default); + + /// + /// Creates a new bucket and sets with the new identifier. + /// + /// name of the bucket + /// owner of the bucket + /// Cancellation token + /// created Bucket + Task CreateBucketAsync(string name, string orgId, CancellationToken cancellationToken = default); + + /// + /// Creates a new bucket and sets with the new identifier. + /// + /// name of the bucket + /// retention rule of the bucket + /// owner of the bucket + /// Cancellation token + /// created Bucket + Task CreateBucketAsync(string name, BucketRetentionRules bucketRetentionRules, string orgId, + CancellationToken cancellationToken = default); + + /// + /// Update a bucket name and retention. + /// + /// bucket update to apply + /// Cancellation token + /// bucket updated + Task UpdateBucketAsync(Bucket bucket, CancellationToken cancellationToken = default); + + /// + /// Delete a bucket. + /// + /// ID of bucket to delete + /// Cancellation token + /// delete has been accepted + Task DeleteBucketAsync(string bucketId, CancellationToken cancellationToken = default); + + /// + /// Delete a bucket. + /// + /// bucket to delete + /// Cancellation token + /// delete has been accepted + Task DeleteBucketAsync(Bucket bucket, CancellationToken cancellationToken = default); + + /// + /// Clone a bucket. + /// + /// name of cloned bucket + /// ID of bucket to clone + /// Cancellation token + /// cloned bucket + Task CloneBucketAsync(string clonedName, string bucketId, + CancellationToken cancellationToken = default); + + /// + /// Clone a bucket. + /// + /// name of cloned bucket + /// bucket to clone + /// Cancellation token + /// cloned bucket + Task CloneBucketAsync(string clonedName, Bucket bucket, + CancellationToken cancellationToken = default); + + /// + /// Retrieve a bucket. + /// + /// ID of bucket to get + /// Cancellation token + /// Bucket Details + Task FindBucketByIdAsync(string bucketId, CancellationToken cancellationToken = default); + + /// + /// Retrieve a bucket. + /// + /// Name of bucket to get + /// Cancellation token + /// Bucket Details + Task FindBucketByNameAsync(string bucketName, + CancellationToken cancellationToken = default); + + /// + /// List all buckets for specified organization. + /// + /// filter buckets to a specific organization + /// Cancellation token + /// A list of buckets + Task> FindBucketsByOrganizationAsync(Organization organization, + CancellationToken cancellationToken = default); + + /// + /// List all buckets for specified orgId. + /// + /// filter buckets to a specific organization + /// Cancellation token + /// A list of buckets + Task> FindBucketsByOrgNameAsync(string orgName, + CancellationToken cancellationToken = default); + + /// + /// List all buckets. + /// + /// (optional) + /// (optional, default to 20) + /// The last resource ID from which to seek from (but not including). This is to be used instead of `offset`. (optional) + /// The name of the organization. (optional) + /// The organization ID. (optional) + /// Only returns buckets with a specific name. (optional) + /// Only returns buckets with a specific ID. (optional) + /// Cancellation token + /// List all buckets + Task> FindBucketsAsync(int? offset = null, int? limit = null, string after = null, + string org = null, string orgID = null, string name = null, string id = null, + CancellationToken cancellationToken = default); + + /// + /// List all buckets. + /// + /// the find options + /// Cancellation token + /// List all buckets + Task FindBucketsAsync(FindOptions findOptions, CancellationToken cancellationToken = default); + + /// + /// List all members of a bucket. + /// + /// bucket of the members + /// Cancellation token + /// the List all members of a bucket + Task> GetMembersAsync(Bucket bucket, CancellationToken cancellationToken = default); + + /// + /// List all members of a bucket. + /// + /// ID of bucket to get members + /// Cancellation token + /// the List all members of a bucket + Task> GetMembersAsync(string bucketId, + CancellationToken cancellationToken = default); + + /// + /// Add a bucket member. + /// + /// the member of a bucket + /// the bucket of a member + /// Cancellation token + /// created mapping + Task AddMemberAsync(User member, Bucket bucket, + CancellationToken cancellationToken = default); + + /// + /// Add a bucket member. + /// + /// the ID of a member + /// the ID of a bucket + /// Cancellation token + /// created mapping + Task AddMemberAsync(string memberId, string bucketId, + CancellationToken cancellationToken = default); + + /// + /// Removes a member from a bucket. + /// + /// the member of a bucket + /// the bucket of a member + /// Cancellation token + /// member removed + Task DeleteMemberAsync(User member, Bucket bucket, CancellationToken cancellationToken = default); + + /// + /// Removes a member from a bucket. + /// + /// the ID of a member + /// the ID of a bucket + /// Cancellation token + /// member removed + Task DeleteMemberAsync(string memberId, string bucketId, CancellationToken cancellationToken = default); + + /// + /// List all owners of a bucket. + /// + /// bucket of the owners + /// Cancellation token + /// the List all owners of a bucket + Task> GetOwnersAsync(Bucket bucket, CancellationToken cancellationToken = default); + + /// + /// List all owners of a bucket. + /// + /// ID of a bucket to get owners + /// Cancellation token + /// the List all owners of a bucket + Task> GetOwnersAsync(string bucketId, + CancellationToken cancellationToken = default); + + /// + /// Add a bucket owner. + /// + /// the owner of a bucket + /// the bucket of a owner + /// Cancellation token + /// created mapping + Task AddOwnerAsync(User owner, Bucket bucket, + CancellationToken cancellationToken = default); + + /// + /// Add a bucket owner. + /// + /// the ID of a owner + /// the ID of a bucket + /// Cancellation token + /// created mapping + Task AddOwnerAsync(string ownerId, string bucketId, + CancellationToken cancellationToken = default); + + /// + /// Removes a owner from a bucket. + /// + /// the owner of a bucket + /// the bucket of a owner + /// Cancellation token + /// owner removed + Task DeleteOwnerAsync(User owner, Bucket bucket, CancellationToken cancellationToken = default); + + /// + /// Removes a owner from a bucket. + /// + /// the ID of a owner + /// the ID of a bucket + /// Cancellation token + /// owner removed + Task DeleteOwnerAsync(string ownerId, string bucketId, CancellationToken cancellationToken = default); + + /// + /// List all labels of a bucket. + /// + /// bucket of the labels + /// Cancellation token + /// the List all labels of a bucket + Task> GetLabelsAsync(Bucket bucket, CancellationToken cancellationToken = default); + + /// + /// List all labels of a bucket. + /// + /// ID of a bucket to get labels + /// Cancellation token + /// the List all labels of a bucket + Task> GetLabelsAsync(string bucketId, CancellationToken cancellationToken = default); + + /// + /// Add a bucket label. + /// + /// the label of a bucket + /// the bucket of a label + /// Cancellation token + /// added label + Task