Skip to content

Commit 4b0975f

Browse files
add support to configure baseUrl & notion version
1 parent 1837197 commit 4b0975f

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
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
}

Src/Notion.Client/NotionClient.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
{
33
public class NotionClient : INotionClient
44
{
5-
public NotionClient(string authToken)
5+
public NotionClient(ClientOptions options)
66
{
7-
AuthToken = authToken;
8-
Users = new UsersClient(new RestClient(authToken));
7+
Users = new UsersClient(new RestClient(options));
98
}
109

1110
public string AuthToken { get; }

Src/Notion.Client/RestClient.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net.Http;
1+
using System;
2+
using System.Net.Http;
23
using System.Net.Http.Headers;
34
using System.Threading.Tasks;
45

@@ -11,12 +12,21 @@ public interface IRestClient
1112

1213
public class RestClient : IRestClient
1314
{
14-
private readonly string _authToken;
1515
private HttpClient _httpClient;
16+
private readonly ClientOptions _options;
1617

17-
public RestClient(string authToken)
18+
public RestClient(ClientOptions options)
1819
{
19-
_authToken = authToken;
20+
_options = MergeOptions(options);
21+
}
22+
23+
private static ClientOptions MergeOptions(ClientOptions options)
24+
{
25+
return new ClientOptions {
26+
AuthToken = options.AuthToken,
27+
BaseUrl = options.BaseUrl ?? Constants.BASE_URL,
28+
NotionVersion = options.NotionVersion ?? Constants.DEFAULT_NOTION_VERSION
29+
};
2030
}
2131

2232
public async Task<T> GetAsync<T>(string uri)
@@ -34,11 +44,18 @@ private HttpClient EnsureHttpClient()
3444
if(_httpClient == null)
3545
{
3646
_httpClient = new HttpClient();
37-
_httpClient.BaseAddress = Constants.BASE_URL;
38-
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _authToken);
47+
_httpClient.BaseAddress = new Uri(_options.BaseUrl);
48+
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _options.AuthToken);
3949
}
4050

4151
return _httpClient;
4252
}
4353
}
54+
55+
public class ClientOptions
56+
{
57+
public string BaseUrl { get; set; }
58+
public string NotionVersion { get; set; }
59+
public string AuthToken { get; set; }
60+
}
4461
}

Test/Notion.UnitTests/UserClientTest.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ namespace Notion.UnitTests
66
{
77
public class UserClientTest
88
{
9-
private readonly string _authToken;
109
private readonly IUsersClient _client;
1110

1211
public UserClientTest()
1312
{
14-
_authToken = "<Token>";
15-
_client = new UsersClient(new RestClient(_authToken));
13+
var options = new ClientOptions
14+
{
15+
AuthToken = "<Token>"
16+
};
17+
18+
_client = new UsersClient(new RestClient(options));
1619
}
1720

1821
[Fact(Skip = "Internal testing purpose")]

0 commit comments

Comments
 (0)