diff --git a/src/Todoist.Net/Extensions/TodoistServiceCollectionExtensions.cs b/src/Todoist.Net/Extensions/TodoistServiceCollectionExtensions.cs new file mode 100644 index 0000000..100b737 --- /dev/null +++ b/src/Todoist.Net/Extensions/TodoistServiceCollectionExtensions.cs @@ -0,0 +1,27 @@ +#if NETSTANDARD2_0 + +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; + } + } +} + +#endif 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/Todoist.Net.csproj b/src/Todoist.Net/Todoist.Net.csproj index 8e71da0..e1c5efa 100644 --- a/src/Todoist.Net/Todoist.Net.csproj +++ b/src/Todoist.Net/Todoist.Net.csproj @@ -29,6 +29,10 @@ + + + + diff --git a/src/Todoist.Net/TodoistClientFactory.cs b/src/Todoist.Net/TodoistClientFactory.cs new file mode 100644 index 0000000..693e9af --- /dev/null +++ b/src/Todoist.Net/TodoistClientFactory.cs @@ -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; + } + + /// + public TodoistClient CreateClient(string token) + { + var httpClient = _httpClientFactory.CreateClient(); + var todoistRestClient = new TodoistRestClient(token, httpClient); + + return new TodoistClient(todoistRestClient); + } + } +} + +#endif diff --git a/src/Todoist.Net/TodoistRestClient.cs b/src/Todoist.Net/TodoistRestClient.cs index b059db5..9a80442 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(); + } }