-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #391 from notion-dotnet/feature/375-support-queryi…
…ng-wiki-databases Add support for querying wiki databases
- Loading branch information
Showing
12 changed files
with
141 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Notion.Client | ||
{ | ||
public sealed partial class DatabasesClient | ||
{ | ||
public async Task<DatabaseQueryResponse> QueryAsync( | ||
string databaseId, | ||
DatabasesQueryParameters databasesQueryParameters, CancellationToken cancellationToken = default) | ||
{ | ||
var body = (IDatabaseQueryBodyParameters)databasesQueryParameters; | ||
var queryParameters = (IDatabaseQueryQueryParameters)databasesQueryParameters; | ||
|
||
var queryParams = queryParameters.FilterProperties? | ||
.Select(x => new KeyValuePair<string, string>("filter_properties", x)); | ||
|
||
return await _client.PostAsync<DatabaseQueryResponse>( | ||
ApiEndpoints.DatabasesApiUrls.Query(databaseId), | ||
body, | ||
queryParams, | ||
cancellationToken: cancellationToken | ||
); | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
Src/Notion.Client/Api/Databases/Query/Response/DatabaseQueryResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Notion.Client | ||
{ | ||
// ReSharper disable once ClassNeverInstantiated.Global | ||
public class DatabaseQueryResponse : PaginatedList<IWikiDatabase> | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using JsonSubTypes; | ||
using Newtonsoft.Json; | ||
|
||
namespace Notion.Client | ||
{ | ||
[JsonConverter(typeof(JsonSubtypes), "object")] | ||
[JsonSubtypes.KnownSubTypeAttribute(typeof(Page), ObjectType.Page)] | ||
[JsonSubtypes.KnownSubTypeAttribute(typeof(Database), ObjectType.Database)] | ||
public interface IWikiDatabase : IObject | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Notion.Client; | ||
using Xunit; | ||
|
||
namespace Notion.IntegrationTests; | ||
|
||
public class DatabasesClientTests : IntegrationTestBase, IDisposable | ||
{ | ||
private readonly Page _page; | ||
|
||
public DatabasesClientTests() | ||
{ | ||
_page = Client.Pages.CreateAsync( | ||
PagesCreateParametersBuilder.Create( | ||
new ParentPageInput { PageId = ParentPageId } | ||
).Build() | ||
).Result; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Client.Pages.UpdateAsync(_page.Id, new PagesUpdateParameters { Archived = true }).Wait(); | ||
} | ||
|
||
[Fact] | ||
public async Task QueryDatabase() | ||
{ | ||
// Arrange | ||
var createdDatabase = await CreateDatabaseWithAPageAsync(); | ||
|
||
// Act | ||
var response = await Client.Databases.QueryAsync(createdDatabase.Id, new DatabasesQueryParameters()); | ||
|
||
// Assert | ||
Assert.NotNull(response.Results); | ||
Assert.Single(response.Results); | ||
var page = response.Results.Cast<Page>().First(); | ||
var title = page.Properties["Name"] as TitlePropertyValue; | ||
Assert.Equal("Test Title", (title!.Title.Cast<RichTextText>().First()).Text.Content); | ||
} | ||
|
||
private async Task<Database> CreateDatabaseWithAPageAsync() | ||
{ | ||
var createDbRequest = new DatabasesCreateParameters | ||
{ | ||
Title = new List<RichTextBaseInput> | ||
{ | ||
new RichTextTextInput | ||
{ | ||
Text = new Text | ||
{ | ||
Content = "Test List", | ||
Link = null | ||
} | ||
} | ||
}, | ||
Properties = new Dictionary<string, IPropertySchema> | ||
{ | ||
{ "Name", new TitlePropertySchema { Title = new Dictionary<string, object>() } }, | ||
}, | ||
Parent = new ParentPageInput { PageId = _page.Id } | ||
}; | ||
|
||
var createdDatabase = await Client.Databases.CreateAsync(createDbRequest); | ||
|
||
var pagesCreateParameters = PagesCreateParametersBuilder | ||
.Create(new DatabaseParentInput { DatabaseId = createdDatabase.Id }) | ||
.AddProperty("Name", | ||
new TitlePropertyValue | ||
{ | ||
Title = new List<RichTextBase> | ||
{ | ||
new RichTextText { Text = new Text { Content = "Test Title" } } | ||
} | ||
}) | ||
.Build(); | ||
|
||
await Client.Pages.CreateAsync(pagesCreateParameters); | ||
|
||
return createdDatabase; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters