Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Src/Notion.Client/Api/ApiEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static class PagesApiUrls
{
public static string Create() => $"/v1/pages";
public static string Retrieve(string pageId) => $"/v1/pages/{pageId}";
public static string Update(string pageId) => $"/v1/pages/{pageId}";
public static string UpdateProperties(string pageId) => $"/v1/pages/{pageId}";
}

Expand Down
9 changes: 9 additions & 0 deletions Src/Notion.Client/Api/Pages/IPagesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@ Task<Page> UpdatePropertiesAsync(
string pageId,
IDictionary<string, PropertyValue> updatedProperties
);

/// <summary>
/// Updates page property values for the specified page.
/// Properties that are not set via the properties parameter will remain unchanged.
/// </summary>
/// <param name="pageId"></param>
/// <param name="pagesUpdateParameters"></param>
/// <returns>Updated page.</returns>
Task<Page> UpdateAsync(string pageId, PagesUpdateParameters pagesUpdateParameters);
}
}
12 changes: 11 additions & 1 deletion Src/Notion.Client/Api/Pages/PagesClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using static Notion.Client.ApiEndpoints;

Expand All @@ -24,6 +25,15 @@ public async Task<Page> RetrieveAsync(string pageId)
return await _client.GetAsync<Page>(url);
}

public async Task<Page> UpdateAsync(string pageId, PagesUpdateParameters pagesUpdateParameters)
{
var url = PagesApiUrls.Update(pageId);
var body = (IPagesUpdateBodyParameters)pagesUpdateParameters;

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

[Obsolete("This method is obsolute. Use UpdateAsync instead. This API will be removed in future release")]
public async Task<Page> UpdatePropertiesAsync(
string pageId,
IDictionary<string, PropertyValue> updatedProperties)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace Notion.Client
{
public interface IPagesUpdateBodyParameters
{
bool Archived { get; set; }
IDictionary<string, PropertyValue> Properties { get; set; }
}
}
10 changes: 10 additions & 0 deletions Src/Notion.Client/Api/Pages/RequestParams/PagesUpdateParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace Notion.Client
{
public class PagesUpdateParameters : IPagesUpdateBodyParameters
{
public bool Archived { get; set; }
public IDictionary<string, PropertyValue> Properties { get; set; }
}
}
3 changes: 3 additions & 0 deletions Test/Notion.UnitTests/Notion.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<None Update="data\databases\UpdateDatabaseResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\pages\ArchivePageResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\pages\CreatePageResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
65 changes: 65 additions & 0 deletions Test/Notion.UnitTests/PagesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,70 @@ public async Task PageObjectShouldHaveUrlProperty()

page.Url.Should().Be("https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c");
}

[Fact]
public async Task UpdatePageAsync()
{
var pageId = "251d2b5f-268c-4de2-afe9-c71ff92ca95c";
var path = ApiEndpoints.PagesApiUrls.UpdateProperties(pageId);

var jsonData = await File.ReadAllTextAsync("data/pages/UpdatePagePropertiesResponse.json");

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

var pagesUpdateParameters = new PagesUpdateParameters
{
Properties = new Dictionary<string, PropertyValue>()
{
{ "In stock", new CheckboxPropertyValue() { Checkbox = true } }
}
};

var page = await _client.UpdateAsync(pageId, pagesUpdateParameters);

page.Id.Should().Be(pageId);
page.IsArchived.Should().BeFalse();
page.Properties.Should().HaveCount(2);
var updatedProperty = page.Properties.First(x => x.Key == "In stock");
((CheckboxPropertyValue)updatedProperty.Value).Checkbox.Should().BeTrue();
}

[Fact]
public async Task ArchivePageAsync()
{
var pageId = "251d2b5f-268c-4de2-afe9-c71ff92ca95c";
var path = ApiEndpoints.PagesApiUrls.UpdateProperties(pageId);

var jsonData = await File.ReadAllTextAsync("data/pages/ArchivePageResponse.json");

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

var pagesUpdateParameters = new PagesUpdateParameters
{
Archived = true,
Properties = new Dictionary<string, PropertyValue>()
{
{ "In stock", new CheckboxPropertyValue() { Checkbox = true } }
}
};

var page = await _client.UpdateAsync(pageId, pagesUpdateParameters);

page.Id.Should().Be(pageId);
page.IsArchived.Should().BeTrue();
page.Properties.Should().HaveCount(2);
var updatedProperty = page.Properties.First(x => x.Key == "In stock");
((CheckboxPropertyValue)updatedProperty.Value).Checkbox.Should().BeTrue();
}
}
}
38 changes: 38 additions & 0 deletions Test/Notion.UnitTests/data/pages/ArchivePageResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"object": "page",
"id": "251d2b5f-268c-4de2-afe9-c71ff92ca95c",
"created_time": "2020-03-17T19:10:04.968Z",
"last_edited_time": "2020-03-17T21:49:37.913Z",
"archived": true,
"url": "https://www.notion.so/Test-251d2b5f268c4de2afe9c71ff92ca95c",
"properties": {
"In stock": {
"id": "{>U;",
"type": "checkbox",
"checkbox": true
},
"Name": {
"id": "title",
"type": "title",
"title": [
{
"type": "text",
"text": {
"content": "Test",
"link": null
},
"annotations": {
"bold": false,
"italic": false,
"strikethrough": false,
"underline": false,
"code": false,
"color": "default"
},
"plain_text": "Test",
"href": null
}
]
}
}
}