-
Notifications
You must be signed in to change notification settings - Fork 93
/
InvokableScriptsApi.cs
312 lines (271 loc) · 14.2 KB
/
InvokableScriptsApi.cs
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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
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 IInvokableScriptsApi
{
/// <summary>
/// Create a script.
/// </summary>
/// <param name="createRequest">The script to create.</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>The created script.</returns>
Task<Script> CreateScriptAsync(ScriptCreateRequest createRequest,
CancellationToken cancellationToken = default);
/// <summary>
/// Update a script.
/// </summary>
/// <param name="scriptId">The ID of the script to update. (required)</param>
/// <param name="updateRequest">Script updates to apply (required)</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>The updated script.</returns>
Task<Script> UpdateScriptAsync(string scriptId, ScriptUpdateRequest updateRequest,
CancellationToken cancellationToken = default);
/// <summary>
/// Delete a script.
/// </summary>
/// <param name="scriptId">The ID of the script to delete. (required)</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>delete has been accepted</returns>
Task DeleteScriptAsync(string scriptId, CancellationToken cancellationToken = default);
/// <summary>
/// List scripts.
/// </summary>
/// <param name="offset">The offset for pagination.</param>
/// <param name="limit">The number of scripts to return.</param>
/// <param name="cancellationToken"></param>
/// <returns>[Script]</returns>
Task<List<Script>> FindScriptsAsync(int? offset = null, int? limit = null,
CancellationToken cancellationToken = default);
/// <summary>
/// Invoke a script and return result as a [FluxTable].
/// </summary>
/// <param name="scriptId">The ID of the script to invoke. (required)</param>
/// <param name="bindParams">Represent key/value pairs parameters to be injected into script</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>[FluxTable]</returns>
Task<List<FluxTable>> InvokeScriptAsync(string scriptId,
Dictionary<string, object> bindParams = default,
CancellationToken cancellationToken = default);
/// <summary>
/// Invoke a script and return result as a [T].
/// </summary>
/// <param name="scriptId">The ID of the script to invoke. (required)</param>
/// <param name="bindParams">Represent key/value pairs parameters to be injected into script</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>[T]</returns>
Task<List<T>> InvokeScriptMeasurementsAsync<T>(string scriptId,
Dictionary<string, object> bindParams = default,
CancellationToken cancellationToken = default);
/// <summary>
/// Invoke a script and return result as a raw string.
/// </summary>
/// <param name="scriptId">The ID of the script to invoke. (required)</param>
/// <param name="bindParams">Represent key/value pairs parameters to be injected into script</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>String</returns>
Task<string> InvokeScriptRawAsync(string scriptId, Dictionary<string, object> bindParams = default,
CancellationToken cancellationToken = default);
/// <summary>
/// Invoke a script and return result as a stream of FluxRecord.
/// </summary>
/// <param name="scriptId">The ID of the script to invoke. (required)</param>
/// <param name="bindParams">Represent key/value pairs parameters to be injected into script</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>stream of FluxRecord</returns>
IAsyncEnumerable<FluxRecord> InvokeScriptEnumerableAsync(string scriptId,
Dictionary<string, object> bindParams = default,
CancellationToken cancellationToken = default);
/// <summary>
/// Invoke a script and return result as a stream of Measurement.
/// </summary>
/// <param name="scriptId">The ID of the script to invoke. (required)</param>
/// <param name="bindParams">Represent key/value pairs parameters to be injected into script</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>stream of Measurement</returns>
IAsyncEnumerable<T> InvokeScriptMeasurementsEnumerableAsync<T>(string scriptId,
Dictionary<string, object> bindParams = default,
CancellationToken cancellationToken = default);
}
public class InvokableScriptsApi : AbstractQueryClient, IInvokableScriptsApi
{
private readonly InvokableScriptsService _service;
protected internal InvokableScriptsApi(InvokableScriptsService service, IFluxResultMapper mapper) : base(mapper,
new FluxCsvParser(FluxCsvParser.ResponseMode.OnlyNames))
{
Arguments.CheckNotNull(service, nameof(service));
_service = service;
RestClient = service.Configuration.ApiClient.RestClient;
}
/// <summary>
/// Create a script.
/// </summary>
/// <param name="createRequest">The script to create.</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>The created script.</returns>
public Task<Script> CreateScriptAsync(ScriptCreateRequest createRequest,
CancellationToken cancellationToken = default)
{
Arguments.CheckNotNull(createRequest, nameof(createRequest));
return _service.PostScriptsAsync(createRequest, cancellationToken);
}
/// <summary>
/// Update a script.
/// </summary>
/// <param name="scriptId">The ID of the script to update. (required)</param>
/// <param name="updateRequest">Script updates to apply (required)</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>The updated script.</returns>
public Task<Script> UpdateScriptAsync(string scriptId, ScriptUpdateRequest updateRequest,
CancellationToken cancellationToken = default)
{
Arguments.CheckNonEmptyString(scriptId, nameof(scriptId));
Arguments.CheckNotNull(updateRequest, nameof(updateRequest));
return _service.PatchScriptsIDAsync(scriptId, updateRequest, cancellationToken);
}
/// <summary>
/// Delete a script.
/// </summary>
/// <param name="scriptId">The ID of the script to delete. (required)</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>delete has been accepted</returns>
public Task DeleteScriptAsync(string scriptId, CancellationToken cancellationToken = default)
{
Arguments.CheckNonEmptyString(scriptId, nameof(scriptId));
return _service.DeleteScriptsIDAsync(scriptId, cancellationToken);
}
/// <summary>
/// List scripts.
/// </summary>
/// <param name="offset">The offset for pagination.</param>
/// <param name="limit">The number of scripts to return.</param>
/// <param name="cancellationToken"></param>
/// <returns>[Script]</returns>
public Task<List<Script>> FindScriptsAsync(int? offset = null, int? limit = null,
CancellationToken cancellationToken = default)
{
return _service.GetScriptsAsync(limit, offset, cancellationToken)
.ContinueWith(t => t.Result._Scripts, cancellationToken);
}
/// <summary>
/// Invoke a script and return result as a [FluxTable].
/// </summary>
/// <param name="scriptId">The ID of the script to invoke. (required)</param>
/// <param name="bindParams">Represent key/value pairs parameters to be injected into script</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>[FluxTable]</returns>
public async Task<List<FluxTable>> InvokeScriptAsync(string scriptId,
Dictionary<string, object> bindParams = default,
CancellationToken cancellationToken = default)
{
var consumer = new FluxCsvParser.FluxResponseConsumerTable();
await InvokeScript(scriptId, consumer, ErrorConsumer, EmptyAction, bindParams, cancellationToken)
.ConfigureAwait(false);
return consumer.Tables;
}
/// <summary>
/// Invoke a script and return result as a [T].
/// </summary>
/// <param name="scriptId">The ID of the script to invoke. (required)</param>
/// <param name="bindParams">Represent key/value pairs parameters to be injected into script</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>[T]</returns>
public async Task<List<T>> InvokeScriptMeasurementsAsync<T>(string scriptId,
Dictionary<string, object> bindParams = default,
CancellationToken cancellationToken = default)
{
var measurements = new List<T>();
var consumer = new FluxResponseConsumerPoco<T>(poco => { measurements.Add(poco); }, Mapper);
await Query(CreateRequest(scriptId, bindParams), consumer, ErrorConsumer, EmptyAction, cancellationToken)
.ConfigureAwait(false);
return measurements;
}
/// <summary>
/// Invoke a script and return result as a raw string.
/// </summary>
/// <param name="scriptId">The ID of the script to invoke. (required)</param>
/// <param name="bindParams">Represent key/value pairs parameters to be injected into script</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>String</returns>
public async Task<string> InvokeScriptRawAsync(string scriptId, Dictionary<string, object> bindParams = default,
CancellationToken cancellationToken = default)
{
var rows = new List<string>();
void Consumer(string row)
{
rows.Add(row);
}
await QueryRaw(CreateRequest(scriptId, bindParams), Consumer, ErrorConsumer, EmptyAction, cancellationToken)
.ConfigureAwait(false);
return string.Join("\n", rows);
}
/// <summary>
/// Invoke a script and return result as a stream of FluxRecord.
/// </summary>
/// <param name="scriptId">The ID of the script to invoke. (required)</param>
/// <param name="bindParams">Represent key/value pairs parameters to be injected into script</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>stream of FluxRecord</returns>
public async IAsyncEnumerable<FluxRecord> InvokeScriptEnumerableAsync(string scriptId,
Dictionary<string, object> bindParams = default,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var requestMessage = CreateRequest(scriptId, bindParams);
await foreach (var record in QueryEnumerable(requestMessage, it => it, cancellationToken)
.ConfigureAwait(false))
yield return record;
}
/// <summary>
/// Invoke a script and return result as a stream of Measurement.
/// </summary>
/// <param name="scriptId">The ID of the script to invoke. (required)</param>
/// <param name="bindParams">Represent key/value pairs parameters to be injected into script</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>stream of Measurement</returns>
public async IAsyncEnumerable<T> InvokeScriptMeasurementsEnumerableAsync<T>(string scriptId,
Dictionary<string, object> bindParams = default,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var requestMessage = CreateRequest(scriptId, bindParams);
await foreach (var record in QueryEnumerable(requestMessage, it => Mapper.ConvertToEntity<T>(it),
cancellationToken).ConfigureAwait(false))
yield return record;
}
protected override void BeforeIntercept(RestRequest request)
{
_service.Configuration.ApiClient.BeforeIntercept(request);
}
protected override T AfterIntercept<T>(int statusCode, Func<IEnumerable<HeaderParameter>> headers, T body)
{
return _service.Configuration.ApiClient.AfterIntercept(statusCode, headers, body);
}
private Task InvokeScript(string scriptId, FluxCsvParser.IFluxResponseConsumer consumer,
Action<Exception> onError = null, Action onComplete = null, Dictionary<string, object> bindParams = default,
CancellationToken cancellationToken = default)
{
Arguments.CheckNotNull(scriptId, nameof(scriptId));
Arguments.CheckNotNull(consumer, nameof(consumer));
var requestMessage = CreateRequest(scriptId, bindParams);
return Query(requestMessage, consumer, onError ?? ErrorConsumer, onComplete ?? EmptyAction,
cancellationToken);
}
private RestRequest CreateRequest(string scriptId,
Dictionary<string, object> bindParams = default)
{
Arguments.CheckNonEmptyString(scriptId, nameof(scriptId));
return _service
.PostScriptsIDInvokeWithRestRequest(scriptId, new ScriptInvocationParams(bindParams));
}
}
}