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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Todoist.Net/Extensions/TodoistServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if NETSTANDARD2_0

using Microsoft.Extensions.DependencyInjection;

namespace Todoist.Net.Extensions
{
/// <summary>
/// Extension methods for setting up todoist client services in an <see cref="IServiceCollection" />.
/// </summary>
public static class TodoistServiceCollectionExtensions
{
/// <summary>
/// Adds todoist client services to the specified <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <returns>The <see cref="IServiceCollection" /> so that additional calls can be chained.</returns>
public static IServiceCollection AddTodoistClient(this IServiceCollection services)
{
services.AddHttpClient();
services.AddSingleton<ITodoistClientFactory, TodoistClientFactory>();

return services;
}
}
}

#endif
15 changes: 15 additions & 0 deletions src/Todoist.Net/ITodoistClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Todoist.Net
{
/// <summary>
/// A factory abstraction for a component that can create <see cref="TodoistClient"/> instances with user tokens.
/// </summary>
public interface ITodoistClientFactory
{
/// <summary>
/// Creates a new instance of <see cref="TodoistClient"/> with the specified user token."/>
/// </summary>
/// <param name="token">The user token to use.</param>
/// <returns>The created <see cref="TodoistClient"/></returns>
TodoistClient CreateClient(string token);
}
}
4 changes: 4 additions & 0 deletions src/Todoist.Net/Todoist.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0"/>
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<Reference Include="System.Net.Http" />
</ItemGroup>
Expand Down
27 changes: 27 additions & 0 deletions src/Todoist.Net/TodoistClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if NETSTANDARD2_0

using System.Net.Http;

namespace Todoist.Net
{
internal sealed class TodoistClientFactory : ITodoistClientFactory
{
private readonly IHttpClientFactory _httpClientFactory;

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

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

return new TodoistClient(todoistRestClient);
}
}
}

#endif
23 changes: 20 additions & 3 deletions src/Todoist.Net/TodoistRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ namespace Todoist.Net
internal sealed class TodoistRestClient : ITodoistRestClient
{
private readonly HttpClient _httpClient;
private readonly bool _disposeHttpClient;

public TodoistRestClient() : this(null, null)
public TodoistRestClient() : this(null, (IWebProxy)null)
{
}

public TodoistRestClient(string token) : this(token, null)
public TodoistRestClient(string token) : this(token, (IWebProxy)null)
{
}

Expand All @@ -43,11 +44,27 @@ public TodoistRestClient(string token, IWebProxy proxy)
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}

_disposeHttpClient = true;
}

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);
}
}

public void Dispose()
{
_httpClient?.Dispose();
if (_disposeHttpClient)
{
_httpClient?.Dispose();
}
}


Expand Down