Skip to content

Commit

Permalink
Merge pull request #100 from notion-dotnet/feature/79-add-support-to-…
Browse files Browse the repository at this point in the history
…retrieve-block-api

Add support to retrieve block API 💖
  • Loading branch information
KoditkarVedant authored Aug 18, 2021
2 parents 71c926c + b9bc3d8 commit a1d801f
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 13 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ var complexFiler = new CompoundFilter(
- [x] Retrieve a page
- [x] Create a page
- [x] Update page
- [x] Blocks
- [x] Retrieve Block Children
- [x] Append Block Children
- [ ] Blocks
- [x] Retrieve a block
- [ ] Update a block
- [x] Retrieve block children
- [x] Append block children
- [x] Users
- [x] Retrieve a User
- [x] List all users
Expand Down
1 change: 1 addition & 0 deletions Src/Notion.Client/Api/ApiEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static class UsersApiUrls

public static class BlocksApiUrls
{
public static string Retrieve(string blockId) => $"/v1/blocks/{blockId}";
public static string RetrieveChildren(string blockId) => $"/v1/blocks/{blockId}/children";
public static string AppendChildren(string blockId) => $"/v1/blocks/{blockId}/children";
}
Expand Down
12 changes: 12 additions & 0 deletions Src/Notion.Client/Api/Blocks/BlocksClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,17 @@ public async Task<Block> AppendChildrenAsync(string blockId, BlocksAppendChildre

return await _client.PatchAsync<Block>(url, body);
}

public async Task<Block> Retrieve(string blockId)
{
if (string.IsNullOrWhiteSpace(blockId))
{
throw new ArgumentNullException(nameof(blockId));
}

var url = BlocksApiUrls.Retrieve(blockId);

return await _client.GetAsync<Block>(url);
}
}
}
7 changes: 7 additions & 0 deletions Src/Notion.Client/Api/Blocks/IBlocksClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ namespace Notion.Client
{
public interface IBlocksClient
{
/// <summary>
/// Retrieves a Block object using the ID specified.
/// </summary>
/// <param name="blockId"></param>
/// <returns>Block</returns>
Task<Block> Retrieve(string blockId);

Task<PaginatedList<Block>> RetrieveChildrenAsync(string blockId, BlocksRetrieveChildrenParameters parameters = null);
Task<Block> AppendChildrenAsync(string blockId, BlocksAppendChildrenParameters parameters = null);
}
Expand Down
39 changes: 32 additions & 7 deletions Test/Notion.UnitTests/BlocksClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Notion.Client;
using WireMock.ResponseBuilders;
using Xunit;

namespace Notion.UnitTests
{
public class BlocksClientTests
public class BlocksClientTests : ApiTestBase
{
private readonly IBlocksClient _client;

public BlocksClientTests()
{
var options = new ClientOptions()
{
AuthToken = "<Token>"
};

_client = new BlocksClient(new RestClient(options));
_client = new BlocksClient(new RestClient(ClientOptions));
}

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

Assert.NotNull(block);
}

[Fact]
public async Task RetrieveAsync()
{
string blockId = "9bc30ad4-9373-46a5-84ab-0a7845ee52e6";
var path = ApiEndpoints.BlocksApiUrls.Retrieve(blockId);
var jsonData = await File.ReadAllTextAsync("data/blocks/RetrieveBlockResponse.json");

Server.Given(CreateGetRequestBuilder(path))
.RespondWith(
Response.Create()
.WithStatusCode(200)
.WithBody(jsonData)
);

var block = await _client.Retrieve(blockId);

block.Id.Should().Be(blockId);
block.HasChildren.Should().BeFalse();
block.Type.Should().Be(BlockType.ToDo);

var todoBlock = ((ToDoBlock)block);
todoBlock.ToDo.Text.Should().ContainSingle();
todoBlock.ToDo.Text.First().Should().BeAssignableTo<RichTextText>();
((RichTextText)todoBlock.ToDo.Text.First()).Text.Content.Should().Be("Lacinato kale");
}
}
}
3 changes: 3 additions & 0 deletions Test/Notion.UnitTests/Notion.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
</ItemGroup>

<ItemGroup>
<None Update="data\blocks\RetrieveBlockResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\databases\CreateDatabaseResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
30 changes: 30 additions & 0 deletions Test/Notion.UnitTests/data/blocks/RetrieveBlockResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"object": "block",
"id": "9bc30ad4-9373-46a5-84ab-0a7845ee52e6",
"created_time": "2021-03-16T16:31:00.000Z",
"last_edited_time": "2021-03-16T16:32:00.000Z",
"has_children": false,
"type": "to_do",
"to_do": {
"text": [
{
"type": "text",
"text": {
"content": "Lacinato kale",
"link": null
},
"annotations": {
"bold": false,
"italic": false,
"strikethrough": false,
"underline": false,
"code": false,
"color": "default"
},
"plain_text": "Lacinato kale",
"href": null
}
],
"checked": false
}
}
8 changes: 5 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ var complexFiler = new CompoundFilter(
- [x] Retrieve a page
- [x] Create a page
- [x] Update page
- [x] Blocks
- [x] Retrieve Block Children
- [x] Append Block Children
- [ ] Blocks
- [x] Retrieve a block
- [ ] Update a block
- [x] Retrieve block children
- [x] Append block children
- [x] Users
- [x] Retrieve a User
- [x] List all users
Expand Down

0 comments on commit a1d801f

Please sign in to comment.