forked from influxdata/influxdb-client-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfluxDBClient.cs
More file actions
431 lines (369 loc) · 14.2 KB
/
Copy pathInfluxDBClient.cs
File metadata and controls
431 lines (369 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
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.Internal;
namespace InfluxDB.Client
{
public class InfluxDBClient : IDisposable
{
private readonly ApiClient _apiClient;
private readonly ExceptionFactory _exceptionFactory;
private readonly HealthService _healthService;
private readonly LoggingHandler _loggingHandler;
private readonly GzipHandler _gzipHandler;
private readonly ReadyService _readyService;
private readonly SetupService _setupService;
private readonly InfluxDBClientOptions _options;
internal readonly List<IDisposable> Apis = new List<IDisposable>();
protected internal InfluxDBClient(InfluxDBClientOptions options)
{
Arguments.CheckNotNull(options, nameof(options));
_options = options;
_loggingHandler = new LoggingHandler(options.LogLevel);
_gzipHandler = new GzipHandler();
var version = AssemblyHelper.GetVersion(typeof(InfluxDBClient));
_apiClient = new ApiClient(options, _loggingHandler, _gzipHandler);
_apiClient.RestClient.UserAgent = $"influxdb-client-csharp/{version}";
_exceptionFactory = (methodName, response) =>
!response.IsSuccessful ? HttpException.Create(response, response.Content) : null;
_setupService = new SetupService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
_healthService = new HealthService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
_readyService = new ReadyService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
}
public void Dispose()
{
//
// Dispose child APIs
//
foreach (var disposable in Apis.ToList()) disposable.Dispose();
//
// signout
//
try
{
_apiClient.Signout();
}
catch (Exception e)
{
Trace.WriteLine("The signout exception");
Trace.WriteLine(e);
}
}
/// <summary>
/// Get the Query client.
/// </summary>
/// <returns>the new client instance for the Query API</returns>
public QueryApi GetQueryApi()
{
var service = new QueryService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
return new QueryApi(_options, service);
}
/// <summary>
/// Get the Write client.
/// </summary>
/// <returns>the new client instance for the Write API</returns>
public WriteApi GetWriteApi()
{
return GetWriteApi(WriteOptions.CreateNew().Build());
}
/// <summary>
/// Get the Write client.
/// </summary>
/// <param name="writeOptions">the configuration for a write client</param>
/// <returns>the new client instance for the Write API</returns>
public WriteApi GetWriteApi(WriteOptions writeOptions)
{
var service = new WriteService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
var writeApi = new WriteApi(_options, service, writeOptions, this);
Apis.Add(writeApi);
return writeApi;
}
/// <summary>
/// Get the <see cref="Organization" /> client.
/// </summary>
/// <returns>the new client instance for Organization API</returns>
public OrganizationsApi GetOrganizationsApi()
{
var service = new OrganizationsService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
return new OrganizationsApi(service);
}
/// <summary>
/// Get the <see cref="InfluxDB.Client.Api.Domain.User" /> client.
/// </summary>
/// <returns>the new client instance for User API</returns>
public UsersApi GetUsersApi()
{
var service = new UsersService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
return new UsersApi(service);
}
/// <summary>
/// Get the <see cref="Bucket" /> client.
/// </summary>
/// <returns>the new client instance for Bucket API</returns>
public BucketsApi GetBucketsApi()
{
var service = new BucketsService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
return new BucketsApi(service);
}
/// <summary>
/// Get the <see cref="Source" /> client.
/// </summary>
/// <returns>the new client instance for Source API</returns>
public SourcesApi GetSourcesApi()
{
var service = new SourcesService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
return new SourcesApi(service);
}
/// <summary>
/// Get the <see cref="InfluxDB.Client.Api.Domain.Authorization" /> client.
/// </summary>
/// <returns>the new client instance for Authorization API</returns>
public AuthorizationsApi GetAuthorizationsApi()
{
var service = new AuthorizationsService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
return new AuthorizationsApi(service);
}
/// <summary>
/// Get the <see cref="InfluxDB.Client.Api.Domain.Task" /> client.
/// </summary>
/// <returns>the new client instance for Task API</returns>
public TasksApi GetTasksApi()
{
var service = new TasksService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
return new TasksApi(service);
}
/// <summary>
/// Get the <see cref="InfluxDB.Client.Api.Domain.ScraperTargetResponse" /> client.
/// </summary>
/// <returns>the new client instance for Scraper API</returns>
public ScraperTargetsApi GetScraperTargetsApi()
{
var service = new ScraperTargetsService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
return new ScraperTargetsApi(service);
}
/// <summary>
/// Get the <see cref="InfluxDB.Client.Api.Domain.Telegraf" /> client.
/// </summary>
/// <returns>the new client instance for Telegrafs API</returns>
public TelegrafsApi GetTelegrafsApi()
{
var service = new TelegrafsService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
return new TelegrafsApi(service);
}
/// <summary>
/// Get the <see cref="InfluxDB.Client.Api.Domain.Label" /> client.
/// </summary>
/// <returns>the new client instance for Label API</returns>
public LabelsApi GetLabelsApi()
{
var service = new LabelsService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
return new LabelsApi(service);
}
/// <summary>
/// Get the <see cref="InfluxDB.Client.Api.Domain.NotificationEndpoint" /> client.
/// </summary>
/// <returns>the new client instance for NotificationEndpoint API</returns>
public NotificationEndpointsApi GetNotificationEndpointsApi()
{
var service = new NotificationEndpointsService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
return new NotificationEndpointsApi(service);
}
/// <summary>
/// Get the <see cref="InfluxDB.Client.Api.Domain.NotificationRules" /> client.
/// </summary>
/// <returns>the new client instance for NotificationRules API</returns>
public NotificationRulesApi GetNotificationRulesApi()
{
var service = new NotificationRulesService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
return new NotificationRulesApi(service);
}
/// <summary>
/// Get the <see cref="InfluxDB.Client.Api.Domain.Check" /> client.
/// </summary>
/// <returns>the new client instance for Checks API</returns>
public ChecksApi GetChecksApi()
{
var service = new ChecksService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
return new ChecksApi(service);
}
/// <summary>
/// Get the Delete client.
/// </summary>
/// <returns>the new client instance for Delete API</returns>
public DeleteApi GetDeleteApi()
{
var service = new DefaultService((Configuration) _apiClient.Configuration)
{
ExceptionFactory = _exceptionFactory
};
return new DeleteApi(service);
}
/// <summary>
/// Set the log level for the request and response information.
/// </summary>
/// <param name="logLevel">the log level to set</param>
public void SetLogLevel(LogLevel logLevel)
{
Arguments.CheckNotNull(logLevel, nameof(logLevel));
_loggingHandler.Level = logLevel;
}
/// <summary>
/// Set the <see cref="LogLevel" /> that is used for logging requests and responses.
/// </summary>
/// <returns>Log Level</returns>
public LogLevel GetLogLevel()
{
return _loggingHandler.Level;
}
/// <summary>
/// Enable Gzip compress for http requests.
///
/// <para>Currently only the "Write" and "Query" endpoints supports the Gzip compression.</para>
/// </summary>
/// <returns></returns>
public InfluxDBClient EnableGzip()
{
_gzipHandler.EnableGzip();
return this;
}
/// <summary>
/// Disable Gzip compress for http request body.
/// </summary>
/// <returns>this</returns>
public InfluxDBClient DisableGzip()
{
_gzipHandler.DisableGzip();
return this;
}
/// <summary>
/// Returns whether Gzip compress for http request body is enabled.
/// </summary>
/// <returns>true if gzip is enabled.</returns>
public bool IsGzipEnabled()
{
return _gzipHandler.IsEnabledGzip();
}
/// <summary>
/// Get the health of an instance.
/// </summary>
/// <returns>health of an instance</returns>
public async Task<HealthCheck> HealthAsync()
{
return await GetHealthAsync(_healthService.GetHealthAsync());
}
/// <summary>
/// The readiness of the InfluxDB 2.0.
/// </summary>
/// <returns>return null if the InfluxDB is not ready</returns>
public async Task<Ready> ReadyAsync()
{
try
{
return await _readyService.GetReadyAsync();
}
catch (Exception e)
{
Trace.TraceError($"The exception: '{e.Message}' occurs during check instance readiness.");
return null;
}
}
/// <summary>
/// Post onboarding request, to setup initial user, org and bucket.
/// </summary>
/// <param name="onboarding">to setup defaults</param>
/// <exception cref="HttpException">With status code 422 when an onboarding has already been completed</exception>
/// <returns>defaults for first run</returns>
public async Task<OnboardingResponse> OnboardingAsync(OnboardingRequest onboarding)
{
Arguments.CheckNotNull(onboarding, nameof(onboarding));
return await _setupService.PostSetupAsync(onboarding);
}
/// <summary>
/// Check if database has default user, org, bucket created, returns true if not.
/// </summary>
/// <returns>True if onboarding has already been completed otherwise false</returns>
public async Task<bool> IsOnboardingAllowedAsync()
{
var isOnboardingAllowed = _setupService.GetSetupAsync().ContinueWith(t => t.Result.Allowed == true);
return await isOnboardingAllowed;
}
internal static string AuthorizationHeader(string username, string password)
{
return "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password));
}
internal static async Task<HealthCheck> GetHealthAsync(Task<HealthCheck> task)
{
Arguments.CheckNotNull(task, nameof(task));
return await task
.ContinueWith(t =>
{
if (t.Exception != null)
{
return new HealthCheck("influxdb", t.Exception?.Message, default(List<HealthCheck>),
HealthCheck.StatusEnum.Fail);
}
return t.Result;
}, CancellationToken.None);
}
}
}