Skip to content

Commit 68551ff

Browse files
authored
refactor(csharp): Playground (#2744)
1 parent 360eb00 commit 68551ff

File tree

16 files changed

+444
-264
lines changed

16 files changed

+444
-264
lines changed

clients/algoliasearch-client-csharp/algoliasearch/Transport/HttpTransport.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ private async Task<TResult> ExecuteRequestAsync<TResult, TData>(HttpMethod metho
190190
}
191191
}
192192

193-
if (_logger.IsEnabled(LogLevel.Debug))
193+
if (_logger.IsEnabled(LogLevel.Error))
194194
{
195-
_logger.LogDebug("Retry strategy failed: {ErrorMessage}", _errorMessage);
195+
_logger.LogError("Retry strategy failed: {ErrorMessage}", _errorMessage);
196196
}
197197

198198
throw new AlgoliaUnreachableHostException("RetryStrategy failed to connect to Algolia. Reason: " + _errorMessage);

playground/csharp/Playground/.env.example

Lines changed: 0 additions & 2 deletions
This file was deleted.

playground/csharp/Playground/Model/Configuration.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ public class Configuration
44
public string AdminApiKey { get; set; }
55
public string SearchApiKey { get; set; }
66
public string MonitoringApiKey { get; set; }
7-
public string MetisAppId { get; set; }
8-
public string MetisApiKey { get; set; }
7+
98
public string DefaultIndexName { get; set; }
109
}

playground/csharp/Playground/Playground.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,4 @@
1919
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0"/>
2020
</ItemGroup>
2121

22-
<ItemGroup>
23-
<None Update=".env">
24-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
25-
</None>
26-
</ItemGroup>
27-
2822
</Project>
Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,44 @@
11
using System.Globalization;
22
using Algolia.Search.Clients;
33
using Algolia.Search.Models.Abtesting;
4+
using Algolia.Utils;
5+
using Microsoft.Extensions.Logging;
46

57
namespace Algolia.Playgrounds;
68

7-
public static class ABTesting
9+
public class ABTestingPlayground : IPlayground
810
{
9-
public static async Task Run(Configuration configuration)
11+
const string DefaultIndex = "test-csharp-new-client";
12+
private readonly AbtestingClient _client;
13+
private readonly Configuration _configuration;
14+
15+
public ABTestingPlayground(Configuration configuration)
16+
{
17+
var loggerFactory = LoggerFactory.Create(i => i.AddFilter("Algolia", LogLevel.Information)
18+
.AddConsole());
19+
var config = new AbtestingConfig(configuration.AppId, configuration.AdminApiKey);
20+
_client = new AbtestingClient(config, loggerFactory);
21+
_configuration = configuration;
22+
}
23+
24+
public async Task Run()
1025
{
11-
Console.WriteLine("------------------------------------");
12-
Console.WriteLine("Starting ABTesting API playground");
13-
Console.WriteLine("------------------------------------");
14-
var client = new AbtestingClient(new AbtestingConfig(configuration.AppId, configuration.AdminApiKey));
26+
PlaygroundHelper.Hello("Starting ABTesting API playground");
1527

16-
var newABTest = await client.AddABTestsAsync(new AddABTestsRequest("A simple A/B Test",
17-
new List<AddABTestsVariant> { new(new AbTestsVariant("test-index", 50)), new(new AbTestsVariant("test-index2", 50)) },
18-
DateTime.UtcNow.AddDays(1d).ToString("o", CultureInfo.InvariantCulture)));
28+
try
29+
{
30+
var newABTest = await _client.AddABTestsAsync(new AddABTestsRequest("A simple A/B Test",
31+
[
32+
new AddABTestsVariant(new AbTestsVariant("test-index", 50)),
33+
new AddABTestsVariant(new AbTestsVariant("test-index2", 50))
34+
],
35+
DateTime.UtcNow.AddDays(1d).ToString("o", CultureInfo.InvariantCulture)));
1936

20-
Console.WriteLine(newABTest.AbTestID);
37+
Console.WriteLine(newABTest.AbTestID);
38+
}
39+
catch (Exception e)
40+
{
41+
Console.WriteLine(e);
42+
}
2143
}
2244
}
Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
using Algolia.Search.Clients;
2+
using Algolia.Utils;
3+
using Microsoft.Extensions.Logging;
24

35
namespace Algolia.Playgrounds;
46

5-
public static class Analytics
7+
public class AnalyticsPlayground : IPlayground
68
{
7-
public static async Task Run(Configuration configuration)
9+
private readonly AnalyticsClient _client;
10+
private readonly Configuration _configuration;
11+
12+
public AnalyticsPlayground(Configuration configuration)
813
{
9-
Console.WriteLine("------------------------------------");
10-
Console.WriteLine("Starting Analytics API playground");
11-
Console.WriteLine("------------------------------------");
12-
var client = new AnalyticsClient(new AnalyticsConfig(configuration.AppId, configuration.AdminApiKey));
14+
var loggerFactory = LoggerFactory.Create(i => i.AddFilter("Algolia", LogLevel.Information)
15+
.AddConsole());
16+
var config = new AnalyticsConfig(configuration.AppId, configuration.AdminApiKey);
17+
_client = new AnalyticsClient(config, loggerFactory);
18+
_configuration = configuration;
19+
}
1320

14-
var shortDateString = DateTime.UtcNow.AddDays(-1).ToString("YYYY-MM-DD");
15-
Console.WriteLine(shortDateString);
16-
var getSearchesCountResponse = await client.GetSearchesCountAsync("test-index", shortDateString);
17-
Console.WriteLine(getSearchesCountResponse.Count);
21+
public async Task Run()
22+
{
23+
PlaygroundHelper.Hello("Starting Analytics API playground");
24+
try
25+
{
26+
var shortDateString = DateTime.UtcNow.AddDays(-1).ToString("YYYY-MM-DD");
27+
Console.WriteLine(shortDateString);
28+
var getSearchesCountResponse = await _client.GetSearchesCountAsync("test-index", shortDateString);
29+
Console.WriteLine(getSearchesCountResponse.Count);
30+
}
31+
catch (Exception)
32+
{
33+
// ignored
34+
}
1835
}
1936
}
Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,60 @@
11
using Algolia.Search.Clients;
22
using Algolia.Search.Models.Ingestion;
3+
using Algolia.Search.Models.Monitoring;
4+
using Algolia.Utils;
5+
using Microsoft.Extensions.Logging;
36

47
namespace Algolia.Playgrounds;
58

6-
public static class Ingestion
9+
public class IngestionPlayground : IPlayground
710
{
8-
public static async Task Run(Configuration configuration)
9-
{
10-
Console.WriteLine("------------------------------------");
11-
Console.WriteLine("Starting Ingestion API playground");
12-
Console.WriteLine("------------------------------------");
13-
var client = new IngestionClient(new IngestionConfig(configuration.AppId, configuration.AdminApiKey, "us"));
11+
private readonly IngestionClient _client;
12+
private readonly Configuration _configuration;
1413

15-
// Get existing JSON source
16-
Console.WriteLine("--- Get existing JSON source `GetSourcesAsync` ---");
17-
var jsonSources = await client.GetSourcesAsync(type: new List<SourceType> { SourceType.Json }).ConfigureAwait(false);
18-
Console.WriteLine(jsonSources.Sources.Count == 0 ? "There is no JSON Source !" : $"There is {jsonSources.Sources.Count} JSON source(s)");
14+
public IngestionPlayground(Configuration configuration)
15+
{
16+
var loggerFactory = LoggerFactory.Create(i => i.AddFilter("Algolia", LogLevel.Information)
17+
.AddConsole());
18+
var config = new IngestionConfig(configuration.AppId, configuration.AdminApiKey, "us");
19+
_client = new IngestionClient(config, loggerFactory);
20+
_configuration = configuration;
21+
}
1922

20-
// Deleting existing JSON source
21-
Console.WriteLine("--- Deleting existing JSON source `DeleteSourceAsync` ---");
22-
foreach (var source in jsonSources.Sources)
23+
public async Task Run()
24+
{
25+
PlaygroundHelper.Hello("Starting Ingestion API playground");
26+
try
2327
{
24-
Console.WriteLine($"Deleting source {source.SourceID}");
25-
var response = await client.DeleteSourceAsync(source.SourceID).ConfigureAwait(false);
26-
Console.WriteLine(response.DeletedAt);
27-
}
28+
// Get existing JSON source
29+
Console.WriteLine("--- Get existing JSON source `GetSourcesAsync` ---");
30+
var jsonSources = await _client.GetSourcesAsync(type: [SourceType.Json]).ConfigureAwait(false);
31+
Console.WriteLine(jsonSources.Sources.Count == 0
32+
? "There is no JSON Source !"
33+
: $"There is {jsonSources.Sources.Count} JSON source(s)");
2834

29-
// Create a new JSON source
30-
Console.WriteLine("--- Create a new source `CreateSourceAsync` ---");
31-
var saved = await client.CreateSourceAsync(new SourceCreate
32-
{
33-
Name = "test-csharp-new-client",
34-
Type = SourceType.Json,
35-
Input = new SourceInput(new SourceJSON("https://test.com/test.json"))
36-
}).ConfigureAwait(false);
35+
// Deleting existing JSON source
36+
Console.WriteLine("--- Deleting existing JSON source `DeleteSourceAsync` ---");
37+
foreach (var source in jsonSources.Sources)
38+
{
39+
Console.WriteLine($"Deleting source {source.SourceID}");
40+
var response = await _client.DeleteSourceAsync(source.SourceID).ConfigureAwait(false);
41+
Console.WriteLine(response.DeletedAt);
42+
}
43+
44+
// Create a new JSON source
45+
Console.WriteLine("--- Create a new source `CreateSourceAsync` ---");
46+
var saved = await _client.CreateSourceAsync(new SourceCreate
47+
{
48+
Name = "test-csharp-new-client",
49+
Type = SourceType.Json,
50+
Input = new SourceInput(new SourceJSON("https://test.com/test.json"))
51+
}).ConfigureAwait(false);
3752

38-
Console.WriteLine(saved.SourceID);
53+
Console.WriteLine(saved.SourceID);
54+
}
55+
catch (Exception)
56+
{
57+
// ignored
58+
}
3959
}
4060
}
Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
11
using Algolia.Search.Clients;
22
using Algolia.Search.Models.Insights;
3+
using Algolia.Utils;
4+
using Microsoft.Extensions.Logging;
35

46
namespace Algolia.Playgrounds;
57

6-
public static class Insights
8+
public class InsightsPlayground : IPlayground
79
{
8-
public static async Task Run(Configuration configuration)
10+
private readonly InsightsClient _client;
11+
private readonly Configuration _configuration;
12+
13+
public InsightsPlayground(Configuration configuration)
14+
{
15+
var loggerFactory = LoggerFactory.Create(i => i.AddFilter("Algolia", LogLevel.Information)
16+
.AddConsole());
17+
var config = new InsightsConfig(configuration.AppId, configuration.AdminApiKey);
18+
_client = new InsightsClient(config, loggerFactory);
19+
_configuration = configuration;
20+
}
21+
22+
public async Task Run()
923
{
10-
Console.WriteLine("------------------------------------");
11-
Console.WriteLine("Starting Insights API playground");
12-
Console.WriteLine("------------------------------------");
13-
var client = new InsightsClient(new InsightsConfig(configuration.AppId, configuration.AdminApiKey));
24+
PlaygroundHelper.Hello("Starting Insights API playground");
1425

15-
Console.WriteLine("--- Push new events `PushEventsAsync` ---");
16-
var response = await client.PushEventsAsync(new InsightsEvents(new List<EventsItems>()
26+
try
1727
{
18-
new(new ConvertedObjectIDs("Buy event", ConversionEvent.Conversion, "test",
19-
new List<string> { "iphone_7", "iphone_8" }, "me"))
20-
})).ConfigureAwait(false);
28+
Console.WriteLine("--- Push new events `PushEventsAsync` ---");
29+
var response = await _client.PushEventsAsync(new InsightsEvents([
30+
new EventsItems(new ConvertedObjectIDs("Buy event", ConversionEvent.Conversion, "test",
31+
["iphone_7", "iphone_8"], "me"))
32+
])).ConfigureAwait(false);
2133

22-
Console.WriteLine(response);
34+
Console.WriteLine(response);
35+
}
36+
catch (Exception e)
37+
{
38+
Console.WriteLine(e);
39+
}
2340
}
24-
25-
2641
}
Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
using System.Globalization;
22
using Algolia.Search.Clients;
3+
using Algolia.Utils;
4+
using Microsoft.Extensions.Logging;
35

46
namespace Algolia.Playgrounds;
57

6-
public static class Monitoring
8+
public class MonitoringPlayground : IPlayground
79
{
8-
public static async Task Run(Configuration configuration)
10+
private readonly MonitoringClient _client;
11+
private readonly Configuration _configuration;
12+
13+
public MonitoringPlayground(Configuration configuration)
14+
{
15+
var loggerFactory = LoggerFactory.Create(i => i.AddFilter("Algolia", LogLevel.Information)
16+
.AddConsole());
17+
var config = new MonitoringConfig(configuration.AppId, configuration.AdminApiKey);
18+
_client = new MonitoringClient(config, loggerFactory);
19+
_configuration = configuration;
20+
}
21+
22+
public async Task Run()
923
{
10-
Console.WriteLine("------------------------------------");
11-
Console.WriteLine("Starting Monitoring API playground");
12-
Console.WriteLine("------------------------------------");
13-
var client = new MonitoringClient(new MonitoringConfig(configuration.AppId, configuration.MonitoringApiKey));
24+
PlaygroundHelper.Hello("Starting Monitoring API playground");
1425

15-
Console.WriteLine("--- Get clusters status `GetStatusAsync` ---");
16-
var response = await client.GetStatusAsync();
17-
Console.WriteLine(string.Join(',', response.Status.Select((x, y) => $"Host: {x.Key} is {(x.Value)}")));
26+
try
27+
{
28+
Console.WriteLine("--- Get clusters status `GetStatusAsync` ---");
29+
var response = await _client.GetStatusAsync();
30+
Console.WriteLine(string.Join(',', response.Status.Select((x, y) => $"Host: {x.Key} is {(x.Value)}")));
1831

19-
Console.WriteLine("--- Get incidents list `GetIncidentsAsync` ---");
20-
var incidentsResponse = await client.GetIncidentsAsync();
32+
Console.WriteLine("--- Get incidents list `GetIncidentsAsync` ---");
33+
var incidentsResponse = await _client.GetIncidentsAsync();
2134

22-
foreach (var incident in incidentsResponse.Incidents)
35+
foreach (var incident in incidentsResponse.Incidents)
36+
{
37+
Console.WriteLine(
38+
$"{incident.Key}: {Environment.NewLine}- {string.Join($"{Environment.NewLine} -", incident.Value.Select(inner => $" {DateTimeOffset.FromUnixTimeMilliseconds(inner.T.Value).ToString(CultureInfo.InvariantCulture)} - {inner.V.Title}"))} {Environment.NewLine}");
39+
}
40+
}
41+
catch (Exception e)
2342
{
24-
Console.WriteLine(
25-
$"{incident.Key}: {Environment.NewLine}- {string.Join($"{Environment.NewLine} -", incident.Value.Select(inner => $" {DateTimeOffset.FromUnixTimeMilliseconds(inner.T.Value).ToString(CultureInfo.InvariantCulture)} - {inner.V.Title}"))} {Environment.NewLine}");
43+
Console.WriteLine(e);
2644
}
2745
}
2846
}
Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
using Algolia.Search.Clients;
2+
using Algolia.Utils;
3+
using Microsoft.Extensions.Logging;
24

35
namespace Algolia.Playgrounds;
46

5-
public static class Personalization
7+
public class PersonalizationPlayground : IPlayground
68
{
7-
public static async Task Run(Configuration configuration)
9+
private readonly PersonalizationClient _client;
10+
private readonly Configuration _configuration;
11+
12+
public PersonalizationPlayground(Configuration configuration)
813
{
9-
Console.WriteLine("------------------------------------");
10-
Console.WriteLine("Starting Personalization API playground");
11-
Console.WriteLine("------------------------------------");
12-
var client = new PersonalizationClient(new PersonalizationConfig(configuration.AppId, configuration.AdminApiKey, "us"));
14+
var loggerFactory = LoggerFactory.Create(i => i.AddFilter("Algolia", LogLevel.Information)
15+
.AddConsole());
16+
var config = new PersonalizationConfig(configuration.AppId, configuration.AdminApiKey, "us");
17+
_client = new PersonalizationClient(config, loggerFactory);
18+
_configuration = configuration;
19+
}
20+
21+
public async Task Run()
22+
{
23+
PlaygroundHelper.Hello("Starting Personalization API playground");
24+
25+
try
26+
{
27+
}
28+
catch (Exception e)
29+
{
30+
Console.WriteLine(e);
31+
}
1332
}
1433
}

0 commit comments

Comments
 (0)