22
22
using Microsoft . Extensions . Logging ;
23
23
using IHttpClientFactory = ksqlDB . RestApi . Client . KSql . RestApi . Http . IHttpClientFactory ;
24
24
using ksqlDb . RestApi . Client . KSql . RestApi . Statements . Providers ;
25
+ using ksqlDB . RestApi . Client . KSql . Query . Context ;
25
26
26
27
namespace ksqlDB . RestApi . Client . KSql . RestApi ;
27
28
29
+ /// <summary>
30
+ /// Represents a client for interacting with the KSQL REST API.
31
+ /// </summary>
28
32
public class KSqlDbRestApiClient : IKSqlDbRestApiClient
29
33
{
30
34
private readonly EntityProvider entityProvider = new ( ) ;
31
35
private readonly IHttpClientFactory httpClientFactory ;
32
36
private readonly ModelBuilder modelBuilder ;
37
+ private readonly KSqlDBRestApiClientOptions clientOptions ;
33
38
private readonly StatementGenerator statementGenerator ;
34
39
private readonly ILogger ? logger ;
35
40
41
+ /// <summary>
42
+ /// Initializes a new instance of the <see cref="KSqlDbRestApiClient"/> class.
43
+ /// </summary>
44
+ /// <param name="httpClientFactory">The factory to create <see cref="HttpClient"/> instances.</param>
45
+ /// <param name="loggerFactory">The factory to create <see cref="ILogger"/> instances. This parameter is optional.</param>
46
+ /// <exception cref="ArgumentNullException">Thrown when <paramref name="httpClientFactory"/> is null.</exception>
36
47
public KSqlDbRestApiClient ( IHttpClientFactory httpClientFactory , ILoggerFactory ? loggerFactory = null )
37
48
: this ( httpClientFactory , new ModelBuilder ( ) , loggerFactory )
38
49
{
39
50
}
40
51
52
+ /// <summary>
53
+ /// Initializes a new instance of the <see cref="KSqlDbRestApiClient"/> class.
54
+ /// </summary>
55
+ /// <param name="httpClientFactory">The factory to create <see cref="HttpClient"/> instances.</param>
56
+ /// <param name="loggerFactory">The factory to create <see cref="ILogger"/> instances. This parameter is optional.</param>
57
+ /// <param name="clientOptions">The options for configuring the KSqlDB REST API client.</param>
58
+ /// <exception cref="ArgumentNullException">Thrown when <paramref name="httpClientFactory"/> is null.</exception>
59
+ public KSqlDbRestApiClient ( IHttpClientFactory httpClientFactory , KSqlDBRestApiClientOptions clientOptions , ILoggerFactory ? loggerFactory = null )
60
+ : this ( httpClientFactory , new ModelBuilder ( ) , clientOptions , loggerFactory )
61
+ {
62
+ }
63
+
64
+ /// <summary>
65
+ /// Initializes a new instance of the <see cref="KSqlDbRestApiClient"/> class.
66
+ /// </summary>
67
+ /// <param name="httpClientFactory">The factory to create <see cref="HttpClient"/> instances.</param>
68
+ /// <param name="modelBuilder">The model builder used to construct the models.</param>
69
+ /// <param name="loggerFactory">The factory to create <see cref="ILogger"/> instances. This parameter is optional.</param>
41
70
public KSqlDbRestApiClient ( IHttpClientFactory httpClientFactory , ModelBuilder modelBuilder , ILoggerFactory ? loggerFactory = null )
71
+ : this ( httpClientFactory , modelBuilder , new KSqlDBRestApiClientOptions ( ) , loggerFactory )
72
+ {
73
+ }
74
+
75
+ /// <summary>
76
+ /// Initializes a new instance of the <see cref="KSqlDbRestApiClient"/> class.
77
+ /// </summary>
78
+ /// <param name="httpClientFactory">The factory to create <see cref="HttpClient"/> instances.</param>
79
+ /// <param name="modelBuilder">The model builder used to construct the models.</param>
80
+ /// <param name="loggerFactory">The factory to create <see cref="ILogger"/> instances. This parameter is optional.</param>
81
+ /// <param name="clientOptions">The options for configuring the KSqlDB REST API client.</param>
82
+ /// <exception cref="ArgumentNullException">Thrown when <paramref name="httpClientFactory"/> is null.</exception>
83
+ public KSqlDbRestApiClient ( IHttpClientFactory httpClientFactory , ModelBuilder modelBuilder , KSqlDBRestApiClientOptions clientOptions , ILoggerFactory ? loggerFactory = null )
42
84
{
43
85
this . httpClientFactory = httpClientFactory ?? throw new ArgumentNullException ( nameof ( httpClientFactory ) ) ;
44
86
this . modelBuilder = modelBuilder ;
87
+ this . clientOptions = clientOptions ;
45
88
46
89
if ( loggerFactory != null )
47
90
logger = loggerFactory . CreateLogger ( LoggingCategory . Name ) ;
@@ -191,6 +234,9 @@ public Task<HttpResponseMessage> CreateStreamAsync<T>(EntityCreationMetadata cre
191
234
ArgumentNullException . ThrowIfNull ( creationMetadata ) ;
192
235
#endif
193
236
237
+ if ( creationMetadata . ShouldPluralizeEntityName == null )
238
+ creationMetadata = creationMetadata with { ShouldPluralizeEntityName = clientOptions . ShouldPluralizeFromItemName } ;
239
+
194
240
var ksql = statementGenerator . CreateStream < T > ( creationMetadata , ifNotExists ) ;
195
241
196
242
return ExecuteAsync ( ksql , cancellationToken ) ;
@@ -211,6 +257,9 @@ public Task<HttpResponseMessage> CreateOrReplaceStreamAsync<T>(EntityCreationMet
211
257
ArgumentNullException . ThrowIfNull ( creationMetadata ) ;
212
258
#endif
213
259
260
+ if ( creationMetadata . ShouldPluralizeEntityName == null )
261
+ creationMetadata = creationMetadata with { ShouldPluralizeEntityName = clientOptions . ShouldPluralizeFromItemName } ;
262
+
214
263
var ksql = statementGenerator . CreateOrReplaceStream < T > ( creationMetadata ) ;
215
264
216
265
return ExecuteAsync ( ksql , cancellationToken ) ;
@@ -232,7 +281,12 @@ public Task<HttpResponseMessage> CreateSourceStreamAsync<T>(EntityCreationMetada
232
281
ArgumentNullException . ThrowIfNull ( creationMetadata ) ;
233
282
#endif
234
283
235
- creationMetadata . IsReadOnly = true ;
284
+ if ( creationMetadata . ShouldPluralizeEntityName == null )
285
+ creationMetadata = creationMetadata with
286
+ {
287
+ ShouldPluralizeEntityName = clientOptions . ShouldPluralizeFromItemName ,
288
+ IsReadOnly = true
289
+ } ;
236
290
237
291
var ksql = statementGenerator . CreateStream < T > ( creationMetadata , ifNotExists ) ;
238
292
@@ -255,6 +309,9 @@ public Task<HttpResponseMessage> CreateTableAsync<T>(EntityCreationMetadata crea
255
309
ArgumentNullException . ThrowIfNull ( creationMetadata ) ;
256
310
#endif
257
311
312
+ if ( creationMetadata . ShouldPluralizeEntityName == null )
313
+ creationMetadata = creationMetadata with { ShouldPluralizeEntityName = clientOptions . ShouldPluralizeFromItemName } ;
314
+
258
315
var ksql = statementGenerator . CreateTable < T > ( creationMetadata , ifNotExists ) ;
259
316
260
317
return ExecuteAsync ( ksql , cancellationToken ) ;
@@ -276,7 +333,12 @@ public Task<HttpResponseMessage> CreateSourceTableAsync<T>(EntityCreationMetadat
276
333
ArgumentNullException . ThrowIfNull ( creationMetadata ) ;
277
334
#endif
278
335
279
- creationMetadata . IsReadOnly = true ;
336
+ if ( creationMetadata . ShouldPluralizeEntityName == null )
337
+ creationMetadata = creationMetadata with
338
+ {
339
+ ShouldPluralizeEntityName = clientOptions . ShouldPluralizeFromItemName ,
340
+ IsReadOnly = true
341
+ } ;
280
342
281
343
var ksql = statementGenerator . CreateTable < T > ( creationMetadata , ifNotExists ) ;
282
344
@@ -298,6 +360,9 @@ public Task<HttpResponseMessage> CreateOrReplaceTableAsync<T>(EntityCreationMeta
298
360
ArgumentNullException . ThrowIfNull ( creationMetadata ) ;
299
361
#endif
300
362
363
+ if ( creationMetadata . ShouldPluralizeEntityName == null )
364
+ creationMetadata = creationMetadata with { ShouldPluralizeEntityName = clientOptions . ShouldPluralizeFromItemName } ;
365
+
301
366
var ksql = statementGenerator . CreateOrReplaceTable < T > ( creationMetadata ) ;
302
367
303
368
return ExecuteAsync ( ksql , cancellationToken ) ;
@@ -321,6 +386,7 @@ private Task<HttpResponseMessage> ExecuteAsync(string ksql, CancellationToken ca
321
386
public Task < HttpResponseMessage > CreateTypeAsync < T > ( CancellationToken cancellationToken = default )
322
387
{
323
388
var properties = new TypeProperties ( ) ;
389
+
324
390
return CreateTypeAsync < T > ( properties , cancellationToken ) ;
325
391
}
326
392
@@ -335,7 +401,11 @@ public Task<HttpResponseMessage> CreateTypeAsync<T>(CancellationToken cancellati
335
401
/// <returns>Http response object.</returns>
336
402
public Task < HttpResponseMessage > CreateTypeAsync < T > ( string typeName , CancellationToken cancellationToken = default )
337
403
{
338
- var properties = new TypeProperties { EntityName = typeName } ;
404
+ var properties = new TypeProperties
405
+ {
406
+ EntityName = typeName
407
+ } ;
408
+
339
409
return CreateTypeAsync < T > ( properties , cancellationToken ) ;
340
410
}
341
411
@@ -350,6 +420,9 @@ public Task<HttpResponseMessage> CreateTypeAsync<T>(string typeName, Cancellatio
350
420
/// <returns>Http response object.</returns>
351
421
public Task < HttpResponseMessage > CreateTypeAsync < T > ( TypeProperties properties , CancellationToken cancellationToken = default )
352
422
{
423
+ if ( properties . ShouldPluralizeEntityName == null )
424
+ properties = properties with { ShouldPluralizeEntityName = clientOptions . ShouldPluralizeFromItemName } ;
425
+
353
426
var ksql = new TypeGenerator ( modelBuilder ) . Print < T > ( properties ) ;
354
427
355
428
return ExecuteAsync ( ksql , cancellationToken ) ;
@@ -363,6 +436,9 @@ public Task<HttpResponseMessage> CreateTypeAsync<T>(TypeProperties properties, C
363
436
/// <returns>Http response object.</returns>
364
437
public Task < HttpResponseMessage > DropTypeAsync < T > ( DropTypeProperties dropTypeProperties , CancellationToken cancellationToken = default )
365
438
{
439
+ if ( dropTypeProperties . ShouldPluralizeEntityName == null )
440
+ dropTypeProperties = dropTypeProperties with { ShouldPluralizeEntityName = clientOptions . ShouldPluralizeFromItemName } ;
441
+
366
442
var typeName = entityProvider . GetFormattedName < T > ( dropTypeProperties ) ;
367
443
string dropStatement = StatementTemplates . DropType ( typeName ) ;
368
444
@@ -413,6 +489,10 @@ public Task<HttpResponseMessage> DropTypeIfExistsAsync(string typeName, Cancella
413
489
/// <returns>Http response object.</returns>
414
490
public Task < HttpResponseMessage > InsertIntoAsync < T > ( T entity , InsertProperties ? insertProperties = null , CancellationToken cancellationToken = default )
415
491
{
492
+ insertProperties ??= new InsertProperties ( ) ;
493
+ if ( insertProperties . ShouldPluralizeEntityName == null )
494
+ insertProperties = insertProperties with { ShouldPluralizeEntityName = clientOptions . ShouldPluralizeFromItemName } ;
495
+
416
496
var insertStatement = ToInsertStatement ( entity , insertProperties ) ;
417
497
418
498
var httpResponseMessage = ExecuteStatementAsync ( insertStatement , cancellationToken ) ;
@@ -429,6 +509,10 @@ public Task<HttpResponseMessage> InsertIntoAsync<T>(T entity, InsertProperties?
429
509
/// <returns>A <see cref="KSqlDbStatement"/></returns>
430
510
public KSqlDbStatement ToInsertStatement < T > ( InsertValues < T > insertValues , InsertProperties ? insertProperties = null )
431
511
{
512
+ insertProperties ??= new InsertProperties ( ) ;
513
+ if ( insertProperties . ShouldPluralizeEntityName == null )
514
+ insertProperties = insertProperties with { ShouldPluralizeEntityName = clientOptions . ShouldPluralizeFromItemName } ;
515
+
432
516
var insertStatement = new CreateInsert ( modelBuilder ) . Generate ( insertValues , insertProperties ) ;
433
517
434
518
return new KSqlDbStatement ( insertStatement ) ;
@@ -443,6 +527,10 @@ public KSqlDbStatement ToInsertStatement<T>(InsertValues<T> insertValues, Insert
443
527
/// <returns>A <see cref="KSqlDbStatement"/></returns>
444
528
public KSqlDbStatement ToInsertStatement < T > ( T entity , InsertProperties ? insertProperties = null )
445
529
{
530
+ insertProperties ??= new InsertProperties ( ) ;
531
+ if ( insertProperties . ShouldPluralizeEntityName == null )
532
+ insertProperties = insertProperties with { ShouldPluralizeEntityName = clientOptions . ShouldPluralizeFromItemName } ;
533
+
446
534
var insertStatement = new CreateInsert ( modelBuilder ) . Generate ( entity , insertProperties ) ;
447
535
448
536
return new KSqlDbStatement ( insertStatement ) ;
@@ -746,6 +834,9 @@ public Task<HttpResponseMessage> DropStreamAsync(string streamName, Cancellation
746
834
/// <returns></returns>
747
835
public Task < HttpResponseMessage > DropTableAsync < T > ( DropFromItemProperties dropFromItemProperties , CancellationToken cancellationToken = default )
748
836
{
837
+ if ( dropFromItemProperties . ShouldPluralizeEntityName == null )
838
+ dropFromItemProperties = dropFromItemProperties with { ShouldPluralizeEntityName = clientOptions . ShouldPluralizeFromItemName } ;
839
+
749
840
var tableName = entityProvider . GetFormattedName < T > ( dropFromItemProperties ) ;
750
841
string dropStatement = StatementTemplates . DropTable ( tableName , dropFromItemProperties . UseIfExistsClause , dropFromItemProperties . DeleteTopic ) ;
751
842
0 commit comments