Skip to content

Commit 957bd85

Browse files
Merge pull request #184 from notion-dotnet/tech/50-add-ms-di-extension
Add Microsoft dependency injection extension for notion client 💖
2 parents 3eabedb + 3ce909a commit 957bd85

File tree

7 files changed

+77
-10
lines changed

7 files changed

+77
-10
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ dotnet add package Notion.Net
5252
Import and initialize the client using the integration token created above.
5353

5454
```csharp
55-
var client = new NotionClient(new ClientOptions
55+
var client = NotionClientFactory.Create(new ClientOptions
5656
{
5757
AuthToken = "<Token>"
5858
});
@@ -64,6 +64,16 @@ Make A request to any Endpoint. For example you can call below to fetch the pagi
6464
var usersList = await client.Users.ListAsync();
6565
```
6666

67+
## Dependency Injection
68+
69+
Library also provides extension method to register NotionClient with Microsoft dependency injection.
70+
71+
```
72+
services.AddNotionClient(options => {
73+
AuthToken = "<Token>"
74+
});
75+
```
76+
6777
### Querying a database
6878

6979
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:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using Notion.Client;
3+
4+
namespace Microsoft.Extensions.DependencyInjection
5+
{
6+
public static class ServiceCollectionExtensions
7+
{
8+
public static IServiceCollection AddNotionClient(this IServiceCollection services, Action<ClientOptions> options)
9+
{
10+
services.AddSingleton<INotionClient, NotionClient>(sp =>
11+
{
12+
var clientOptions = new ClientOptions();
13+
options?.Invoke(clientOptions);
14+
15+
return NotionClientFactory.Create(clientOptions);
16+
});
17+
18+
return services;
19+
}
20+
}
21+
}

Src/Notion.Client/Notion.Client.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
2424
<PackageReference Include="JsonSubTypes" Version="1.8.0" />
2525
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0"/>
26+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0"/>
2627
</ItemGroup>
2728

2829
<ItemGroup>

Src/Notion.Client/NotionClient.cs

+13-7
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ public interface INotionClient
1212

1313
public class NotionClient : INotionClient
1414
{
15-
public NotionClient(ClientOptions options)
15+
public NotionClient(
16+
RestClient restClient,
17+
UsersClient users,
18+
DatabasesClient databases,
19+
PagesClient pages,
20+
SearchClient search,
21+
BlocksClient blocks)
1622
{
17-
RestClient = new RestClient(options);
18-
Users = new UsersClient(RestClient);
19-
Databases = new DatabasesClient(RestClient);
20-
Pages = new PagesClient(RestClient);
21-
Search = new SearchClient(RestClient);
22-
Blocks = new BlocksClient(RestClient);
23+
RestClient = restClient;
24+
Users = users;
25+
Databases = databases;
26+
Pages = pages;
27+
Search = search;
28+
Blocks = blocks;
2329
}
2430

2531
public IUsersClient Users { get; }
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace Notion.Client
2+
{
3+
public static class NotionClientFactory
4+
{
5+
public static NotionClient Create(ClientOptions options)
6+
{
7+
var restClient = new RestClient(options);
8+
9+
return new NotionClient(
10+
restClient: restClient
11+
, users: new UsersClient(restClient)
12+
, databases: new DatabasesClient(restClient)
13+
, pages: new PagesClient(restClient)
14+
, search: new SearchClient(restClient)
15+
, blocks: new BlocksClient(restClient)
16+
);
17+
}
18+
}
19+
}

Test/Notion.IntegrationTests/IPageClientTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public async Task Bug_unable_to_create_page_with_select_property()
6363
AuthToken = Environment.GetEnvironmentVariable("NOTION_AUTH_TOKEN")
6464
};
6565

66-
INotionClient _client = new NotionClient(options);
66+
INotionClient _client = NotionClientFactory.Create(options);
6767

6868
PagesCreateParameters pagesCreateParameters = PagesCreateParametersBuilder.Create(new DatabaseParentInput
6969
{

docs/README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ dotnet add package Notion.Net
1919
Import and initialize the client using the integration token created above.
2020

2121
```csharp
22-
var client = new NotionClient(new ClientOptions
22+
var client = NotionClientFactory.Create(new ClientOptions
2323
{
2424
AuthToken = "<Token>"
2525
});
@@ -31,6 +31,16 @@ Make A request to any Endpoint. For example you can call below to fetch the pagi
3131
var usersList = await client.Users.ListAsync();
3232
```
3333

34+
## Register using Microsoft.Extensions.DependencyInjection
35+
36+
Library also provides extension method to register NotionClient with Microsoft dependency injection.
37+
38+
```
39+
services.AddNotionClient(options => {
40+
AuthToken = "<Token>"
41+
});
42+
```
43+
3444
### Querying a database
3545

3646
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:

0 commit comments

Comments
 (0)