10000 Merge pull request #391 from notion-dotnet/feature/375-support-queryi… · notion-dotnet/notion-sdk-net@b422493 · GitHub
[go: up one dir, main page]

Skip to content

Commit b422493

Browse files
Merge pull request #391 from notion-dotnet/feature/375-support-querying-wiki-databases
Add support for querying wiki databases
2 parents 95d0b68 + 8236df3 commit b422493

File tree

12 files changed

+141
-27
lines changed

12 files changed

+141
-27
lines changed

Src/Notion.Client/Api/Databases/DatabasesClient.cs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
3-
using System.Threading;
1+
using System.Threading;
42
using System.Threading.Tasks;
53
using static Notion.Client.ApiEndpoints;
64

75
namespace Notion.Client
86
{
9-
public class DatabasesClient : IDatabasesClient
7+
public sealed partial class DatabasesClient : IDatabasesClient
108
{
119
private readonly IRestClient _client;
1210

@@ -20,24 +18,6 @@ public async Task<Database> RetrieveAsync(string databaseId, CancellationToken c
2018
return await _client.GetAsync<Database>(DatabasesApiUrls.Retrieve(databaseId), cancellationToken: cancellationToken);
2119
}
2220

23-
public async Task<PaginatedList<Page>> QueryAsync(
24-
string databaseId,
25-
DatabasesQueryParameters databasesQueryParameters, CancellationToken cancellationToken = default)
26-
{
27-
var body = (IDatabaseQueryBodyParameters)databasesQueryParameters;
28-
var queryParameters = (IDatabaseQueryQueryParameters)databasesQueryParameters;
29-
30-
var queryParams = queryParameters.FilterProperties?
31-
.Select(x => new KeyValuePair<string, string>("filter_properties", x));
32-
33-
return await _client.PostAsync<PaginatedList<Page>>(
34-
DatabasesApiUrls.Query(databaseId),
35-
body,
36-
queryParams,
37-
cancellationToken: cancellationToken
38-
);
39-
}
40-
4121
public async Task<Database> CreateAsync(DatabasesCreateParameters databasesCreateParameters, CancellationToken cancellationToken = default)
4222
{
4323
var body = (IDatabasesCreateBodyParameters)databasesCreateParameters;

Src/Notion.Client/Api/Databases/IDatabasesClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface IDatabasesClient
2424
/// <returns>
2525
/// <see cref="PaginatedList{T}" />
2626
/// </returns>
27-
Task<PaginatedList<Page>> QueryAsync(string databaseId, DatabasesQueryParameters databasesQueryParameters, CancellationToken cancellationToken = default);
27+
Task<DatabaseQueryResponse> QueryAsync(string databaseId, DatabasesQueryParameters databasesQueryParameters, CancellationToken cancellationToken = default);
2828

2929
/// <summary>
3030
/// Creates a database as a subpage in the specified parent page, with the specified properties schema.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
namespace Notion.Client
7+
{
8+
public sealed partial class DatabasesClient
9+
{
10+
public async Task<DatabaseQueryResponse> QueryAsync(
11+
string databaseId,
12+
DatabasesQueryParameters databasesQueryParameters, CancellationToken cancellationToken = default)
13+
{
14+
var body = (IDatabaseQueryBodyParameters)databasesQueryParameters;
15+
var queryParameters = (IDatabaseQueryQueryParameters)databasesQueryParameters;
16+
17+
var queryParams = queryParameters.FilterProperties?
18+
.Select(x => new KeyValuePair<string, string>("filter_properties", x));
19+
20+
return await _client.PostAsync<DatabaseQueryResponse>(
21+
ApiEndpoints.DatabasesApiUrls.Query(databaseId),
22+
body,
23+
queryParams,
24+
cancellationToken: cancellationToken
25+
);
26+
}
27+
}
28+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Notion.Client
2+
{
3+
// ReSharper disable once ClassNeverInstantiated.Global
4+
public class DatabaseQueryResponse : PaginatedList<IWikiDatabase>
5+
{
6+
}
7+
}

Src/Notion.Client/Models/Database/Database.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Notion.Client
66
{
7-
public class Database : IObject, IObjectModificationData
7+
public class Database : IObject, IObjectModificationData, IWikiDatabase
88
{
99
[JsonProperty("title")]
1010
public List<RichTextBase> Title { get; set; }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using JsonSubTypes;
2+
using Newtonsoft.Json;
3+
4+
namespace Notion.Client
5+
{
6+
[JsonConverter(typeof(JsonSubtypes), "object")]
7+
[JsonSubtypes.KnownSubTypeAttribute(typeof(Page), ObjectType.Page)]
8+
[JsonSubtypes.KnownSubTypeAttribute(typeof(Database), ObjectType.Database)]
9+
public interface IWikiDatabase : IObject
10+
{
11+
}
12+
}

Src/Notion.Client/Models/Page/Page.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Notion.Client
66
{
7-
public class Page : IObject, IObjectModificationData
7+
public class Page : IObject, IObjectModificationData, IWikiDatabase
88
{
99
/// <summary>
1010
/// The parent of this page. Can be a database, page, or workspace.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Notion.Client;
6+
using Xunit;
7+
8+
namespace Notion.IntegrationTests;
9+
10+
public class DatabasesClientTests : IntegrationTestBase, IDisposable
11+
{
12+
private readonly Page _page;
13+
14+
public DatabasesClientTests()
15+
{
16+
_page = Client.Pages.CreateAsync(
17+
PagesCreateParametersBuilder.Create(
18+
new ParentPageInput { PageId = ParentPageId }
19+
).Build()
20+
).Result;
21+
}
22+
23+
public void Dispose()
24+
{
25+
Client.Pages.UpdateAsync(_page.Id, new PagesUpdateParameters { Archived = true }).Wait();
26+
}
27+
28+
[Fact]
29+
public async Task QueryDatabase()
30+
{
31+
// Arrange
32+
var createdDatabase = await CreateDatabaseWithAPageAsync();
33+
34+
// Act
35+
var response = await Client.Databases.QueryAsync(createdDatabase.Id, new DatabasesQueryParameters());
36+
37+
// Assert
38+
Assert.NotNull(response.Results);
39+
Assert.Single(response.Results);
40+
var page = response.Results.Cast<Page>().First();
41+
var title = page.Properties["Name"] as TitlePropertyValue;
42+
Assert.Equal("Test Title", (title!.Title.Cast<RichTextText>().First()).Text.Content);
43+
}
44+
45+
private async Task<Database> CreateDatabaseWithAPageAsync()
46+
{
47+
var createDbRequest = new DatabasesCreateParameters
48+
{
49+
Title = new List<RichTextBaseInput>
50+
{
51+
new RichTextTextInput
52+
{
53+
Text = new Text
54+
{
55+
Content = "Test List",
56+
Link = null
57+
}
58+
}
59+
},
60+
Properties = new Dictionary<string, IPropertySchema>
61+
{
62+
{ "Name", new TitlePropertySchema { Title = new Dictionary<string, object>() } },
63+
},
64+
Parent = new ParentPageInput { PageId = _page.Id }
65+
};
66+
67+
var createdDatabase = await Client.Databases.CreateAsync(createDbRequest);
68+
69+
var pagesCreateParameters = PagesCreateParametersBuilder
70+
.Create(new DatabaseParentInput { DatabaseId = createdDatabase.Id })
71+
.AddProperty("Name",
72+
new TitlePropertyValue
73+
{
74+
Title = new List<RichTextBase>
75+
{
76+
new RichTextText { Text = new Text { Content = "Test Title" } }
77+
}
78+
})
79+
.Build();
80+
81+
await Client.Pages.CreateAsync(pagesCreateParameters);
82+
83+
return createdDatabase;
84+
}
85+
}

Test/Notion.UnitTests/DatabasesClientTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ public async Task QueryAsync()
6464

6565
pagesPaginatedList.Results.Should().ContainSingle();
6666

67-
foreach (var page in pagesPaginatedList.Results)
67+
foreach (var iWikiDatabase in pagesPaginatedList.Results)
6868
{
69+
var page = (Page)iWikiDatabase;
6970
page.Parent.Should().BeAssignableTo<IPageParent>();
7071
page.Object.Should().Be(ObjectType.Page);
7172
}
@@ -476,8 +477,9 @@ var jsonData
476477

477478
pagesPaginatedList.Results.Should().ContainSingle();
478479

479-
foreach (var page in pagesPaginatedList.Results)
480+
foreach (var iWikiDatabase in pagesPaginatedList.Results)
480481
{
482+
var page = (Page)iWikiDatabase;
481483
page.Parent.Should().BeAssignableTo<IPageParent>();
482484
page.Object.Should().Be(ObjectType.Page);
483485

0 commit comments

Comments
 (0)
0