From 2ae2c01efb168eaa38b88a08020bb8a4b694fd85 Mon Sep 17 00:00:00 2001 From: Ahmed Zaki Date: Tue, 25 Jun 2024 10:44:32 +0300 Subject: [PATCH 1/4] Added a constructor for `TodoistRestClient` that takes an `HttpClient` argument. --- src/Todoist.Net/TodoistRestClient.cs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Todoist.Net/TodoistRestClient.cs b/src/Todoist.Net/TodoistRestClient.cs index 2dbf494..7c287e8 100644 --- a/src/Todoist.Net/TodoistRestClient.cs +++ b/src/Todoist.Net/TodoistRestClient.cs @@ -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) { } @@ -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(); + } } From 0838ee61866345eb4a9a463e0bc5376cee7ad4dc Mon Sep 17 00:00:00 2001 From: Ahmed Zaki Date: Tue, 25 Jun 2024 10:45:17 +0300 Subject: [PATCH 2/4] Added a `TodoistClientFactory` that builds `TodoistClients` with tokens. --- src/Todoist.Net/ITodoistClientFactory.cs | 15 +++++++++++++++ src/Todoist.Net/TodoistClientFactory.cs | 23 +++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/Todoist.Net/ITodoistClientFactory.cs create mode 100644 src/Todoist.Net/TodoistClientFactory.cs diff --git a/src/Todoist.Net/ITodoistClientFactory.cs b/src/Todoist.Net/ITodoistClientFactory.cs new file mode 100644 index 0000000..968b3b5 --- /dev/null +++ b/src/Todoist.Net/ITodoistClientFactory.cs @@ -0,0 +1,15 @@ +namespace Todoist.Net +{ + /// + /// A factory abstraction for a component that can create instances with user tokens. + /// + public interface ITodoistClientFactory + { + /// + /// Creates a new instance of with the specified user token."/> + /// + /// The user token to use. + /// The created + TodoistClient CreateClient(string token); + } +} diff --git a/src/Todoist.Net/TodoistClientFactory.cs b/src/Todoist.Net/TodoistClientFactory.cs new file mode 100644 index 0000000..3d9e5b8 --- /dev/null +++ b/src/Todoist.Net/TodoistClientFactory.cs @@ -0,0 +1,23 @@ +using System.Net.Http; + +namespace Todoist.Net +{ + 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); + } + } +} From 3362520f24c39b8a667b9aeb941d66470093cefc Mon Sep 17 00:00:00 2001 From: Ahmed Zaki Date: Tue, 25 Jun 2024 10:45:43 +0300 Subject: [PATCH 3/4] Added an extension method to add the `TodoistClientFactory` to DI container. --- .../TodoistServiceCollectionExtensions.cs | 23 +++++++++++++++++++ src/Todoist.Net/Todoist.Net.csproj | 1 + 2 files changed, 24 insertions(+) create mode 100644 src/Todoist.Net/Extensions/TodoistServiceCollectionExtensions.cs diff --git a/src/Todoist.Net/Extensions/TodoistServiceCollectionExtensions.cs b/src/Todoist.Net/Extensions/TodoistServiceCollectionExtensions.cs new file mode 100644 index 0000000..c86d7bf --- /dev/null +++ b/src/Todoist.Net/Extensions/TodoistServiceCollectionExtensions.cs @@ -0,0 +1,23 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace Todoist.Net.Extensions +{ + /// + /// Extension methods for setting up todoist client services in an . + /// + public static class TodoistServiceCollectionExtensions + { + /// + /// Adds todoist client services to the specified . + /// + /// The to add services to. + /// The so that additional calls can be chained. + public static IServiceCollection AddTodoistClient(this IServiceCollection services) + { + services.AddHttpClient(); + services.AddSingleton(); + + return services; + } + } +} diff --git a/src/Todoist.Net/Todoist.Net.csproj b/src/Todoist.Net/Todoist.Net.csproj index 7281005..97531fd 100644 --- a/src/Todoist.Net/Todoist.Net.csproj +++ b/src/Todoist.Net/Todoist.Net.csproj @@ -25,6 +25,7 @@ + From cc68ac44a45c49e77db0c303f4009e1a0c1b9a93 Mon Sep 17 00:00:00 2001 From: Ahmed Zaki Date: Mon, 5 Aug 2024 22:18:12 +0300 Subject: [PATCH 4/4] Disabled the added extension method and its dependency for net462 target framework. --- .../Extensions/TodoistServiceCollectionExtensions.cs | 4 ++++ src/Todoist.Net/Todoist.Net.csproj | 5 ++++- src/Todoist.Net/TodoistClientFactory.cs | 4 ++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Todoist.Net/Extensions/TodoistServiceCollectionExtensions.cs b/src/Todoist.Net/Extensions/TodoistServiceCollectionExtensions.cs index c86d7bf..100b737 100644 --- a/src/Todoist.Net/Extensions/TodoistServiceCollectionExtensions.cs +++ b/src/Todoist.Net/Extensions/TodoistServiceCollectionExtensions.cs @@ -1,3 +1,5 @@ +#if NETSTANDARD2_0 + using Microsoft.Extensions.DependencyInjection; namespace Todoist.Net.Extensions @@ -21,3 +23,5 @@ public static IServiceCollection AddTodoistClient(this IServiceCollection servic } } } + +#endif diff --git a/src/Todoist.Net/Todoist.Net.csproj b/src/Todoist.Net/Todoist.Net.csproj index 07a04e3..e1c5efa 100644 --- a/src/Todoist.Net/Todoist.Net.csproj +++ b/src/Todoist.Net/Todoist.Net.csproj @@ -25,11 +25,14 @@ - + + + + diff --git a/src/Todoist.Net/TodoistClientFactory.cs b/src/Todoist.Net/TodoistClientFactory.cs index 3d9e5b8..693e9af 100644 --- a/src/Todoist.Net/TodoistClientFactory.cs +++ b/src/Todoist.Net/TodoistClientFactory.cs @@ -1,3 +1,5 @@ +#if NETSTANDARD2_0 + using System.Net.Http; namespace Todoist.Net @@ -21,3 +23,5 @@ public TodoistClient CreateClient(string token) } } } + +#endif