Skip to content

Commit 74b33a5

Browse files
authored
Add support for dependency injection's IHttpClientFactory. (#56)
* Added a constructor for `TodoistRestClient` that takes an `HttpClient` argument. * Added a `TodoistClientFactory` that builds `TodoistClients` with tokens. * Added an extension method to add the `TodoistClientFactory` to DI container. * Disabled the added extension method and its dependency for net462 target framework.
1 parent d444c07 commit 74b33a5

5 files changed

+93
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#if NETSTANDARD2_0
2+
3+
using Microsoft.Extensions.DependencyInjection;
4+
5+
namespace Todoist.Net.Extensions
6+
{
7+
/// <summary>
8+
/// Extension methods for setting up todoist client services in an <see cref="IServiceCollection" />.
9+
/// </summary>
10+
public static class TodoistServiceCollectionExtensions
11+
{
12+
/// <summary>
13+
/// Adds todoist client services to the specified <see cref="IServiceCollection" />.
14+
/// </summary>
15+
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
16+
/// <returns>The <see cref="IServiceCollection" /> so that additional calls can be chained.</returns>
17+
public static IServiceCollection AddTodoistClient(this IServiceCollection services)
18+
{
19+
services.AddHttpClient();
20+
services.AddSingleton<ITodoistClientFactory, TodoistClientFactory>();
21+
22+
return services;
23+
}
24+
}
25+
}
26+
27+
#endif
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Todoist.Net
2+
{
3+
/// <summary>
4+
/// A factory abstraction for a component that can create <see cref="TodoistClient"/> instances with user tokens.
5+
/// </summary>
6+
public interface ITodoistClientFactory
7+
{
8+
/// <summary>
9+
/// Creates a new instance of <see cref="TodoistClient"/> with the specified user token."/>
10+
/// </summary>
11+
/// <param name="token">The user token to use.</param>
12+
/// <returns>The created <see cref="TodoistClient"/></returns>
13+
TodoistClient CreateClient(string token);
14+
}
15+
}

src/Todoist.Net/Todoist.Net.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
<PackageReference Include="System.Text.Json" Version="8.0.4" />
3030
</ItemGroup>
3131

32+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
33+
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0"/>
34+
</ItemGroup>
35+
3236
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
3337
<Reference Include="System.Net.Http" />
3438
</ItemGroup>
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#if NETSTANDARD2_0
2+
3+
using System.Net.Http;
4+
5+
namespace Todoist.Net
6+
{
7+
internal sealed class TodoistClientFactory : ITodoistClientFactory
8+
{
9+
private readonly IHttpClientFactory _httpClientFactory;
10+
11+
public TodoistClientFactory(IHttpClientFactory httpClientFactory)
12+
{
13+
_httpClientFactory = httpClientFactory;
14+
}
15+
16+
/// <inheritdoc/>
17+
public TodoistClient CreateClient(string token)
18+
{
19+
var httpClient = _httpClientFactory.CreateClient();
20+
var todoistRestClient = new TodoistRestClient(token, httpClient);
21+
22+
return new TodoistClient(todoistRestClient);
23+
}
24+
}
25+
}
26+
27+
#endif

src/Todoist.Net/TodoistRestClient.cs

+20-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ namespace Todoist.Net
1111
internal sealed class TodoistRestClient : ITodoistRestClient
1212
{
1313
private readonly HttpClient _httpClient;
14+
private readonly bool _disposeHttpClient;
1415

15-
public TodoistRestClient() : this(null, null)
16+
public TodoistRestClient() : this(null, (IWebProxy)null)
1617
{
1718
}
1819

19-
public TodoistRestClient(string token) : this(token, null)
20+
public TodoistRestClient(string token) : this(token, (IWebProxy)null)
2021
{
2122
}
2223

@@ -43,11 +44,27 @@ public TodoistRestClient(string token, IWebProxy proxy)
4344
{
4445
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
4546
}
47+
48+
_disposeHttpClient = true;
49+
}
50+
51+
public TodoistRestClient(string token, HttpClient httpClient)
52+
{
53+
_httpClient = httpClient;
54+
55+
_httpClient.BaseAddress = new Uri("https://api.todoist.com/sync/v9/");
56+
if (!string.IsNullOrEmpty(token))
57+
{
58+
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
59+
}
4660
}
4761

4862
public void Dispose()
4963
{
50-
_httpClient?.Dispose();
64+
if (_disposeHttpClient)
65+
{
66+
_httpClient?.Dispose();
67+
}
5168
}
5269

5370

0 commit comments

Comments
 (0)