Skip to content

Add support for dependency injection's IHttpClientFactory.#56

Merged
olsh merged 5 commits into
olsh:masterfrom
AhmedZaki99:patch-dependency-injection
Aug 6, 2024
Merged

Add support for dependency injection's IHttpClientFactory.#56
olsh merged 5 commits into
olsh:masterfrom
AhmedZaki99:patch-dependency-injection

Conversation

@AhmedZaki99
Copy link
Copy Markdown
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;
    }
}

Comment thread src/Todoist.Net/Todoist.Net.csproj Outdated

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done 👍

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Aug 5, 2024

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

olsh commented Aug 6, 2024

Thank you!

@AhmedZaki99 AhmedZaki99 deleted the patch-dependency-injection branch April 27, 2025 02:58
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