Skip to content

Commit

Permalink
refactor(csharp): move, tiny additions and cleanings (#2740)
Browse files Browse the repository at this point in the history
  • Loading branch information
morganleroi authored Feb 20, 2024
1 parent 34b55ad commit d86c75f
Show file tree
Hide file tree
Showing 27 changed files with 280 additions and 331 deletions.
91 changes: 24 additions & 67 deletions clients/algoliasearch-client-csharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

<p align="center">
<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>
<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>
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="Licence"></img></a>
</p>
</p>

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

## ✨ Features

* Targets .NET Standard:
* `.NET Standard 1.3` to `.NET Standard 2.1`.
* Targets .NET Standard: `.NET Standard 2.0` or `.NET Standard 2.1`.
* 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).
* Asynchronous and synchronous methods to interact with Algolia's API
* Thread-safe clients
* Typed requests and responses
* Injectable HTTP client
* Asynchronous and synchronous methods to interact with Algolia's API.
* Thread-safe clients.
* No external dependencies.
* Typed requests and responses.
* Injectable HTTP client.
* Retry strategy & Helpers.

**Migration note for v7.x**
> 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/).
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.

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/).

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

In 30 seconds, this quick start tutorial will show you how to index and search objects.

#### Initialize the cient

To start, you need to initialize the client. To do this, you need your **Application ID** and **API Key**.
You can find both on [your Algolia account](https://www.algolia.com/api-keys).

```csharp
SearchClient client = new SearchClient("YourApplicationID", "YourAPIKey");
SearchIndex index = client.InitIndex("your_index_name");
```

#### Push data

Without any prior configuration, you can start indexing contacts in the contacts index using the following code:

```csharp
public class Contact
{
public string ObjectID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}

SearchIndex index = client.InitIndex("contacts");

index.SaveObject(new Contact
{
ObjectID = "ID1",
Name = "Jimmie",
Age = 30
});
```

#### Search

You can now search for contacts by `firstname`, `lastname`, `company`, etc. (even with typos):

```csharp

// Synchronous
index.Search<Contact>(new Query { "jimmie" });

// Asynchronous
await index.SearchAsync<Contact>(new Query { "jimmie" });
```

For full documentation, visit the **[Algolia .NET API Client documentation](https://www.algolia.com/doc/api-client/getting-started/install/csharp/)**.
#### ASP.NET
If you're using ASP.NET, checkout the [following tutorial](https://www.algolia.com/doc/api-client/getting-started/tutorials/asp.net/csharp/).

## ❓ Troubleshooting
### Documentation, Guides & API Reference

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.
You will find all you need to get started in our API Client Automation [documentation](https://api-clients-automation.netlify.app/docs/clients/csharp/).

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

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

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.
## ❓ Troubleshooting
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.

## 📄 License
Algolia .NET API Client is an open-sourced software licensed under the [MIT license](LICENSE.md).
## 📄 License
Algolia .NET API Client is an open-sourced software licensed under the [MIT license](LICENSE.md).
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Algolia.Search.Serializer;
using Algolia.Search.Transport;
using Algolia.Search.Utils;
using Microsoft.Extensions.Logging;

namespace Algolia.Search.Clients
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AlgoliaApiException : Exception
public int HttpErrorCode { get; set; }

/// <summary>
/// Ctor with error code and message
/// Create a new AlgoliaAPIException
/// </summary>
/// <param name="message"></param>
/// <param name="httpErrorCode"></param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Algolia.Search.Exceptions;

/// <summary>
/// Algolia exception.
/// Exception thrown when an error occurs in the Algolia client.
/// </summary>
public class AlgoliaException : Exception
{
Expand All @@ -14,4 +14,14 @@ public class AlgoliaException : Exception
public AlgoliaException(string message) : base(message)
{
}

/// <summary>
/// Create a new Algolia exception, with an inner exception.
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
public AlgoliaException(string message, Exception inner)
: base(message, inner)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Algolia.Search.Http;

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

if (request.Body != null)
if (request.Body != null && httpRequestMessage.Content != null)
{
httpRequestMessage.Content.Headers.Clear();
httpRequestMessage.Content.Headers.Fill(request);
Expand Down Expand Up @@ -100,9 +100,9 @@ public async Task<AlgoliaHttpResponse> SendRequestAsync(Request request, TimeSpa
}
catch (TimeoutException ex)
{
if (_logger.IsEnabled(LogLevel.Debug))
if (_logger.IsEnabled(LogLevel.Warning))
{
_logger.LogDebug(ex, "Timeout while sending request");
_logger.LogWarning(ex, "Timeout while sending request");
}

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

return new AlgoliaHttpResponse { IsNetworkError = true, Error = ex.Message };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public AlgoliaUserAgent(string clientName)
/// <summary>
/// Add a new segment to the user-agent header
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="key">The segment key</param>
/// <param name="value">The segment value. Will be wrapped in parenthesis</param>
/// <exception cref="ArgumentException"></exception>
public void AddSegment(string key, string value)
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,24 @@ internal static class HttpRequestHeadersExtensions
/// <param name="headers"></param>
/// <param name="dictionary"></param>
/// <returns></returns>
internal static HttpRequestHeaders Fill(this HttpRequestHeaders headers, IDictionary<string, string> dictionary)
internal static void Fill(this HttpRequestHeaders headers, IDictionary<string, string> dictionary)
{
foreach (var header in dictionary)
{
headers.TryAddWithoutValidation(header.Key, header.Value);
}

return headers;
}

/// <summary>
/// Extension method to easily fill HttpContentHeaders with the Request object
/// </summary>
/// <param name="headers"></param>
/// <param name="request"></param>
internal static HttpContentHeaders Fill(this HttpContentHeaders headers, Request request)
internal static void Fill(this HttpContentHeaders headers, Request request)
{
if (request.Body == null)
{
return headers;
return;
}

headers.Add(Defaults.ContentType, Defaults.ApplicationJson);
Expand All @@ -40,7 +38,5 @@ internal static HttpContentHeaders Fill(this HttpContentHeaders headers, Request
{
headers.ContentEncoding.Add(Defaults.GzipEncoding);
}

return headers;
}
}
Loading

0 comments on commit d86c75f

Please sign in to comment.