Skip to content

Commit

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

Add support to update a block 💖
  • Loading branch information
KoditkarVedant authored Aug 18, 2021
2 parents a1d801f + 52fa7a2 commit 8f8f4b9
Show file tree
Hide file tree
Showing 18 changed files with 203 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ var complexFiler = new CompoundFilter(
- [x] Retrieve a page
- [x] Create a page
- [x] Update page
- [ ] Blocks
- [x] Blocks
- [x] Retrieve a block
- [ ] Update a block
- [x] Update a block
- [x] Retrieve block children
- [x] Append block children
- [x] 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 @@ -20,6 +20,7 @@ public static class UsersApiUrls
public static class BlocksApiUrls
{
public static string Retrieve(string blockId) => $"/v1/blocks/{blockId}";
public static string Update(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
14 changes: 13 additions & 1 deletion Src/Notion.Client/Api/Blocks/BlocksClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task<Block> AppendChildrenAsync(string blockId, BlocksAppendChildre
return await _client.PatchAsync<Block>(url, body);
}

public async Task<Block> Retrieve(string blockId)
public async Task<Block> RetrieveAsync(string blockId)
{
if (string.IsNullOrWhiteSpace(blockId))
{
Expand All @@ -59,5 +59,17 @@ public async Task<Block> Retrieve(string blockId)

return await _client.GetAsync<Block>(url);
}

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

var url = BlocksApiUrls.Update(blockId);

return await _client.PatchAsync<Block>(url, updateBlock);
}
}
}
10 changes: 9 additions & 1 deletion Src/Notion.Client/Api/Blocks/IBlocksClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ public interface IBlocksClient
/// </summary>
/// <param name="blockId"></param>
/// <returns>Block</returns>
Task<Block> Retrieve(string blockId);
Task<Block> RetrieveAsync(string blockId);

/// <summary>
/// Updates the content for the specified block_id based on the block type.
/// </summary>
/// <param name="blockId"></param>
/// <param name="updateBlock"></param>
/// <returns>Block</returns>
Task<Block> UpdateAsync(string blockId, IUpdateBlock updateBlock);

Task<PaginatedList<Block>> RetrieveChildrenAsync(string blockId, BlocksRetrieveChildrenParameters parameters = null);
Task<Block> AppendChildrenAsync(string blockId, BlocksAppendChildrenParameters parameters = null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Notion.Client
{
public class BulletedListItemUpdateBlock : IUpdateBlock
{
[JsonProperty("bulleted_list_item")]
public TextContentUpdate BulletedListItem { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Notion.Client
{
public class HeadingOneUpdateBlock : IUpdateBlock
{
[JsonProperty("heading_1")]
public TextContentUpdate Heading_1 { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Notion.Client
{
public class HeadingThreeeUpdateBlock : IUpdateBlock
{
[JsonProperty("heading_3")]
public TextContentUpdate Heading_3 { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Notion.Client
{
public class HeadingTwoUpdateBlock : IUpdateBlock
{
[JsonProperty("heading_2")]
public TextContentUpdate Heading_2 { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Notion.Client
{
public interface IUpdateBlock
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Notion.Client
{
public class NumberedListItemUpdateBlock : IUpdateBlock
{
[JsonProperty("numbered_list_item")]
public TextContentUpdate NumberedListItem { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Notion.Client
{
public class ParagraphUpdateBlock : IUpdateBlock
{
public TextContentUpdate Paragraph { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace Notion.Client
{
public class TextContentUpdate
{
public IEnumerable<RichTextBaseInput> Text { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Notion.Client
{
public class ToDoUpdateBlock : IUpdateBlock
{
[JsonProperty("to_do")]
public Info ToDo { get; set; }

public class Info
{
public IEnumerable<RichTextBaseInput> Text { get; set; }

[JsonProperty("checked")]
public bool IsChecked { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Notion.Client
{
public class ToggleUpdateBlock : IUpdateBlock
{
public TextContentUpdate Toggle { get; set; }
}
}
46 changes: 45 additions & 1 deletion Test/Notion.UnitTests/BlocksClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,51 @@ public async Task RetrieveAsync()
.WithBody(jsonData)
);

var block = await _client.Retrieve(blockId);
var block = await _client.RetrieveAsync(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");
}

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

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

var updateBlock = new ToDoUpdateBlock
{
ToDo = new ToDoUpdateBlock.Info
{
Text = new List<RichTextBaseInput>()
{
new RichTextTextInput
{
Text = new Text
{
Content = "Lacinato kale"
},
}
},
IsChecked = true
}
};

var block = await _client.UpdateAsync(blockId, updateBlock);

block.Id.Should().Be(blockId);
block.HasChildren.Should().BeFalse();
Expand Down
3 changes: 3 additions & 0 deletions Test/Notion.UnitTests/Notion.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<None Update="data\blocks\RetrieveBlockResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\blocks\UpdateBlockResponse.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/UpdateBlockResponse.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
}
}
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ var complexFiler = new CompoundFilter(
- [x] Retrieve a page
- [x] Create a page
- [x] Update page
- [ ] Blocks
- [x] Blocks
- [x] Retrieve a block
- [ ] Update a block
- [x] Update a block
- [x] Retrieve block children
- [x] Append block children
- [x] Users
Expand Down

0 comments on commit 8f8f4b9

Please sign in to comment.