Skip to content

Commit

Permalink
Merge #493
Browse files Browse the repository at this point in the history
493: Simplify construction of MeilisearchClient in DI r=ahmednfwela a=ahmednfwela

# Pull Request

## Related issue
Fixes part of #481, which is the inability to use `.AddHttpMessageHandler<MeilisearchMessageHandler>()`

## What does this PR do?
- Adds default constructor for `MeilisearchMessageHandler`
- Refactor `MeilisearchClient` constructors to call one another, instead of redefining the logic in both.

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Ahmed Fwela <[email protected]>
Co-authored-by: Ahmed Fwela <[email protected]>
  • Loading branch information
3 people committed Oct 25, 2023
2 parents 946a4c3 + ab293a5 commit d0193c4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
7 changes: 1 addition & 6 deletions src/Meilisearch/MeilisearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@ public class MeilisearchClient
/// </summary>
/// <param name="url">URL corresponding to Meilisearch server.</param>
/// <param name="apiKey">API Key to connect to the Meilisearch server.</param>
public MeilisearchClient(string url, string apiKey = default)
public MeilisearchClient(string url, string apiKey = default) : this(new HttpClient(new MeilisearchMessageHandler(new HttpClientHandler())) { BaseAddress = url.ToSafeUri() }, apiKey)
{
_http = new HttpClient(new MeilisearchMessageHandler(new HttpClientHandler())) { BaseAddress = url.ToSafeUri() };
_http.AddApiKeyToHeader(apiKey);
_http.AddDefaultUserAgent();
_taskEndpoint = null;
ApiKey = apiKey;
}

/// <summary>
Expand Down
9 changes: 9 additions & 0 deletions src/Meilisearch/MeilisearchMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ namespace Meilisearch
/// </summary>
public class MeilisearchMessageHandler : DelegatingHandler
{

/// <summary>
/// Initializes a new instance of the <see cref="MeilisearchMessageHandler"/> class.
/// Default message handler for Meilisearch API.
/// </summary>
public MeilisearchMessageHandler()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MeilisearchMessageHandler"/> class.
/// Default message handler for Meilisearch API.
Expand Down

0 comments on commit d0193c4

Please sign in to comment.