-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNotionRepository.cs
60 lines (53 loc) · 1.86 KB
/
NotionRepository.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Notion.Interfaces;
using Notion.Models;
namespace Notion
{
public class NotionRepository : INotionRepository
{
private const string BaseUrl = "https://api.notion.com";
private const string AccessToken = "";
private const string TaskDatabaseId = "";
private readonly HttpClient _httpClient;
public NotionRepository()
{
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + AccessToken);
}
public async Task<List<Result>> GetTodoTasks()
{
var endpoint = $"{BaseUrl}/v1/databases/{TaskDatabaseId}/query";
var query = @"{
""filter"": {
""property"": ""Status"",
""select"": {
""does_not_equal"": ""Done 🙌""
}
}
}";
var startCursor = "";
var resultItems = new List<Result>();
do
{
var localEndpoint = endpoint;
if (!string.IsNullOrEmpty(startCursor))
{
localEndpoint = $"{endpoint}?start_cursor={startCursor}";
Thread.Sleep(500);
}
var response = _httpClient.PostAsync(localEndpoint,
new StringContent(query, Encoding.UTF8, "application/json"));
var result =
JsonConvert.DeserializeObject<QueryResponse>(await response.Result.Content.ReadAsStringAsync());
resultItems.AddRange(result.Results);
startCursor = result.HasMore ? result.NextCursor : string.Empty;
} while (!string.IsNullOrEmpty(startCursor));
return resultItems;
}
}
}