Skip to content

Commit d86c75f

Browse files
authored
refactor(csharp): move, tiny additions and cleanings (#2740)
1 parent 34b55ad commit d86c75f

27 files changed

+280
-331
lines changed

clients/algoliasearch-client-csharp/README.md

Lines changed: 24 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88

99
<p align="center">
1010
<a href="https://www.nuget.org/packages/Algolia.Search/"><img src="https://img.shields.io/nuget/v/Algolia.Search.svg?style=flat-square" alt="Nuget"></img></a>
11-
<a href="https://circleci.com/gh/algolia/algoliasearch-client-csharp"><img src="https://circleci.com/gh/algolia/algoliasearch-client-csharp.svg?style=shield" alt="CircleCI"></img></a>
1211
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="Licence"></img></a>
1312
</p>
14-
</p>
1513

1614
<p align="center">
17-
<a href="https://www.algolia.com/doc/api-client/getting-started/install/csharp/" target="_blank">Documentation</a> •
15+
<a href="https://api-clients-automation.netlify.app/docs/clients/csharp/" target="_blank">Documentation</a> •
1816
<a href="https://discourse.algolia.com" target="_blank">Community Forum</a> •
1917
<a href="http://stackoverflow.com/questions/tagged/algolia" target="_blank">Stack Overflow</a> •
2018
<a href="https://github.com/algolia/algoliasearch-client-csharp/issues" target="_blank">Report a bug</a> •
@@ -24,13 +22,21 @@
2422

2523
## ✨ Features
2624

27-
* Targets .NET Standard:
28-
* `.NET Standard 1.3` to `.NET Standard 2.1`.
25+
* Targets .NET Standard: `.NET Standard 2.0` or `.NET Standard 2.1`.
2926
* For more details about supported .NET implementations, please see .NET Standard official [page](https://docs.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-2-1).
30-
* Asynchronous and synchronous methods to interact with Algolia's API
31-
* Thread-safe clients
32-
* Typed requests and responses
33-
* Injectable HTTP client
27+
* Asynchronous and synchronous methods to interact with Algolia's API.
28+
* Thread-safe clients.
29+
* No external dependencies.
30+
* Typed requests and responses.
31+
* Injectable HTTP client.
32+
* Retry strategy & Helpers.
33+
34+
**Migration note for v7.x**
35+
> In February 2024, we released v7 of our .NET client. If you are using version 6.x of the client, read the [migration guide to version 7.x](https://www.algolia.com/doc/api-client/getting-started/upgrade-guides/csharp/).
36+
37+
This version has been completely rewritten and is part of the [Api Automation initiative](https://github.com/algolia/api-clients-automation), meaning that models and routes are generated from the Algolia APIs specifications.
38+
39+
This version introduces breaking changes. A document has been created to help you migrate from v6 to v7. You can find it [here](https://api-clients-automation.netlify.app/docs/clients/migration-guides/).
3440

3541
**Migration note from v5.x to v6.x**
3642
>
@@ -51,66 +57,17 @@ or with the `Nuget Package Manager Console`:
5157
Install-Package Algolia.Search
5258
```
5359

54-
In 30 seconds, this quick start tutorial will show you how to index and search objects.
55-
56-
#### Initialize the cient
57-
58-
To start, you need to initialize the client. To do this, you need your **Application ID** and **API Key**.
59-
You can find both on [your Algolia account](https://www.algolia.com/api-keys).
60-
61-
```csharp
62-
SearchClient client = new SearchClient("YourApplicationID", "YourAPIKey");
63-
SearchIndex index = client.InitIndex("your_index_name");
64-
```
65-
66-
#### Push data
67-
68-
Without any prior configuration, you can start indexing contacts in the contacts index using the following code:
69-
70-
```csharp
71-
public class Contact
72-
{
73-
public string ObjectID { get; set; }
74-
public string Name { get; set; }
75-
public int Age { get; set; }
76-
}
77-
78-
SearchIndex index = client.InitIndex("contacts");
79-
80-
index.SaveObject(new Contact
81-
{
82-
ObjectID = "ID1",
83-
Name = "Jimmie",
84-
Age = 30
85-
});
86-
```
87-
88-
#### Search
89-
90-
You can now search for contacts by `firstname`, `lastname`, `company`, etc. (even with typos):
91-
92-
```csharp
93-
94-
// Synchronous
95-
index.Search<Contact>(new Query { "jimmie" });
96-
97-
// Asynchronous
98-
await index.SearchAsync<Contact>(new Query { "jimmie" });
99-
```
100-
101-
For full documentation, visit the **[Algolia .NET API Client documentation](https://www.algolia.com/doc/api-client/getting-started/install/csharp/)**.
102-
103-
#### ASP.NET
104-
If you're using ASP.NET, checkout the [following tutorial](https://www.algolia.com/doc/api-client/getting-started/tutorials/asp.net/csharp/).
105-
106-
## ❓ Troubleshooting
60+
### Documentation, Guides & API Reference
10761

108-
Encountering an issue? Before reaching out to support, we recommend heading to our [FAQ](https://www.algolia.com/doc/api-client/troubleshooting/faq/csharp/) where you will find answers for the most common issues and gotchas with the client.
62+
You will find all you need to get started in our API Client Automation [documentation](https://api-clients-automation.netlify.app/docs/clients/csharp/).
10963

64+
For full documentation, visit the **[Algolia .NET API Client documentation](https://www.algolia.com/doc/api-client/getting-started/install/csharp/)**.
11065

111-
## Use the Dockerfile
66+
#### ASP.NET
67+
If you're using ASP.NET, checkout the [following tutorial](https://www.algolia.com/doc/api-client/getting-started/tutorials/asp.net/csharp/).
11268

113-
If you want to contribute to this project without installing all its dependencies, you can use our Docker image. Please check our [dedicated guide](DOCKER_README.md) to learn more.
69+
## ❓ Troubleshooting
70+
Encountering an issue? Before reaching out to support, we recommend heading to our [FAQ](https://www.algolia.com/doc/api-client/troubleshooting/faq/csharp/) where you will find answers for the most common issues and gotchas with the client.
11471

115-
## 📄 License
116-
Algolia .NET API Client is an open-sourced software licensed under the [MIT license](LICENSE.md).
72+
## 📄 License
73+
Algolia .NET API Client is an open-sourced software licensed under the [MIT license](LICENSE.md).

clients/algoliasearch-client-csharp/algoliasearch/Clients/AlgoliaConfig.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Algolia.Search.Serializer;
66
using Algolia.Search.Transport;
77
using Algolia.Search.Utils;
8-
using Microsoft.Extensions.Logging;
98

109
namespace Algolia.Search.Clients
1110
{

clients/algoliasearch-client-csharp/algoliasearch/Exceptions/AlgoliaApiException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class AlgoliaApiException : Exception
1313
public int HttpErrorCode { get; set; }
1414

1515
/// <summary>
16-
/// Ctor with error code and message
16+
/// Create a new AlgoliaAPIException
1717
/// </summary>
1818
/// <param name="message"></param>
1919
/// <param name="httpErrorCode"></param>

clients/algoliasearch-client-csharp/algoliasearch/Exceptions/AlgoliaException.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Algolia.Search.Exceptions;
44

55
/// <summary>
6-
/// Algolia exception.
6+
/// Exception thrown when an error occurs in the Algolia client.
77
/// </summary>
88
public class AlgoliaException : Exception
99
{
@@ -14,4 +14,14 @@ public class AlgoliaException : Exception
1414
public AlgoliaException(string message) : base(message)
1515
{
1616
}
17+
18+
/// <summary>
19+
/// Create a new Algolia exception, with an inner exception.
20+
/// </summary>
21+
/// <param name="message"></param>
22+
/// <param name="inner"></param>
23+
public AlgoliaException(string message, Exception inner)
24+
: base(message, inner)
25+
{
26+
}
1727
}

clients/algoliasearch-client-csharp/algoliasearch/Http/AlgoliaHttpRequester.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Algolia.Search.Http;
1111

1212
/// <summary>
1313
/// Algolia's HTTP requester
14-
/// You can inject your own by the SearchClient or Analytics Client
14+
/// You can inject your own by implementing IHttpRequester
1515
/// </summary>
1616
internal class AlgoliaHttpRequester : IHttpRequester
1717
{
@@ -61,7 +61,7 @@ public async Task<AlgoliaHttpResponse> SendRequestAsync(Request request, TimeSpa
6161
Content = request.Body != null ? new StreamContent(request.Body) : null
6262
};
6363

64-
if (request.Body != null)
64+
if (request.Body != null && httpRequestMessage.Content != null)
6565
{
6666
httpRequestMessage.Content.Headers.Clear();
6767
httpRequestMessage.Content.Headers.Fill(request);
@@ -100,9 +100,9 @@ public async Task<AlgoliaHttpResponse> SendRequestAsync(Request request, TimeSpa
100100
}
101101
catch (TimeoutException ex)
102102
{
103-
if (_logger.IsEnabled(LogLevel.Debug))
103+
if (_logger.IsEnabled(LogLevel.Warning))
104104
{
105-
_logger.LogDebug(ex, "Timeout while sending request");
105+
_logger.LogWarning(ex, "Timeout while sending request");
106106
}
107107

108108
return new AlgoliaHttpResponse { IsTimedOut = true, Error = ex.ToString() };
@@ -111,9 +111,9 @@ public async Task<AlgoliaHttpResponse> SendRequestAsync(Request request, TimeSpa
111111
{
112112
// HttpRequestException is thrown when an underlying issue happened such as
113113
// network connectivity, DNS failure, server certificate validation.
114-
if (_logger.IsEnabled(LogLevel.Debug))
114+
if (_logger.IsEnabled(LogLevel.Error))
115115
{
116-
_logger.LogDebug(ex, "Error while sending request");
116+
_logger.LogError(ex, "Error while sending request {Request}", request);
117117
}
118118

119119
return new AlgoliaHttpResponse { IsNetworkError = true, Error = ex.Message };

clients/algoliasearch-client-csharp/algoliasearch/Http/AlgoliaUserAgent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public AlgoliaUserAgent(string clientName)
3434
/// <summary>
3535
/// Add a new segment to the user-agent header
3636
/// </summary>
37-
/// <param name="key"></param>
38-
/// <param name="value"></param>
37+
/// <param name="key">The segment key</param>
38+
/// <param name="value">The segment value. Will be wrapped in parenthesis</param>
3939
/// <exception cref="ArgumentException"></exception>
4040
public void AddSegment(string key, string value)
4141
{

clients/algoliasearch-client-csharp/algoliasearch/Http/ClientUtils.cs

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

clients/algoliasearch-client-csharp/algoliasearch/Http/HttpRequestHeadersExtensions.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,24 @@ internal static class HttpRequestHeadersExtensions
1212
/// <param name="headers"></param>
1313
/// <param name="dictionary"></param>
1414
/// <returns></returns>
15-
internal static HttpRequestHeaders Fill(this HttpRequestHeaders headers, IDictionary<string, string> dictionary)
15+
internal static void Fill(this HttpRequestHeaders headers, IDictionary<string, string> dictionary)
1616
{
1717
foreach (var header in dictionary)
1818
{
1919
headers.TryAddWithoutValidation(header.Key, header.Value);
2020
}
21-
22-
return headers;
2321
}
2422

2523
/// <summary>
2624
/// Extension method to easily fill HttpContentHeaders with the Request object
2725
/// </summary>
2826
/// <param name="headers"></param>
2927
/// <param name="request"></param>
30-
internal static HttpContentHeaders Fill(this HttpContentHeaders headers, Request request)
28+
internal static void Fill(this HttpContentHeaders headers, Request request)
3129
{
3230
if (request.Body == null)
3331
{
34-
return headers;
32+
return;
3533
}
3634

3735
headers.Add(Defaults.ContentType, Defaults.ApplicationJson);
@@ -40,7 +38,5 @@ internal static HttpContentHeaders Fill(this HttpContentHeaders headers, Request
4038
{
4139
headers.ContentEncoding.Add(Defaults.GzipEncoding);
4240
}
43-
44-
return headers;
4541
}
4642
}

0 commit comments

Comments
 (0)