using Habittodo.Model.Todoist.Responses; using Newtonsoft.Json; using System.Collections.Generic; using System.Net; using System.Threading.Tasks; namespace Habittodo.Service { public class TodoistServiceClient { private string apiKey { get; set; } private string baseUrl => "https://api.todoist.com/sync/v8/"; public TodoistServiceClient(string apiKey) { this.apiKey = apiKey; } public async Task GetItems() { var response = await GetItemChanges("*"); return response; } public async Task GetItemChanges(string syncToken) { using (var client = CreateWebClient()) { var body = InitializeRequestBody(); body["sync_token"] = syncToken; body["resource_types"] = "[\"items\"]"; var json = await client.UploadStringTaskAsync($"{baseUrl}sync", RequestBodyToString(body)); var response = JsonConvert.DeserializeObject(json); return response; } } private string RequestBodyToString(Dictionary body) { var bodyStr = ""; foreach (var pair in body) bodyStr += $"{pair.Key}={pair.Value}&"; return bodyStr; } private Dictionary InitializeRequestBody() { var body = new Dictionary(); body["token"] = apiKey; return body; } private WebClient CreateWebClient() { var client = new WebClient(); client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; return client; } } }