Skip to content

Commit ccf3408

Browse files
Merge branch 'main' into feature/add-support-for-search-apis
2 parents b9e6fa0 + 69cf5bc commit ccf3408

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

Src/Notion.Client/Api/ApiEndpoints.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ public static class BlocksApiUrls
2121
public static string AppendChildren(string blockId) => $"/v1/blocks/{blockId}/children";
2222
}
2323

24+
public static class PagesApiUrls
25+
{
26+
public static string Create() => $"/v1/pages";
27+
public static string Retrieve(string pageId) => $"/v1/pages/{pageId}";
28+
public static string UpdateProperties(string pageId) => $"/v1/pages/{pageId}";
29+
}
30+
2431
public static class SearchApiUrls
2532
{
2633
public static string Search() => "/v1/search";
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
4+
namespace Notion.Client
5+
{
6+
public interface IPagesClient
7+
{
8+
Task<Page> CreateAsync(NewPage page);
9+
Task<Page> RetrieveAsync(string pageId);
10+
11+
Task<Page> UpdatePropertiesAsync(
12+
string pageId,
13+
IDictionary<string, PropertyValue> updatedProperties
14+
);
15+
}
16+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using static Notion.Client.ApiEndpoints;
4+
5+
namespace Notion.Client
6+
{
7+
public class PagesClient : IPagesClient
8+
{
9+
private readonly IRestClient _client;
10+
11+
public PagesClient(IRestClient client)
12+
{
13+
_client = client;
14+
}
15+
16+
public async Task<Page> CreateAsync(NewPage page)
17+
{
18+
return await _client.PostAsync<Page>(PagesApiUrls.Create(), page);
19+
}
20+
21+
public async Task<Page> RetrieveAsync(string pageId)
22+
{
23+
var url = PagesApiUrls.Retrieve(pageId);
24+
return await _client.GetAsync<Page>(url);
25+
}
26+
27+
public async Task<Page> UpdatePropertiesAsync(
28+
string pageId,
29+
IDictionary<string, PropertyValue> updatedProperties)
30+
{
31+
var url = PagesApiUrls.UpdateProperties(pageId);
32+
var body = new UpdatePropertiesParameters { Properties = updatedProperties };
33+
34+
return await _client.PatchAsync<Page>(url, body);
35+
}
36+
37+
private class UpdatePropertiesParameters
38+
{
39+
public IDictionary<string, PropertyValue> Properties { get; set; }
40+
}
41+
}
42+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Collections.Generic;
2+
3+
namespace Notion.Client
4+
{
5+
public class NewPage
6+
{
7+
/// <summary>
8+
/// Constructor without arguments: added for class initializations using class literals.
9+
/// </summary>
10+
public NewPage()
11+
{
12+
Properties = new Dictionary<string, PropertyValue>();
13+
Children = new List<Block>();
14+
}
15+
16+
/// <summary>
17+
/// Constructor that adds required <c>Parent</c> property. Used when you don't want to
18+
/// assign properties in separate operations.
19+
/// </summary>
20+
public NewPage(Parent parent)
21+
{
22+
Parent = parent;
23+
Properties = new Dictionary<string, PropertyValue>();
24+
Children = new List<Block>();
25+
}
26+
27+
public Parent Parent { get; set; }
28+
29+
public Dictionary<string, PropertyValue> Properties { get; set; }
30+
31+
public List<Block> Children { get; set; }
32+
33+
public NewPage AddProperty(string nameOrId, PropertyValue value)
34+
{
35+
Properties[nameOrId] = value;
36+
return this;
37+
}
38+
39+
public NewPage AddPageContent(Block block)
40+
{
41+
Children.Add(block);
42+
return this;
43+
}
44+
}
45+
}

Src/Notion.Client/NotionClient.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public interface INotionClient
44
{
55
IUsersClient Users { get; }
66
IDatabasesClient Databases { get; }
7+
IPagesClient Pages { get; }
78
ISearchClient Search { get; }
89
}
910

@@ -14,11 +15,13 @@ public NotionClient(ClientOptions options)
1415
var restClient = new RestClient(options);
1516
Users = new UsersClient(restClient);
1617
Databases = new DatabasesClient(restClient);
18+
Pages = new PagesClient(restClient);
1719
Search = new SearchClient(restClient);
1820
}
1921

2022
public IUsersClient Users { get; }
2123
public IDatabasesClient Databases { get; }
24+
public IPagesClient Pages { get; }
2225
public ISearchClient Search { get; }
2326
}
2427
}

0 commit comments

Comments
 (0)