Skip to content

Commit

Permalink
Merge pull request #27 from asevos/feature/pages_endpoints
Browse files Browse the repository at this point in the history
Add Pages API's support ✨
  • Loading branch information
KoditkarVedant authored Jun 20, 2021
2 parents 871fab4 + f3656a6 commit 69cf5bc
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Src/Notion.Client/Api/ApiEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@ public static class BlocksApiUrls
public static string RetrieveChildren(string blockId) => $"/v1/blocks/{blockId}/children";
public static string AppendChildren(string blockId) => $"/v1/blocks/{blockId}/children";
}

public static class PagesApiUrls
{
public static string Create() => $"/v1/pages";
public static string Retrieve(string pageId) => $"/v1/pages/{pageId}";
public static string UpdateProperties(string pageId) => $"/v1/pages/{pageId}";
}
}
}
16 changes: 16 additions & 0 deletions Src/Notion.Client/Api/Pages/IPagesClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Notion.Client
{
public interface IPagesClient
{
Task<Page> CreateAsync(NewPage page);
Task<Page> RetrieveAsync(string pageId);

Task<Page> UpdatePropertiesAsync(
string pageId,
IDictionary<string, PropertyValue> updatedProperties
);
}
}
42 changes: 42 additions & 0 deletions Src/Notion.Client/Api/Pages/PagesClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using static Notion.Client.ApiEndpoints;

namespace Notion.Client
{
public class PagesClient : IPagesClient
{
private readonly IRestClient _client;

public PagesClient(IRestClient client)
{
_client = client;
}

public async Task<Page> CreateAsync(NewPage page)
{
return await _client.PostAsync<Page>(PagesApiUrls.Create(), page);
}

public async Task<Page> RetrieveAsync(string pageId)
{
var url = PagesApiUrls.Retrieve(pageId);
return await _client.GetAsync<Page>(url);
}

public async Task<Page> UpdatePropertiesAsync(
string pageId,
IDictionary<string, PropertyValue> updatedProperties)
{
var url = PagesApiUrls.UpdateProperties(pageId);
var body = new UpdatePropertiesParameters { Properties = updatedProperties };

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

private class UpdatePropertiesParameters
{
public IDictionary<string, PropertyValue> Properties { get; set; }
}
}
}
45 changes: 45 additions & 0 deletions Src/Notion.Client/Models/Page/NewPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Collections.Generic;

namespace Notion.Client
{
public class NewPage
{
/// <summary>
/// Constructor without arguments: added for class initializations using class literals.
/// </summary>
public NewPage()
{
Properties = new Dictionary<string, PropertyValue>();
Children = new List<Block>();
}

/// <summary>
/// Constructor that adds required <c>Parent</c> property. Used when you don't want to
/// assign properties in separate operations.
/// </summary>
public NewPage(Parent parent)
{
Parent = parent;
Properties = new Dictionary<string, PropertyValue>();
Children = new List<Block>();
}

public Parent Parent { get; set; }

public Dictionary<string, PropertyValue> Properties { get; set; }

public List<Block> Children { get; set; }

public NewPage AddProperty(string nameOrId, PropertyValue value)
{
Properties[nameOrId] = value;
return this;
}

public NewPage AddPageContent(Block block)
{
Children.Add(block);
return this;
}
}
}
3 changes: 3 additions & 0 deletions Src/Notion.Client/NotionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public interface INotionClient
{
IUsersClient Users { get; }
IDatabasesClient Databases { get; }
IPagesClient Pages { get; }
}

public class NotionClient : INotionClient
Expand All @@ -13,9 +14,11 @@ public NotionClient(ClientOptions options)
var restClient = new RestClient(options);
Users = new UsersClient(restClient);
Databases = new DatabasesClient(restClient);
Pages = new PagesClient(restClient);
}

public IUsersClient Users { get; }
public IDatabasesClient Databases { get; }
public IPagesClient Pages { get; }
}
}

0 comments on commit 69cf5bc

Please sign in to comment.