seperated todoist and habitica api calls into services
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Habitica.Todoist.Integration.Apis
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Habitica.Todoist.Integration.Model.Storage;
|
||||
using Microsoft.Azure.Cosmos.Table;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Habitica.Todoist.Integration.Services
|
||||
{
|
||||
public class HabitTodoStorageClient
|
||||
{
|
||||
private CloudStorageAccount storageAccount { get; set; }
|
||||
|
||||
public HabitTodoStorageClient(string connectionString)
|
||||
{
|
||||
this.storageAccount = CloudStorageAccount.Parse(connectionString);
|
||||
}
|
||||
|
||||
//public TodoLink CreateTodoLink(TodoLink todoLink)
|
||||
//{
|
||||
// return null;
|
||||
//}
|
||||
}
|
||||
}
|
||||
@@ -4,4 +4,13 @@
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Azure.Cosmos.Table" Version="1.0.6" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Habitica.Todoist.Integration.Model\Habitica.Todoist.Integration.Model.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
using HabiticaTask = Habitica.Todoist.Integration.Model.Habitica.Task;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Habitica.Todoist.Integration.Services
|
||||
{
|
||||
public class HabiticaClientService
|
||||
{
|
||||
private string userId { get; set; }
|
||||
private string apiKey { get; set; }
|
||||
|
||||
private string baseUrl => "https://habitica.com/api/v3/";
|
||||
|
||||
public HabiticaClientService(string userId, string apiKey)
|
||||
{
|
||||
this.userId = userId;
|
||||
this.apiKey = apiKey;
|
||||
}
|
||||
|
||||
public async Task<HabiticaTask> CreateUserTask(HabiticaTask task)
|
||||
{
|
||||
using (var client = CreateWebClient())
|
||||
{
|
||||
var request = JsonConvert.SerializeObject(task);
|
||||
var json = await client.UploadStringTaskAsync($"{baseUrl}/tasks/user", "POST", request);
|
||||
|
||||
return JsonConvert.DeserializeObject<HabiticaTask>(json);
|
||||
}
|
||||
}
|
||||
|
||||
private WebClient CreateWebClient()
|
||||
{
|
||||
var client = new WebClient();
|
||||
|
||||
client.Headers[HttpRequestHeader.ContentType] = "application/json";
|
||||
client.Headers["x-api-user"] = userId;
|
||||
client.Headers["x-api-key"] = apiKey;
|
||||
client.Headers["x-client"] = "dotnet-habitica-client";
|
||||
|
||||
return client;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
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<SyncResponse> 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<SyncResponse>(json);
|
||||
}
|
||||
}
|
||||
|
||||
private string RequestBodyToString(Dictionary<string, string> body)
|
||||
{
|
||||
var bodyStr = "";
|
||||
foreach (var pair in body)
|
||||
bodyStr += $"{pair.Key}={pair.Value}&";
|
||||
|
||||
return bodyStr;
|
||||
}
|
||||
|
||||
private Dictionary<string, string> InitializeRequestBody()
|
||||
{
|
||||
var body = new Dictionary<string, string>();
|
||||
body["token"] = apiKey;
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
private WebClient CreateWebClient()
|
||||
{
|
||||
var client = new WebClient();
|
||||
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
|
||||
|
||||
return client;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user