Skip to content

Commit

Permalink
Merge pull request #184 from notion-dotnet/tech/50-add-ms-di-extension
Browse files Browse the repository at this point in the history
Add Microsoft dependency injection extension for notion client 💖
  • Loading branch information
KoditkarVedant authored Nov 1, 2021
2 parents 3eabedb + 3ce909a commit 957bd85
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 10 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<Token>"
});
Expand All @@ -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 = "<Token>"
});
```

### 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:
Expand Down
21 changes: 21 additions & 0 deletions Src/Notion.Client/DI/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<ClientOptions> options)
{
services.AddSingleton<INotionClient, NotionClient>(sp =>
{
var clientOptions = new ClientOptions();
options?.Invoke(clientOptions);

return NotionClientFactory.Create(clientOptions);
});

return services;
}
}
}
1 change: 1 addition & 0 deletions Src/Notion.Client/Notion.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="JsonSubTypes" Version="1.8.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0"/>
</ItemGroup>

<ItemGroup>
Expand Down
20 changes: 13 additions & 7 deletions Src/Notion.Client/NotionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
19 changes: 19 additions & 0 deletions Src/Notion.Client/NotionClientFactory.cs
Original file line number Diff line number Diff line change
@@ -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)
);
}
}
}
2 changes: 1 addition & 1 deletion Test/Notion.IntegrationTests/IPageClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
12 changes: 11 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<Token>"
});
Expand All @@ -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 = "<Token>"
});
```

### 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:
Expand Down

0 comments on commit 957bd85

Please sign in to comment.