seperated todoist and habitica api calls into services
This commit is contained in:
@@ -67,8 +67,8 @@
|
|||||||
<Reference Include="Microsoft.Threading.Tasks.Extensions, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
<Reference Include="Microsoft.Threading.Tasks.Extensions, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||||
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.165\lib\net45\Microsoft.Threading.Tasks.Extensions.dll</HintPath>
|
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.165\lib\net45\Microsoft.Threading.Tasks.Extensions.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||||
<HintPath>..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||||
@@ -129,6 +129,10 @@
|
|||||||
<Project>{1EDCF34E-E1B1-4F82-AEC7-90C35A267967}</Project>
|
<Project>{1EDCF34E-E1B1-4F82-AEC7-90C35A267967}</Project>
|
||||||
<Name>Habitica.Todoist.Integration.Model</Name>
|
<Name>Habitica.Todoist.Integration.Model</Name>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
|
<ProjectReference Include="..\Habitica.Todoist.Integration.Services\Habitica.Todoist.Integration.Services.csproj">
|
||||||
|
<Project>{A804D4CC-B5CC-466F-AF3D-E850B16D2D15}</Project>
|
||||||
|
<Name>Habitica.Todoist.Integration.Services</Name>
|
||||||
|
</ProjectReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
|
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Habitica.Todoist.Integration.Model.Habitica;
|
using Habitica.Todoist.Integration.Model.Habitica;
|
||||||
using Habitica.Todoist.Integration.Model.Todoist;
|
using Habitica.Todoist.Integration.Model.Todoist;
|
||||||
|
using Habitica.Todoist.Integration.Services;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System;
|
using System;
|
||||||
@@ -27,17 +28,10 @@ namespace Habitica.Todoist.Integration.Console
|
|||||||
{
|
{
|
||||||
ConfigBuild();
|
ConfigBuild();
|
||||||
|
|
||||||
SyncResponse response = null;
|
var todoistClient = new TodoistClientService(todoistApiKey);
|
||||||
using (var client = new WebClient())
|
var syncResponse = todoistClient.GetUpdatedItems().ConfigureAwait(false).GetAwaiter().GetResult();
|
||||||
{
|
|
||||||
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
|
|
||||||
var token = "*";
|
|
||||||
var body = $"token={todoistApiKey}&sync_token={token}&resource_types=[\"items\"]";
|
|
||||||
response = JsonConvert.DeserializeObject<SyncResponse>(client.UploadString($"{todoistApiUrl}sync", body));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
foreach (var item in syncResponse.Items)
|
||||||
foreach (var item in response.Items)
|
|
||||||
{
|
{
|
||||||
var newTask = new Task
|
var newTask = new Task
|
||||||
{
|
{
|
||||||
@@ -47,18 +41,8 @@ namespace Habitica.Todoist.Integration.Console
|
|||||||
Priority = GetHabiticaDifficulty(item.GetDifficulty().GetValueOrDefault())
|
Priority = GetHabiticaDifficulty(item.GetDifficulty().GetValueOrDefault())
|
||||||
};
|
};
|
||||||
|
|
||||||
using (var client = new WebClient())
|
var habiticaClient = new HabiticaClientService(habiticaUserId, habiticaApiKey);
|
||||||
{
|
var task = habiticaClient.CreateUserTask(newTask).ConfigureAwait(false).GetAwaiter().GetResult();
|
||||||
client.Headers["Content-Type"] = "application/json";
|
|
||||||
client.Headers["x-api-user"] = habiticaUserId;
|
|
||||||
client.Headers["x-api-key"] = habiticaApiKey;
|
|
||||||
client.Headers["x-client"] = "console-test";
|
|
||||||
|
|
||||||
var result = client.UploadString($"{habiticaApiUrl}tasks/user", "POST", JsonConvert.SerializeObject(newTask, Formatting.Indented));
|
|
||||||
|
|
||||||
System.Console.WriteLine(result);
|
|
||||||
System.Console.ReadKey();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
<package id="Microsoft.Extensions.FileSystemGlobbing" version="3.1.2" targetFramework="net472" />
|
<package id="Microsoft.Extensions.FileSystemGlobbing" version="3.1.2" targetFramework="net472" />
|
||||||
<package id="Microsoft.Extensions.Primitives" version="3.1.2" targetFramework="net472" />
|
<package id="Microsoft.Extensions.Primitives" version="3.1.2" targetFramework="net472" />
|
||||||
<package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net471" />
|
<package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net471" />
|
||||||
<package id="Newtonsoft.Json" version="6.0.3" targetFramework="net471" />
|
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net472" />
|
||||||
<package id="System.Buffers" version="4.5.0" targetFramework="net472" />
|
<package id="System.Buffers" version="4.5.0" targetFramework="net472" />
|
||||||
<package id="System.Memory" version="4.5.3" targetFramework="net472" />
|
<package id="System.Memory" version="4.5.3" targetFramework="net472" />
|
||||||
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
|
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="6.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -15,5 +15,7 @@ namespace Habitica.Todoist.Integration.Model.Habitica
|
|||||||
public string Date { get; set; }
|
public string Date { get; set; }
|
||||||
[JsonProperty("priority")]
|
[JsonProperty("priority")]
|
||||||
public string Priority { get; set; }
|
public string Priority { get; set; }
|
||||||
|
[JsonProperty("id")]
|
||||||
|
public string Id { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
Habitica.Todoist.Integration.Model/Storage/HabitTodoLink.cs
Normal file
12
Habitica.Todoist.Integration.Model/Storage/HabitTodoLink.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Habitica.Todoist.Integration.Model.Storage
|
||||||
|
{
|
||||||
|
public class HabitTodoLink
|
||||||
|
{
|
||||||
|
public string HabiticaId { get; set; }
|
||||||
|
public string TodoistId { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
14
Habitica.Todoist.Integration.Model/Storage/TodoAction.cs
Normal file
14
Habitica.Todoist.Integration.Model/Storage/TodoAction.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Habitica.Todoist.Integration.Model.Storage
|
||||||
|
{
|
||||||
|
public enum TodoAction
|
||||||
|
{
|
||||||
|
Add = 1,
|
||||||
|
Update = 2,
|
||||||
|
Complete = 3,
|
||||||
|
Delete = 4
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Habitica.Todoist.Integration.Model/Storage/TodoApp.cs
Normal file
12
Habitica.Todoist.Integration.Model/Storage/TodoApp.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Habitica.Todoist.Integration.Model.Storage
|
||||||
|
{
|
||||||
|
public enum TodoApp
|
||||||
|
{
|
||||||
|
Habitica = 1,
|
||||||
|
Todoist = 2
|
||||||
|
}
|
||||||
|
}
|
||||||
15
Habitica.Todoist.Integration.Model/Storage/TodoChange.cs
Normal file
15
Habitica.Todoist.Integration.Model/Storage/TodoChange.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Habitica.Todoist.Integration.Model.Storage
|
||||||
|
{
|
||||||
|
public class TodoChange
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public TodoApp Application { get; set; }
|
||||||
|
public TodoAction Action { get; set; }
|
||||||
|
public bool Applied { get; set; }
|
||||||
|
public string JsonTodo { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Habitica.Todoist.Integration.Model/Storage/TodoHabitLink.cs
Normal file
12
Habitica.Todoist.Integration.Model/Storage/TodoHabitLink.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Habitica.Todoist.Integration.Model.Storage
|
||||||
|
{
|
||||||
|
public class TodoHabitLink
|
||||||
|
{
|
||||||
|
public string TodoistId { get; set; }
|
||||||
|
public string HabiticaId { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Habitica.Todoist.Integration.Model/Storage/TodoistSync.cs
Normal file
11
Habitica.Todoist.Integration.Model/Storage/TodoistSync.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Habitica.Todoist.Integration.Model.Storage
|
||||||
|
{
|
||||||
|
public class TodoistSync
|
||||||
|
{
|
||||||
|
public string SyncToken { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using Newtonsoft.Json;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -7,8 +8,11 @@ namespace Habitica.Todoist.Integration.Model.Todoist
|
|||||||
{
|
{
|
||||||
public class Due
|
public class Due
|
||||||
{
|
{
|
||||||
|
[JsonProperty("date")]
|
||||||
public string Date { get; set; }
|
public string Date { get; set; }
|
||||||
|
[JsonProperty("timezone")]
|
||||||
public string Timezone { get; set; }
|
public string Timezone { get; set; }
|
||||||
|
[JsonProperty("string")]
|
||||||
public string @String { get; set; }
|
public string @String { get; set; }
|
||||||
|
|
||||||
public string ToJavaScriptDateStr()
|
public string ToJavaScriptDateStr()
|
||||||
|
|||||||
@@ -1,14 +1,20 @@
|
|||||||
using System;
|
using Newtonsoft.Json;
|
||||||
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace Habitica.Todoist.Integration.Model.Todoist
|
namespace Habitica.Todoist.Integration.Model.Todoist
|
||||||
{
|
{
|
||||||
public class Item
|
public class Item
|
||||||
{
|
{
|
||||||
|
[JsonProperty("content")]
|
||||||
public string Content { get; set; }
|
public string Content { get; set; }
|
||||||
|
[JsonProperty("Id")]
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
|
[JsonProperty("due")]
|
||||||
public Due Due { get; set; }
|
public Due Due { get; set; }
|
||||||
|
[JsonProperty("is_deleted")]
|
||||||
public int Is_deleted { get; set; }
|
public int Is_deleted { get; set; }
|
||||||
|
[JsonProperty("date_completed")]
|
||||||
public string Date_completed { get; set; }
|
public string Date_completed { get; set; }
|
||||||
|
|
||||||
public int? GetDifficulty()
|
public int? GetDifficulty()
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using Newtonsoft.Json;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
@@ -6,8 +7,11 @@ namespace Habitica.Todoist.Integration.Model.Todoist
|
|||||||
{
|
{
|
||||||
public class SyncResponse
|
public class SyncResponse
|
||||||
{
|
{
|
||||||
|
[JsonProperty("sync_token")]
|
||||||
public string Sync_token { get; set; }
|
public string Sync_token { get; set; }
|
||||||
|
[JsonProperty("full_sync")]
|
||||||
public bool Full_sync { get; set; }
|
public bool Full_sync { get; set; }
|
||||||
|
[JsonProperty("itmes")]
|
||||||
public List<Item> Items { get; set; }
|
public List<Item> Items { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</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>
|
</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