Skip to content

Commit e3e5e15

Browse files
Merge pull request #9 from notion-dotnet/add-database-object-api-client
Add database object API client
2 parents 1837197 + 3ec58c5 commit e3e5e15

File tree

15 files changed

+1446
-29
lines changed

15 files changed

+1446
-29
lines changed

Src/Notion.Client/Constants.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
using System;
2-
3-
namespace Notion.Client
1+
namespace Notion.Client
42
{
53
internal class Constants
64
{
7-
internal static Uri BASE_URL = new Uri("https://api.notion.com/v1/");
5+
internal static string BASE_URL = "https://api.notion.com/v1/";
6+
internal static string DEFAULT_NOTION_VERSION = "2021-05-13";
87
}
98
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
5+
namespace Notion.Client
6+
{
7+
public interface IDatabasesClient
8+
{
9+
Task<Database> RetrieveAsync(string databaseId);
10+
Task<PaginatedList<Page>> QueryAsync(string databaseId, DatabasesQueryParameters databasesQueryParameters);
11+
Task<PaginatedList<Database>> ListAsync(DatabasesListParameters databasesListParameters = null);
12+
}
13+
14+
public class DatabasesClient : IDatabasesClient
15+
{
16+
private readonly IRestClient _client;
17+
18+
public DatabasesClient(IRestClient client)
19+
{
20+
_client = client;
21+
}
22+
23+
public async Task<Database> RetrieveAsync(string databaseId)
24+
{
25+
try
26+
{
27+
return await _client.GetAsync<Database>($"databases/{databaseId}");
28+
}
29+
catch (Exception e)
30+
{
31+
// Todo: Throw Custom Exception
32+
return null;
33+
}
34+
}
35+
36+
public async Task<PaginatedList<Database>> ListAsync(DatabasesListParameters databasesListParameters = null)
37+
{
38+
try
39+
{
40+
var databasesListQueryParmaters = (IDatabasesListQueryParmaters)databasesListParameters;
41+
var queryParams = new Dictionary<string, string>()
42+
{
43+
{ "start_cursor", databasesListQueryParmaters?.StartCursor },
44+
{ "page_size", databasesListQueryParmaters?.PageSize }
45+
};
46+
47+
return await _client.GetAsync<PaginatedList<Database>>("databases", queryParams);
48+
}
49+
catch (Exception e)
50+
{
51+
// Todo: Throw Custom Exception
52+
return null;
53+
}
54+
}
55+
56+
public async Task<PaginatedList<Page>> QueryAsync(string databaseId, DatabasesQueryParameters databasesQueryParameters)
57+
{
58+
try
59+
{
60+
var body = (IDatabaseQueryBodyParameters)databasesQueryParameters;
61+
return await _client.PostAsync<PaginatedList<Page>>($"databases/{databaseId}/query", body);
62+
}
63+
catch (Exception e)
64+
{
65+
return null;
66+
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)