Skip to content

Commit 8755042

Browse files
add support for databases list query params ✨
1 parent a29e793 commit 8755042

File tree

5 files changed

+122
-6
lines changed

5 files changed

+122
-6
lines changed

Src/Notion.Client/DatabaseClient.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
using System.Collections.Generic;
12
using System.Threading.Tasks;
23

34
namespace Notion.Client
45
{
56
public interface IDatabaseClient
67
{
7-
Task<PaginatedList<Database>> ListAsync();
8-
8+
Task<PaginatedList<Database>> ListAsync(DatabasesListParameters databasesListParameters = null);
99
}
1010

1111
public class DatabaseClient : IDatabaseClient
@@ -17,11 +17,17 @@ public DatabaseClient(IRestClient client)
1717
_client = client;
1818
}
1919

20-
public async Task<PaginatedList<Database>> ListAsync()
20+
public async Task<PaginatedList<Database>> ListAsync(DatabasesListParameters databasesListParameters = null)
2121
{
2222
try
2323
{
24-
return await _client.GetAsync<PaginatedList<Database>>("databases");
24+
var queryParams = new Dictionary<string, string>()
25+
{
26+
{ "start_cursor", databasesListParameters?.PaginationParameters?.StartCursor },
27+
{ "page_size", databasesListParameters?.PaginationParameters?.PageSize }
28+
};
29+
30+
return await _client.GetAsync<PaginatedList<Database>>("databases", queryParams);
2531
}
2632
catch (System.Exception e)
2733
{

Src/Notion.Client/Models/Database.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
namespace Notion.Client
99
{
10+
public class DatabasesListParameters
11+
{
12+
public PaginationParameters PaginationParameters { get; set; }
13+
}
14+
1015
public class Database
1116
{
1217
public string Object => "database";

Src/Notion.Client/Models/PaginatedList.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
namespace Notion.Client
55
{
6+
public class PaginationParameters
7+
{
8+
public string StartCursor { get; set; }
9+
public string PageSize { get; set; }
10+
}
11+
612
public class PaginatedList<T>
713
{
814
public const string Object = "List";

Src/Notion.Client/RestClient.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
using System.Net.Http.Headers;
55
using System.Threading.Tasks;
66
using Newtonsoft.Json;
7+
using Notion.Client.http;
78

89
namespace Notion.Client
910
{
1011
public interface IRestClient
1112
{
12-
Task<T> GetAsync<T>(string uri);
13+
Task<T> GetAsync<T>(string uri, Dictionary<string, string> queryParams = null);
1314
}
1415

1516
public class RestClient : IRestClient
@@ -37,10 +38,12 @@ private static ClientOptions MergeOptions(ClientOptions options)
3738
};
3839
}
3940

40-
public async Task<T> GetAsync<T>(string uri)
41+
public async Task<T> GetAsync<T>(string uri, Dictionary<string, string> queryParams = null)
4142
{
4243
EnsureHttpClient();
4344

45+
uri = queryParams == null ? uri : QueryHelpers.AddQueryString(uri, queryParams);
46+
4447
using (var stream = await _httpClient.GetStreamAsync(uri))
4548
{
4649
return SerializerHelper.Deserialize<T>(stream, jsonConverters);
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Text.Encodings.Web;
6+
7+
namespace Notion.Client.http
8+
{
9+
internal static class QueryHelpers
10+
{
11+
public static string AddQueryString(string uri, string name, string value)
12+
{
13+
if (uri == null)
14+
{
15+
throw new ArgumentNullException(nameof(uri));
16+
}
17+
18+
if (name == null)
19+
{
20+
throw new ArgumentNullException(nameof(name));
21+
}
22+
23+
if (value == null)
24+
{
25+
throw new ArgumentNullException(nameof(value));
26+
}
27+
28+
return AddQueryString(uri, new[] { new KeyValuePair<string, string>(name, value) });
29+
}
30+
31+
public static string AddQueryString(string uri, IDictionary<string, string> queryParams)
32+
{
33+
if (uri == null)
34+
{
35+
throw new ArgumentNullException(nameof(uri));
36+
}
37+
38+
if (queryParams == null)
39+
{
40+
throw new ArgumentNullException(nameof(queryParams));
41+
}
42+
43+
return AddQueryString(uri, (IEnumerable<KeyValuePair<string, string>>)queryParams);
44+
}
45+
46+
private static string AddQueryString(
47+
string uri,
48+
IEnumerable<KeyValuePair<string, string>> queryParams)
49+
{
50+
if (uri == null)
51+
{
52+
throw new ArgumentNullException(nameof(uri));
53+
}
54+
55+
if (queryParams == null)
56+
{
57+
throw new ArgumentNullException(nameof(queryParams));
58+
}
59+
60+
queryParams = RemoveEmptyValueQueryParams(queryParams);
61+
62+
var anchorIndex = uri.IndexOf('#');
63+
var uriToBeAppended = uri;
64+
var anchorText = "";
65+
66+
if (anchorIndex != -1)
67+
{
68+
anchorText = uri.Substring(anchorIndex);
69+
uriToBeAppended = uri.Substring(0, anchorIndex);
70+
}
71+
72+
var queryIndex = uriToBeAppended.IndexOf('?');
73+
var hasQuery = queryIndex != -1;
74+
75+
var sb = new StringBuilder();
76+
sb.Append(uriToBeAppended);
77+
foreach (var parameter in queryParams)
78+
{
79+
sb.Append(hasQuery ? '&' : '?');
80+
sb.Append(UrlEncoder.Default.Encode(parameter.Key));
81+
sb.Append('=');
82+
sb.Append(UrlEncoder.Default.Encode(parameter.Value));
83+
hasQuery = true;
84+
}
85+
86+
sb.Append(anchorText);
87+
return sb.ToString();
88+
}
89+
90+
private static IEnumerable<KeyValuePair<string, string>> RemoveEmptyValueQueryParams(IEnumerable<KeyValuePair<string, string>> queryParams)
91+
{
92+
return queryParams.Where(x => !string.IsNullOrWhiteSpace(x.Value));
93+
}
94+
95+
}
96+
}

0 commit comments

Comments
 (0)