Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dependency injection's IHttpClientFactory. #56

Merged
merged 5 commits into from
Aug 6, 2024

Conversation

AhmedZaki99
Copy link
Contributor

Picking up from #54 (Closes #54)

Changes:

  1. Added a constructor overload for TodoistRestClient that takes an HttpClient instance as an argument:
public TodoistRestClient(string token, HttpClient httpClient)
{
    _httpClient = httpClient;

    _httpClient.BaseAddress = new Uri("https://api.todoist.com/sync/v9/");
    if (!string.IsNullOrEmpty(token))
    {
        _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
    }
}
  1. Created an ITodoistClientFactory interface and TodoistClientFactory implementation to inject into the DI container, and use the provided IHttpClientFactory:
public interface ITodoistClientFactory
{
    TodoistClient CreateClient(string token);
}

internal sealed class TodoistClientFactory : ITodoistClientFactory
{
    private readonly IHttpClientFactory _httpClientFactory;

    public TodoistClientFactory(IHttpClientFactory httpClientFactory)
    {
        _httpClientFactory = httpClientFactory;
    }

    public TodoistClient CreateClient(string token)
    {
        var httpClient = _httpClientFactory.CreateClient();
        var todoistRestClient = new TodoistRestClient(token, httpClient);

        return new TodoistClient(todoistRestClient);
    }
}
  1. Added an extension method to inject both the ITodoistClientFactory and IHttpClientFactory:
public static class TodoistServiceCollectionExtensions
{
    public static IServiceCollection AddTodoistClient(this IServiceCollection services)
    {
        services.AddHttpClient();
        services.AddSingleton<ITodoistClientFactory, TodoistClientFactory>();

        return services;
    }
}

@@ -25,6 +25,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
Copy link
Owner

@olsh olsh Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend including the dependency only for the .NET Core version of the library. There's no need to include it for the full framework. Additionally, we should consider lowering the version to the oldest LTS - 6.x.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

Copy link

sonarqubecloud bot commented Aug 5, 2024

@olsh olsh merged commit 74b33a5 into olsh:master Aug 6, 2024
2 checks passed
@olsh
Copy link
Owner

olsh commented Aug 6, 2024

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggestion to add support for dependency injection's IHttpClientFactory.
2 participants