Skip to content

Commit e66a83f

Browse files
Add block property in paginated block children response
1 parent b2d5708 commit e66a83f

File tree

11 files changed

+104
-46
lines changed

11 files changed

+104
-46
lines changed

Src/Notion.Client/Api/Blocks/BlocksClient.cs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Notion.Client
88
{
9-
public class BlocksClient : IBlocksClient
9+
public sealed partial class BlocksClient : IBlocksClient
1010
{
1111
private readonly IRestClient _client;
1212

@@ -15,28 +15,6 @@ public BlocksClient(IRestClient client)
1515
_client = client;
1616
}
1717

18-
public async Task<PaginatedList<IBlock>> RetrieveChildrenAsync(
19-
string blockId,
20-
BlocksRetrieveChildrenParameters parameters = null, CancellationToken cancellationToken = default)
21-
{
22-
if (string.IsNullOrWhiteSpace(blockId))
23-
{
24-
throw new ArgumentNullException(nameof(blockId));
25-
}
26-
27-
var url = BlocksApiUrls.RetrieveChildren(blockId);
28-
29-
var queryParameters = (IBlocksRetrieveChildrenQueryParameters)parameters;
30-
31-
var queryParams = new Dictionary<string, string>
32-
{
33-
{ "start_cursor", queryParameters?.StartCursor },
34-
{ "page_size", queryParameters?.PageSize?.ToString() }
35-
};
36-
37-
return await _client.GetAsync<PaginatedList<IBlock>>(url, queryParams, cancellationToken: cancellationToken);
38-
}
39-
4018
public async Task<PaginatedList<IBlock>> AppendChildrenAsync(
4119
string blockId,
4220
BlocksAppendChildrenParameters parameters = null, CancellationToken cancellationToken = default)

Src/Notion.Client/Api/Blocks/IBlocksClient.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,22 @@ public interface IBlocksClient
1818
/// <param name="blockId"></param>
1919
/// <param name="updateBlock"></param>
2020
/// <returns>Block</returns>
21-
Task<IBlock> UpdateAsync(string blockId, IUpdateBlock updateBlock, CancellationToken cancellationToken = default);
21+
Task<IBlock> UpdateAsync(string blockId, IUpdateBlock updateBlock,
22+
CancellationToken cancellationToken = default);
2223

23-
Task<PaginatedList<IBlock>> RetrieveChildrenAsync(
24-
string blockId,
25-
BlocksRetrieveChildrenParameters parameters = null, CancellationToken cancellationToken = default);
24+
/// <summary>
25+
/// Returns a paginated array of child block objects contained in the block using the ID specified.
26+
/// <br/>
27+
/// In order to receive a complete representation of a block, you may need to recursively retrieve the
28+
/// block children of child blocks.
29+
/// </summary>
30+
/// <param name="request"></param>
31+
/// <param name="cancellationToken"></param>
32+
/// <returns></returns>
33+
Task<RetrieveChildrenResponse> RetrieveChildrenAsync(
34+
BlockRetrieveChildrenRequest request,
35+
CancellationToken cancellationToken = default
36+
);
2637

2738
/// <summary>
2839
/// Creates and appends new children blocks to the parent block_id specified.

Src/Notion.Client/Api/Blocks/RequestParams/BlocksRetrieveChildrenParameters.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

Src/Notion.Client/Api/Blocks/RequestParams/IBlocksRetrieveChildrenQueryParameters.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
namespace Notion.Client
7+
{
8+
public sealed partial class BlocksClient
9+
{
10+
public async Task<RetrieveChildrenResponse> RetrieveChildrenAsync(
11+
BlockRetrieveChildrenRequest request,
12+
CancellationToken cancellationToken = default)
13+
{
14+
if (string.IsNullOrWhiteSpace(request.BlockId))
15+
{
16+
throw new ArgumentNullException(nameof(request.BlockId));
17+
}
18+
19+
var url = ApiEndpoints.BlocksApiUrls.RetrieveChildren(request.BlockId);
20+
21+
var queryParameters = (IBlockRetrieveChildrenQueryParameters)request;
22+
23+
var queryParams = new Dictionary<string, string>
24+
{
25+
{ "start_cursor", queryParameters?.StartCursor },
26+
{ "page_size", queryParameters?.PageSize?.ToString() }
27+
};
28+
29+
return await _client.GetAsync<RetrieveChildrenResponse>(
30+
url,
31+
queryParams,
32+
cancellationToken: cancellationToken
33+
);
34+
}
35+
}
36+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Notion.Client
2+
{
3+
public class BlockRetrieveChildrenRequest :
4+
IBlockRetrieveChildrenQueryParameters,
5+
IBlockRetrieveChildrenPathParameters
6+
{
7+
public string StartCursor { get; set; }
8+
9+
public int? PageSize { get; set; }
10+
11+
public string BlockId { get; set; }
12+
}
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Notion.Client
2+
{
3+
public interface IBlockRetrieveChildrenPathParameters
4+
{
5+
public string BlockId { get; set; }
6+
}
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Notion.Client
2+
{
3+
public interface IBlockRetrieveChildrenQueryParameters : IPaginationParameters
4+
{
5+
}
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
4+
namespace Notion.Client
5+
{
6+
public class RetrieveChildrenResponse : PaginatedList<IBlock>
7+
{
8+
[JsonProperty("block")]
9+
public Dictionary<string, object> Block { get; set; }
10+
}
11+
}

Test/Notion.IntegrationTests/IBlocksClientTests.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ public async Task UpdateBlockAsync_UpdatesGivenBlock()
6868
var blockId = blocks.Results.First().Id;
6969
await Client.Blocks.UpdateAsync(blockId, new BreadcrumbUpdateBlock());
7070

71-
blocks = await Client.Blocks.RetrieveChildrenAsync(page.Id);
71+
blocks = await Client.Blocks.RetrieveChildrenAsync(new BlockRetrieveChildrenRequest
72+
{
73+
BlockId = page.Id
74+
});
7275
blocks.Results.Should().HaveCount(1);
7376

7477
// cleanup
@@ -121,7 +124,10 @@ public async Task UpdateAsync_UpdatesGivenBlock(
121124
var blockId = blocks.Results.First().Id;
122125
await Client.Blocks.UpdateAsync(blockId, updateBlock);
123126

124-
blocks = await Client.Blocks.RetrieveChildrenAsync(page.Id);
127+
blocks = await Client.Blocks.RetrieveChildrenAsync(new BlockRetrieveChildrenRequest
128+
{
129+
BlockId = page.Id
130+
});
125131
blocks.Results.Should().HaveCount(1);
126132

127133
var updatedBlock = blocks.Results.First();
@@ -443,7 +449,9 @@ private static IEnumerable<object[]> BlockData()
443449
var tableBlock = block.Should().NotBeNull().And.BeOfType<TableBlock>().Subject;
444450
tableBlock.HasChildren.Should().BeTrue();
445451

446-
var children = client.Blocks.RetrieveChildrenAsync(tableBlock.Id).GetAwaiter().GetResult();
452+
var children = client.Blocks.RetrieveChildrenAsync(new BlockRetrieveChildrenRequest{
453+
BlockId = tableBlock.Id
454+
}).GetAwaiter().GetResult();
447455

448456
children.Results.Should().ContainSingle()
449457
.Subject.Should().BeOfType<TableRowBlock>()

0 commit comments

Comments
 (0)