Skip to content

Commit 32783a4

Browse files
feat(csharp): add TransformationOptions for ingestion transporter configuration (generated)
algolia/api-clients-automation#6292 Co-authored-by: algolia-bot <accounts+algolia-api-client-bot@algolia.com> Co-authored-by: Mario-Alexandru Dan <marioalexandrudan@gmail.com>
1 parent ab5c5e2 commit 32783a4

3 files changed

Lines changed: 151 additions & 14 deletions

File tree

algoliasearch/Clients/SearchClient.cs

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5146,6 +5146,7 @@ public partial class SearchClient : ISearchClient
51465146
internal HttpTransport _transport;
51475147
private readonly ILogger<SearchClient> _logger;
51485148
private IIngestionClient _ingestionTransporter;
5149+
private ILoggerFactory _loggerFactory;
51495150

51505151
/// <summary>
51515152
/// Create a new Search client for the given appID and apiKey.
@@ -5216,6 +5217,8 @@ public SearchClient(
52165217
"WARNING: DEBUG level logging is enabled. This logs full request/response bodies which may contain sensitive data. Only use in local development."
52175218
);
52185219
}
5220+
5221+
_loggerFactory = factory;
52195222
}
52205223

52215224
/// <summary>
@@ -5229,27 +5232,64 @@ public void SetClientApiKey(string apiKey)
52295232
}
52305233

52315234
/// <summary>
5232-
/// Sets the region of the transformation pipeline. This is required to be called
5233-
/// if you wish to leverage the transformation pipeline (via the *WithTransformation methods).
5235+
/// Sets (or replaces) the ingestion transporter used by *WithTransformation helpers.
5236+
/// The ingestion transporter is created using the region from <paramref name="transformationOptions"/>
5237+
/// and Ingestion API defaults; only fields explicitly set on <paramref name="transformationOptions"/>
5238+
/// replace those defaults.
5239+
/// See https://www.algolia.com/doc/libraries/sdk/methods/ingestion
5240+
/// </summary>
5241+
/// <param name="transformationOptions">The transformation options including region and optional ingestion transporter overrides.</param>
5242+
public void SetTransformationOptions(TransformationOptions transformationOptions)
5243+
{
5244+
if (transformationOptions == null)
5245+
throw new ArgumentNullException(nameof(transformationOptions));
5246+
5247+
var ingestionConfig = new IngestionConfig(
5248+
_transport._algoliaConfig.AppId,
5249+
_transport._algoliaConfig.ApiKey,
5250+
transformationOptions.Region
5251+
);
5252+
5253+
if (transformationOptions.CustomHosts != null)
5254+
ingestionConfig.CustomHosts = transformationOptions.CustomHosts;
5255+
if (transformationOptions.ConnectTimeout.HasValue)
5256+
ingestionConfig.ConnectTimeout = transformationOptions.ConnectTimeout;
5257+
if (transformationOptions.ReadTimeout.HasValue)
5258+
ingestionConfig.ReadTimeout = transformationOptions.ReadTimeout;
5259+
if (transformationOptions.WriteTimeout.HasValue)
5260+
ingestionConfig.WriteTimeout = transformationOptions.WriteTimeout;
5261+
if (transformationOptions.Compression.HasValue)
5262+
ingestionConfig.Compression = transformationOptions.Compression.Value;
5263+
if (transformationOptions.DefaultHeaders != null)
5264+
{
5265+
foreach (var kvp in transformationOptions.DefaultHeaders)
5266+
ingestionConfig.DefaultHeaders[kvp.Key] = kvp.Value;
5267+
}
5268+
5269+
_ingestionTransporter = new IngestionClient(ingestionConfig, _loggerFactory);
5270+
}
5271+
5272+
/// <summary>
5273+
/// Sets the region of the transformation pipeline.
52345274
/// </summary>
52355275
/// <param name="region">The region ("us" or "eu")</param>
5236-
/// <param name="factory">Logger factory</param>
5276+
/// <param name="factory">Logger factory.</param>
5277+
[Obsolete(
5278+
"SetTransformationRegion is deprecated. Use SetTransformationOptions instead. "
5279+
+ "See https://www.algolia.com/doc/libraries/sdk/methods/ingestion"
5280+
)]
52375281
public void SetTransformationRegion(string region, ILoggerFactory factory = null)
52385282
{
52395283
if (string.IsNullOrWhiteSpace(region))
5240-
{
52415284
throw new ArgumentException(
52425285
"`region` must be provided when leveraging the transformation pipeline"
52435286
);
5244-
}
52455287

52465288
if (
52475289
string.IsNullOrWhiteSpace(_transport._algoliaConfig.AppId)
52485290
|| string.IsNullOrWhiteSpace(_transport._algoliaConfig.ApiKey)
52495291
)
5250-
{
52515292
throw new ArgumentException("AppId and ApiKey are required for transformation pipeline");
5252-
}
52535293

52545294
_ingestionTransporter = new IngestionClient(
52555295
new IngestionConfig(_transport._algoliaConfig.AppId, _transport._algoliaConfig.ApiKey, region)
@@ -5261,10 +5301,57 @@ public void SetTransformationRegion(string region, ILoggerFactory factory = null
52615301
Compression = _transport._algoliaConfig.Compression,
52625302
CustomHosts = _transport._algoliaConfig.CustomHosts,
52635303
},
5264-
factory
5304+
factory ?? _loggerFactory
52655305
);
52665306
}
52675307

5308+
/// <summary>
5309+
/// Creates a <see cref="SearchClient"/> configured with <see cref="TransformationOptions"/> for use
5310+
/// with <c>*WithTransformation</c> helpers. The ingestion transporter is initialised eagerly using
5311+
/// Ingestion API defaults; set override fields on <see cref="TransformationOptions"/> to change
5312+
/// specific defaults.
5313+
/// See https://www.algolia.com/doc/libraries/sdk/methods/ingestion
5314+
/// </summary>
5315+
/// <param name="appId">Your Algolia application ID.</param>
5316+
/// <param name="apiKey">Your Algolia API key.</param>
5317+
/// <param name="transformationOptions">The transformation options including region and optional ingestion transporter overrides.</param>
5318+
/// <param name="loggerFactory">Logger factory.</param>
5319+
public static SearchClient WithTransformation(
5320+
string appId,
5321+
string apiKey,
5322+
TransformationOptions transformationOptions,
5323+
ILoggerFactory loggerFactory = null
5324+
)
5325+
{
5326+
if (transformationOptions == null)
5327+
throw new ArgumentNullException(nameof(transformationOptions));
5328+
var client = new SearchClient(appId, apiKey, loggerFactory);
5329+
client.SetTransformationOptions(transformationOptions);
5330+
return client;
5331+
}
5332+
5333+
/// <summary>
5334+
/// Creates a <see cref="SearchClient"/> configured with <see cref="TransformationOptions"/> for use
5335+
/// with <c>*WithTransformation</c> helpers, using a custom <see cref="SearchConfig"/> for the search
5336+
/// transporter.
5337+
/// See https://www.algolia.com/doc/libraries/sdk/methods/ingestion
5338+
/// </summary>
5339+
/// <param name="searchConfig">Custom search transporter configuration.</param>
5340+
/// <param name="transformationOptions">The transformation options including region and optional ingestion transporter overrides.</param>
5341+
/// <param name="loggerFactory">Logger factory.</param>
5342+
public static SearchClient WithTransformation(
5343+
SearchConfig searchConfig,
5344+
TransformationOptions transformationOptions,
5345+
ILoggerFactory loggerFactory = null
5346+
)
5347+
{
5348+
if (transformationOptions == null)
5349+
throw new ArgumentNullException(nameof(transformationOptions));
5350+
var client = new SearchClient(searchConfig, loggerFactory);
5351+
client.SetTransformationOptions(transformationOptions);
5352+
return client;
5353+
}
5354+
52685355
/// <inheritdoc />
52695356
public async Task<AddApiKeyResponse> AddApiKeyAsync(
52705357
ApiKey apiKey,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Algolia.Search.Models.Common;
4+
using Algolia.Search.Transport;
5+
6+
namespace Algolia.Search.Clients;
7+
8+
/// <summary>
9+
/// Configuration options for the ingestion transporter used by <c>*WithTransformation</c> helpers.
10+
/// An ingestion transporter is eagerly created using the provided region and Ingestion API defaults
11+
/// (25 s timeouts, region-derived hosts, no compression). Only fields explicitly set here replace
12+
/// the Ingestion API defaults. Credentials are always taken from the parent <see cref="SearchClient"/>.
13+
/// See https://www.algolia.com/doc/libraries/sdk/methods/ingestion
14+
/// </summary>
15+
public class TransformationOptions
16+
{
17+
/// <summary>The ingestion region ("eu" or "us"). Required.</summary>
18+
public string Region { get; }
19+
20+
/// <summary>Override the default ingestion hosts.</summary>
21+
public List<StatefulHost> CustomHosts { get; set; }
22+
23+
/// <summary>Override the default connect timeout (25 s).</summary>
24+
public TimeSpan? ConnectTimeout { get; set; }
25+
26+
/// <summary>Override the default read timeout (25 s).</summary>
27+
public TimeSpan? ReadTimeout { get; set; }
28+
29+
/// <summary>Override the default write timeout (25 s).</summary>
30+
public TimeSpan? WriteTimeout { get; set; }
31+
32+
/// <summary>Override the default compression (none).</summary>
33+
public CompressionType? Compression { get; set; }
34+
35+
/// <summary>Override the default headers.</summary>
36+
public Dictionary<string, string> DefaultHeaders { get; set; }
37+
38+
/// <param name="region">The ingestion region ("eu" or "us"). Required.</param>
39+
/// <exception cref="ArgumentException">Thrown when region is null or whitespace.</exception>
40+
public TransformationOptions(string region)
41+
{
42+
if (string.IsNullOrWhiteSpace(region))
43+
{
44+
throw new ArgumentException(
45+
"region is required in TransformationOptions. See https://www.algolia.com/doc/libraries/sdk/methods/ingestion"
46+
);
47+
}
48+
Region = region;
49+
}
50+
}

algoliasearch/Utils/SearchClientExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ List<BatchResponse> PartialUpdateObjects<T>(
443443
/// <summary>
444444
/// Helper: Similar to the `SaveObjects` method but requires a Push connector to be created first,
445445
/// in order to transform records before indexing them to Algolia.
446-
/// The ingestion region must have been provided at client instantiation.
446+
/// <see cref="TransformationOptions"/> must have been set via <see cref="SearchClient.SetTransformationOptions"/> or <see cref="SearchClient.WithTransformation"/>.
447447
/// </summary>
448448
/// <param name="indexName">The index in which to perform the request.</param>
449449
/// <param name="objects">The list of `objects` to store in the given Algolia `indexName`.</param>
@@ -476,7 +476,7 @@ List<BatchResponse> PartialUpdateObjects<T>(
476476
/// <summary>
477477
/// Helper: Similar to the `PartialUpdateObjects` method but requires a Push connector to be created first,
478478
/// in order to transform records before indexing them to Algolia.
479-
/// The ingestion region must have been provided at client instantiation.
479+
/// <see cref="TransformationOptions"/> must have been set via <see cref="SearchClient.SetTransformationOptions"/> or <see cref="SearchClient.WithTransformation"/>.
480480
/// </summary>
481481
/// <param name="indexName">The index in which to perform the request.</param>
482482
/// <param name="objects">The list of `objects` to update in the given Algolia `indexName`.</param>
@@ -514,7 +514,7 @@ List<BatchResponse> PartialUpdateObjects<T>(
514514
/// <summary>
515515
/// Helper: Similar to the `ReplaceAllObjects` method but requires a Push connector to be created first,
516516
/// in order to transform records before indexing them to Algolia.
517-
/// The ingestion region must have been provided at client instantiation.
517+
/// <see cref="TransformationOptions"/> must have been set via <see cref="SearchClient.SetTransformationOptions"/> or <see cref="SearchClient.WithTransformation"/>.
518518
/// A temporary index is created during this process in order to backup your data.
519519
/// </summary>
520520
/// <param name="indexName">The index in which to perform the request.</param>
@@ -1386,7 +1386,7 @@ public async Task<
13861386
if (_ingestionTransporter == null)
13871387
{
13881388
throw new AlgoliaException(
1389-
"`setTransformationRegion` must have been called before calling this method."
1389+
"TransformationOptions must be set in the client config before calling this method. It defaults to the Ingestion API defaults. See https://www.algolia.com/doc/libraries/sdk/methods/ingestion"
13901390
);
13911391
}
13921392

@@ -1446,7 +1446,7 @@ public async Task<
14461446
if (_ingestionTransporter == null)
14471447
{
14481448
throw new AlgoliaException(
1449-
"`setTransformationRegion` must have been called before calling this method."
1449+
"TransformationOptions must be set in the client config before calling this method. It defaults to the Ingestion API defaults. See https://www.algolia.com/doc/libraries/sdk/methods/ingestion"
14501450
);
14511451
}
14521452

@@ -1510,7 +1510,7 @@ public async Task<ReplaceAllObjectsWithTransformationResponse> ReplaceAllObjects
15101510
if (_ingestionTransporter == null)
15111511
{
15121512
throw new AlgoliaException(
1513-
"`setTransformationRegion` must have been called before calling this method."
1513+
"TransformationOptions must be set in the client config before calling this method. It defaults to the Ingestion API defaults. See https://www.algolia.com/doc/libraries/sdk/methods/ingestion"
15141514
);
15151515
}
15161516

0 commit comments

Comments
 (0)