Skip to content

Commit 71c926c

Browse files
Merge pull request #99 from notion-dotnet/feature/64-add-support-to-archive-a-page
Add support to archive a page 💖
2 parents 95062af + 2ba6fe3 commit 71c926c

File tree

8 files changed

+147
-1
lines changed

8 files changed

+147
-1
lines changed

Src/Notion.Client/Api/ApiEndpoints.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static class PagesApiUrls
2727
{
2828
public static string Create() => $"/v1/pages";
2929
public static string Retrieve(string pageId) => $"/v1/pages/{pageId}";
30+
public static string Update(string pageId) => $"/v1/pages/{pageId}";
3031
public static string UpdateProperties(string pageId) => $"/v1/pages/{pageId}";
3132
}
3233

Src/Notion.Client/Api/Pages/IPagesClient.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,14 @@ Task<Page> UpdatePropertiesAsync(
1212
string pageId,
1313
IDictionary<string, PropertyValue> updatedProperties
1414
);
15+
16+
/// <summary>
17+
/// Updates page property values for the specified page.
18+
/// Properties that are not set via the properties parameter will remain unchanged.
19+
/// </summary>
20+
/// <param name="pageId"></param>
21+
/// <param name="pagesUpdateParameters"></param>
22+
/// <returns>Updated page.</returns>
23+
Task<Page> UpdateAsync(string pageId, PagesUpdateParameters pagesUpdateParameters);
1524
}
1625
}

Src/Notion.Client/Api/Pages/PagesClient.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Threading.Tasks;
34
using static Notion.Client.ApiEndpoints;
45

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

28+
public async Task<Page> UpdateAsync(string pageId, PagesUpdateParameters pagesUpdateParameters)
29+
{
30+
var url = PagesApiUrls.Update(pageId);
31+
var body = (IPagesUpdateBodyParameters)pagesUpdateParameters;
32+
33+
return await _client.PatchAsync<Page>(url, body);
34+
}
35+
36+
[Obsolete("This method is obsolute. Use UpdateAsync instead. This API will be removed in future release")]
2737
public async Task<Page> UpdatePropertiesAsync(
2838
string pageId,
2939
IDictionary<string, PropertyValue> updatedProperties)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
3+
namespace Notion.Client
4+
{
5+
public interface IPagesUpdateBodyParameters
6+
{
7+
bool Archived { get; set; }
8+
IDictionary<string, PropertyValue> Properties { get; set; }
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
3+
namespace Notion.Client
4+
{
5+
public class PagesUpdateParameters : IPagesUpdateBodyParameters
6+
{
7+
public bool Archived { get; set; }
8+
public IDictionary<string, PropertyValue> Properties { get; set; }
9+
}
10+
}

Test/Notion.UnitTests/Notion.UnitTests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
<None Update="data\databases\UpdateDatabaseResponse.json">
3737
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3838
</None>
39+
<None Update="data\pages\ArchivePageResponse.json">
40+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
41+
</None>
3942
<None Update="data\pages\CreatePageResponse.json">
4043
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4144
</None>

Test/Notion.UnitTests/PagesClientTests.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,70 @@ public async Task PageObjectShouldHaveUrlProperty()
125125

126126
page.Url.Should().Be("https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c");
127127
}
128+
129+
[Fact]
130+
public async Task UpdatePageAsync()
131+
{
132+
var pageId = "251d2b5f-268c-4de2-afe9-c71ff92ca95c";
133+
var path = ApiEndpoints.PagesApiUrls.UpdateProperties(pageId);
134+
135+
var jsonData = await File.ReadAllTextAsync("data/pages/UpdatePagePropertiesResponse.json");
136+
137+
Server.Given(CreatePatchRequestBuilder(path))
138+
.RespondWith(
139+
Response.Create()
140+
.WithStatusCode(200)
141+
.WithBody(jsonData)
142+
);
143+
144+
var pagesUpdateParameters = new PagesUpdateParameters
145+
{
146+
Properties = new Dictionary<string, PropertyValue>()
147+
{
148+
{ "In stock", new CheckboxPropertyValue() { Checkbox = true } }
149+
}
150+
};
151+
152+
var page = await _client.UpdateAsync(pageId, pagesUpdateParameters);
153+
154+
page.Id.Should().Be(pageId);
155+
page.IsArchived.Should().BeFalse();
156+
page.Properties.Should().HaveCount(2);
157+
var updatedProperty = page.Properties.First(x => x.Key == "In stock");
158+
((CheckboxPropertyValue)updatedProperty.Value).Checkbox.Should().BeTrue();
159+
}
160+
161+
[Fact]
162+
public async Task ArchivePageAsync()
163+
{
164+
var pageId = "251d2b5f-268c-4de2-afe9-c71ff92ca95c";
165+
var path = ApiEndpoints.PagesApiUrls.UpdateProperties(pageId);
166+
167+
var jsonData = await File.ReadAllTextAsync("data/pages/ArchivePageResponse.json");
168+
169+
Server.Given(CreatePatchRequestBuilder(path))
170+
.RespondWith(
171+
Response.Create()
172+
.WithStatusCode(200)
173+
.WithBody(jsonData)
174+
);
175+
176+
var pagesUpdateParameters = new PagesUpdateParameters
177+
{
178+
Archived = true,
179+
Properties = new Dictionary<string, PropertyValue>()
180+
{
181+
{ "In stock", new CheckboxPropertyValue() { Checkbox = true } }
182+
}
183+
};
184+
185+
var page = await _client.UpdateAsync(pageId, pagesUpdateParameters);
186+
187+
page.Id.Should().Be(pageId);
188+
page.IsArchived.Should().BeTrue();
189+
page.Properties.Should().HaveCount(2);
190+
var updatedProperty = page.Properties.First(x => x.Key == "In stock");
191+
((CheckboxPropertyValue)updatedProperty.Value).Checkbox.Should().BeTrue();
192+
}
128193
}
129194
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"object": "page",
3+
"id": "251d2b5f-268c-4de2-afe9-c71ff92ca95c",
4+
"created_time": "2020-03-17T19:10:04.968Z",
5+
"last_edited_time": "2020-03-17T21:49:37.913Z",
6+
"archived": true,
7+
"url": "https://www.notion.so/Test-251d2b5f268c4de2afe9c71ff92ca95c",
8+
"properties": {
9+
"In stock": {
10+
"id": "{>U;",
11+
"type": "checkbox",
12+
"checkbox": true
13+
},
14+
"Name": {
15+
"id": "title",
16+
"type": "title",
17+
"title": [
18+
{
19+
"type": "text",
20+
"text": {
21+
"content": "Test",
22+
"link": null
23+
},
24+
"annotations": {
25+
"bold": false,
26+
"italic": false,
27+
"strikethrough": false,
28+
"underline": false,
29+
"code": false,
30+
"color": "default"
31+
},
32+
"plain_text": "Test",
33+
"href": null
34+
}
35+
]
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)