diff --git a/README.md b/README.md index eec01522..39368ff1 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ dotnet add package Notion.Net Import and initialize the client using the integration token created above. ```csharp -var client = new NotionClient(new ClientOptions +var client = NotionClientFactory.Create(new ClientOptions { AuthToken = "" }); @@ -64,6 +64,16 @@ Make A request to any Endpoint. For example you can call below to fetch the pagi var usersList = await client.Users.ListAsync(); ``` +## Dependency Injection + +Library also provides extension method to register NotionClient with Microsoft dependency injection. + +``` +services.AddNotionClient(options => { + AuthToken = "" +}); +``` + ### Querying a database After you initialized your client and got an id of a database, you can query it for any contained pages. You can add filters and sorts to your request. Here is a simple example: diff --git a/Src/Notion.Client/DI/ServiceCollectionExtensions.cs b/Src/Notion.Client/DI/ServiceCollectionExtensions.cs new file mode 100644 index 00000000..a4aed7fd --- /dev/null +++ b/Src/Notion.Client/DI/ServiceCollectionExtensions.cs @@ -0,0 +1,21 @@ +using System; +using Notion.Client; + +namespace Microsoft.Extensions.DependencyInjection +{ + public static class ServiceCollectionExtensions + { + public static IServiceCollection AddNotionClient(this IServiceCollection services, Action options) + { + services.AddSingleton(sp => + { + var clientOptions = new ClientOptions(); + options?.Invoke(clientOptions); + + return NotionClientFactory.Create(clientOptions); + }); + + return services; + } + } +} diff --git a/Src/Notion.Client/Notion.Client.csproj b/Src/Notion.Client/Notion.Client.csproj index 31f53181..faaa9fb0 100644 --- a/Src/Notion.Client/Notion.Client.csproj +++ b/Src/Notion.Client/Notion.Client.csproj @@ -23,6 +23,7 @@ + diff --git a/Src/Notion.Client/NotionClient.cs b/Src/Notion.Client/NotionClient.cs index b47cc632..5bbc6dad 100644 --- a/Src/Notion.Client/NotionClient.cs +++ b/Src/Notion.Client/NotionClient.cs @@ -12,14 +12,20 @@ public interface INotionClient public class NotionClient : INotionClient { - public NotionClient(ClientOptions options) + public NotionClient( + RestClient restClient, + UsersClient users, + DatabasesClient databases, + PagesClient pages, + SearchClient search, + BlocksClient blocks) { - RestClient = new RestClient(options); - Users = new UsersClient(RestClient); - Databases = new DatabasesClient(RestClient); - Pages = new PagesClient(RestClient); - Search = new SearchClient(RestClient); - Blocks = new BlocksClient(RestClient); + RestClient = restClient; + Users = users; + Databases = databases; + Pages = pages; + Search = search; + Blocks = blocks; } public IUsersClient Users { get; } diff --git a/Src/Notion.Client/NotionClientFactory.cs b/Src/Notion.Client/NotionClientFactory.cs new file mode 100644 index 00000000..4010eb8c --- /dev/null +++ b/Src/Notion.Client/NotionClientFactory.cs @@ -0,0 +1,19 @@ +namespace Notion.Client +{ + public static class NotionClientFactory + { + public static NotionClient Create(ClientOptions options) + { + var restClient = new RestClient(options); + + return new NotionClient( + restClient: restClient + , users: new UsersClient(restClient) + , databases: new DatabasesClient(restClient) + , pages: new PagesClient(restClient) + , search: new SearchClient(restClient) + , blocks: new BlocksClient(restClient) + ); + } + } +} diff --git a/Test/Notion.IntegrationTests/IPageClientTests.cs b/Test/Notion.IntegrationTests/IPageClientTests.cs index 6446763b..6a8259be 100644 --- a/Test/Notion.IntegrationTests/IPageClientTests.cs +++ b/Test/Notion.IntegrationTests/IPageClientTests.cs @@ -63,7 +63,7 @@ public async Task Bug_unable_to_create_page_with_select_property() AuthToken = Environment.GetEnvironmentVariable("NOTION_AUTH_TOKEN") }; - INotionClient _client = new NotionClient(options); + INotionClient _client = NotionClientFactory.Create(options); PagesCreateParameters pagesCreateParameters = PagesCreateParametersBuilder.Create(new DatabaseParentInput { diff --git a/docs/README.md b/docs/README.md index f1445c87..5f32406e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -19,7 +19,7 @@ dotnet add package Notion.Net Import and initialize the client using the integration token created above. ```csharp -var client = new NotionClient(new ClientOptions +var client = NotionClientFactory.Create(new ClientOptions { AuthToken = "" }); @@ -31,6 +31,16 @@ Make A request to any Endpoint. For example you can call below to fetch the pagi var usersList = await client.Users.ListAsync(); ``` +## Register using Microsoft.Extensions.DependencyInjection + +Library also provides extension method to register NotionClient with Microsoft dependency injection. + +``` +services.AddNotionClient(options => { + AuthToken = "" +}); +``` + ### Querying a database After you initialized your client and got an id of a database, you can query it for any contained pages. You can add filters and sorts to your request. Here is a simple example: