From e909426b38c21425b8f035d7f81cfd4a49d2a565 Mon Sep 17 00:00:00 2001 From: Vedant Koditkar Date: Mon, 25 Oct 2021 23:08:33 +0530 Subject: [PATCH 1/5] =?UTF-8?q?Create=20NotionClientFactory=20to=20create?= =?UTF-8?q?=20new=20client=20=E2=99=BB=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * move creation of sub clients under factory method --- Src/Notion.Client/NotionClient.cs | 20 +++++++++++++------- Src/Notion.Client/NotionClientFactory.cs | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 Src/Notion.Client/NotionClientFactory.cs 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..86b1cc14 --- /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) + ); + } + } +} From 32a7165c1c9c6acb38ed728412a6589aaaa30bb6 Mon Sep 17 00:00:00 2001 From: Vedant Koditkar Date: Mon, 25 Oct 2021 23:09:02 +0530 Subject: [PATCH 2/5] =?UTF-8?q?Update=20test=20cases=20to=20use=20notion?= =?UTF-8?q?=20client=20factory=20=E2=9C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Test/Notion.IntegrationTests/IPageClientTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 { From 21e5ceaf4f7b952826f057639e74ac89d88d393a Mon Sep 17 00:00:00 2001 From: Vedant Koditkar Date: Mon, 25 Oct 2021 23:09:32 +0530 Subject: [PATCH 3/5] Add DI extension method to register NotionClient --- .../DI/ServiceCollectionExtensions.cs | 20 +++++++++++++++++++ Src/Notion.Client/Notion.Client.csproj | 1 + 2 files changed, 21 insertions(+) create mode 100644 Src/Notion.Client/DI/ServiceCollectionExtensions.cs diff --git a/Src/Notion.Client/DI/ServiceCollectionExtensions.cs b/Src/Notion.Client/DI/ServiceCollectionExtensions.cs new file mode 100644 index 00000000..b4b4f053 --- /dev/null +++ b/Src/Notion.Client/DI/ServiceCollectionExtensions.cs @@ -0,0 +1,20 @@ +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 @@ + From a7bf03fe14abb2cc15ea5a0d87a83546c207bdd4 Mon Sep 17 00:00:00 2001 From: Vedant Koditkar Date: Mon, 25 Oct 2021 23:15:53 +0530 Subject: [PATCH 4/5] =?UTF-8?q?Update=20Readme=20with=20DI=20extension=20d?= =?UTF-8?q?etails=20=F0=9F=93=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 +++++++++++- docs/README.md | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) 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/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: From 3ce909ad39382e056680659b3309a019d0bac85b Mon Sep 17 00:00:00 2001 From: Vedant Koditkar Date: Mon, 1 Nov 2021 18:38:31 +0530 Subject: [PATCH 5/5] =?UTF-8?q?Run=20dotnet=20format=20=F0=9F=9A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Src/Notion.Client/DI/ServiceCollectionExtensions.cs | 7 ++++--- Src/Notion.Client/NotionClientFactory.cs | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Src/Notion.Client/DI/ServiceCollectionExtensions.cs b/Src/Notion.Client/DI/ServiceCollectionExtensions.cs index b4b4f053..a4aed7fd 100644 --- a/Src/Notion.Client/DI/ServiceCollectionExtensions.cs +++ b/Src/Notion.Client/DI/ServiceCollectionExtensions.cs @@ -1,13 +1,14 @@ -using System; +using System; using Notion.Client; namespace Microsoft.Extensions.DependencyInjection { public static class ServiceCollectionExtensions { - public static IServiceCollection AddNotionClient(this IServiceCollection services, Action options) + public static IServiceCollection AddNotionClient(this IServiceCollection services, Action options) { - services.AddSingleton(sp => { + services.AddSingleton(sp => + { var clientOptions = new ClientOptions(); options?.Invoke(clientOptions); diff --git a/Src/Notion.Client/NotionClientFactory.cs b/Src/Notion.Client/NotionClientFactory.cs index 86b1cc14..4010eb8c 100644 --- a/Src/Notion.Client/NotionClientFactory.cs +++ b/Src/Notion.Client/NotionClientFactory.cs @@ -1,4 +1,4 @@ -namespace Notion.Client +namespace Notion.Client { public static class NotionClientFactory {