Skip to content

Commit a1d801f

Browse files
Merge pull request #100 from notion-dotnet/feature/79-add-support-to-retrieve-block-api
Add support to retrieve block API 💖
2 parents 71c926c + b9bc3d8 commit a1d801f

File tree

8 files changed

+95
-13
lines changed

8 files changed

+95
-13
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ var complexFiler = new CompoundFilter(
103103
- [x] Retrieve a page
104104
- [x] Create a page
105105
- [x] Update page
106-
- [x] Blocks
107-
- [x] Retrieve Block Children
108-
- [x] Append Block Children
106+
- [ ] Blocks
107+
- [x] Retrieve a block
108+
- [ ] Update a block
109+
- [x] Retrieve block children
110+
- [x] Append block children
109111
- [x] Users
110112
- [x] Retrieve a User
111113
- [x] List all users

Src/Notion.Client/Api/ApiEndpoints.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static class UsersApiUrls
1919

2020
public static class BlocksApiUrls
2121
{
22+
public static string Retrieve(string blockId) => $"/v1/blocks/{blockId}";
2223
public static string RetrieveChildren(string blockId) => $"/v1/blocks/{blockId}/children";
2324
public static string AppendChildren(string blockId) => $"/v1/blocks/{blockId}/children";
2425
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,17 @@ public async Task<Block> AppendChildrenAsync(string blockId, BlocksAppendChildre
4747

4848
return await _client.PatchAsync<Block>(url, body);
4949
}
50+
51+
public async Task<Block> Retrieve(string blockId)
52+
{
53+
if (string.IsNullOrWhiteSpace(blockId))
54+
{
55+
throw new ArgumentNullException(nameof(blockId));
56+
}
57+
58+
var url = BlocksApiUrls.Retrieve(blockId);
59+
60+
return await _client.GetAsync<Block>(url);
61+
}
5062
}
5163
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ namespace Notion.Client
44
{
55
public interface IBlocksClient
66
{
7+
/// <summary>
8+
/// Retrieves a Block object using the ID specified.
9+
/// </summary>
10+
/// <param name="blockId"></param>
11+
/// <returns>Block</returns>
12+
Task<Block> Retrieve(string blockId);
13+
714
Task<PaginatedList<Block>> RetrieveChildrenAsync(string blockId, BlocksRetrieveChildrenParameters parameters = null);
815
Task<Block> AppendChildrenAsync(string blockId, BlocksAppendChildrenParameters parameters = null);
916
}

Test/Notion.UnitTests/BlocksClientTests.cs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
24
using System.Threading.Tasks;
5+
using FluentAssertions;
36
using Notion.Client;
7+
using WireMock.ResponseBuilders;
48
using Xunit;
59

610
namespace Notion.UnitTests
711
{
8-
public class BlocksClientTests
12+
public class BlocksClientTests : ApiTestBase
913
{
1014
private readonly IBlocksClient _client;
1115

1216
public BlocksClientTests()
1317
{
14-
var options = new ClientOptions()
15-
{
16-
AuthToken = "<Token>"
17-
};
18-
19-
_client = new BlocksClient(new RestClient(options));
18+
_client = new BlocksClient(new RestClient(ClientOptions));
2019
}
2120

2221
[Fact(Skip = "Dev only")]
@@ -61,5 +60,31 @@ public async Task AppendBlockChildren()
6160

6261
Assert.NotNull(block);
6362
}
63+
64+
[Fact]
65+
public async Task RetrieveAsync()
66+
{
67+
string blockId = "9bc30ad4-9373-46a5-84ab-0a7845ee52e6";
68+
var path = ApiEndpoints.BlocksApiUrls.Retrieve(blockId);
69+
var jsonData = await File.ReadAllTextAsync("data/blocks/RetrieveBlockResponse.json");
70+
71+
Server.Given(CreateGetRequestBuilder(path))
72+
.RespondWith(
73+
Response.Create()
74+
.WithStatusCode(200)
75+
.WithBody(jsonData)
76+
);
77+
78+
var block = await _client.Retrieve(blockId);
79+
80+
block.Id.Should().Be(blockId);
81+
block.HasChildren.Should().BeFalse();
82+
block.Type.Should().Be(BlockType.ToDo);
83+
84+
var todoBlock = ((ToDoBlock)block);
85+
todoBlock.ToDo.Text.Should().ContainSingle();
86+
todoBlock.ToDo.Text.First().Should().BeAssignableTo<RichTextText>();
87+
((RichTextText)todoBlock.ToDo.Text.First()).Text.Content.Should().Be("Lacinato kale");
88+
}
6489
}
6590
}

Test/Notion.UnitTests/Notion.UnitTests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
</ItemGroup>
2828

2929
<ItemGroup>
30+
<None Update="data\blocks\RetrieveBlockResponse.json">
31+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
32+
</None>
3033
<None Update="data\databases\CreateDatabaseResponse.json">
3134
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3235
</None>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"object": "block",
3+
"id": "9bc30ad4-9373-46a5-84ab-0a7845ee52e6",
4+
"created_time": "2021-03-16T16:31:00.000Z",
5+
"last_edited_time": "2021-03-16T16:32:00.000Z",
6+
"has_children": false,
7+
"type": "to_do",
8+
"to_do": {
9+
"text": [
10+
{
11+
"type": "text",
12+
"text": {
13+
"content": "Lacinato kale",
14+
"link": null
15+
},
16+
"annotations": {
17+
"bold": false,
18+
"italic": false,
19+
"strikethrough": false,
20+
"underline": false,
21+
"code": false,
22+
"color": "default"
23+
},
24+
"plain_text": "Lacinato kale",
25+
"href": null
26+
}
27+
],
28+
"checked": false
29+
}
30+
}

docs/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ var complexFiler = new CompoundFilter(
7373
- [x] Retrieve a page
7474
- [x] Create a page
7575
- [x] Update page
76-
- [x] Blocks
77-
- [x] Retrieve Block Children
78-
- [x] Append Block Children
76+
- [ ] Blocks
77+
- [x] Retrieve a block
78+
- [ ] Update a block
79+
- [x] Retrieve block children
80+
- [x] Append block children
7981
- [x] Users
8082
- [x] Retrieve a User
8183
- [x] List all users

0 commit comments

Comments
 (0)