Skip to content

Commit 4a7443f

Browse files
add users client to list users 🎉
1 parent eb550fb commit 4a7443f

File tree

8 files changed

+155
-9
lines changed

8 files changed

+155
-9
lines changed

‎Src/Notion.Client/Class1.cs‎

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace Notion.Client
4+
{
5+
internal class Constants
6+
{
7+
internal static Uri BASE_URL = new Uri("https://api.notion.com/v1/");
8+
}
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Newtonsoft.Json;
2+
using System.Collections.Generic;
3+
4+
namespace Notion.Client
5+
{
6+
public class PaginatedList<T>
7+
{
8+
public List<T> Results { get; set; }
9+
10+
[JsonProperty("has_more")]
11+
public bool HasMore { get; set; }
12+
13+
[JsonProperty("next_cursor")]
14+
public string NextCursor { get; set; }
15+
}
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Notion.Client
4+
{
5+
public class User
6+
{
7+
public string Object => "user";
8+
public string Id { get; set; }
9+
public string Type { get; set; }
10+
public string Name { get; set; }
11+
12+
[JsonProperty("avatar_url")]
13+
public string AvatarUrl { get; set; }
14+
15+
public Person Person { get; set; }
16+
public Bot Bot { get; set; }
17+
}
18+
19+
public class Person
20+
{
21+
public string Email { get; set; }
22+
}
23+
24+
public class Bot
25+
{
26+
27+
}
28+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
9+
</ItemGroup>
10+
711
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Net.Http;
2+
using System.Net.Http.Headers;
3+
using System.Threading.Tasks;
4+
5+
namespace Notion.Client
6+
{
7+
public interface IRestClient
8+
{
9+
Task<T> GetAsync<T>(string uri);
10+
}
11+
12+
public class RestClient : IRestClient
13+
{
14+
private readonly string _authToken;
15+
private HttpClient _httpClient;
16+
17+
public RestClient(string authToken)
18+
{
19+
_authToken = authToken;
20+
}
21+
22+
public async Task<T> GetAsync<T>(string uri)
23+
{
24+
EnsureHttpClient();
25+
26+
using (var stream = await _httpClient.GetStreamAsync(uri))
27+
{
28+
return SerializerHelper.Deserialize<T>(stream);
29+
}
30+
}
31+
32+
private HttpClient EnsureHttpClient()
33+
{
34+
if(_httpClient == null)
35+
{
36+
_httpClient = new HttpClient();
37+
_httpClient.BaseAddress = Constants.BASE_URL;
38+
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _authToken);
39+
}
40+
41+
return _httpClient;
42+
}
43+
}
44+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Newtonsoft.Json;
2+
using System.IO;
3+
4+
namespace Notion.Client
5+
{
6+
internal class SerializerHelper
7+
{
8+
internal static T Deserialize<T>(Stream stream)
9+
{
10+
using (StreamReader sr = new StreamReader(stream))
11+
{
12+
using (JsonReader reader = new JsonTextReader(sr))
13+
{
14+
JsonSerializer serializer = new JsonSerializer();
15+
16+
return serializer.Deserialize<T>(reader);
17+
}
18+
}
19+
}
20+
}
21+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace Notion.Client
5+
{
6+
public interface IUsersClient
7+
{
8+
Task<PaginatedList<User>> ListAsync();
9+
}
10+
11+
public class UsersClient : IUsersClient
12+
{
13+
private readonly IRestClient _client;
14+
15+
public UsersClient(IRestClient client)
16+
{
17+
_client = client;
18+
}
19+
20+
public async Task<PaginatedList<User>> ListAsync()
21+
{
22+
try
23+
{
24+
return await _client.GetAsync<PaginatedList<User>>("users");
25+
}
26+
catch (Exception e)
27+
{
28+
return null;
29+
}
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)