using Habitica.Todoist.Integration.Model.Todoist; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Net; using System.Text; using System.Threading.Tasks; namespace Habitica.Todoist.Integration.Services { public class TodoistClientService { private string apiKey { get; set; } private string baseUrl => "https://api.todoist.com/sync/v8/"; public TodoistClientService(string apiKey) { this.apiKey = apiKey; } public async Task GetUpdatedItems(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)); return JsonConvert.DeserializeObject(json); } } 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; } } }