Skip to content

Commit a2926f0

Browse files
committed
Initial Version
Initial release of C# Toggl API interface library
0 parents  commit a2926f0

16 files changed

+913
-0
lines changed

IToggl.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.Serialization;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace TogglAPI
9+
{
10+
interface IToggl
11+
{
12+
int? Id { get; set; }
13+
string Name { get; set; }
14+
}
15+
}

Properties/AssemblyInfo.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("TogglAPI")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Thermo Fisher Scientific")]
12+
[assembly: AssemblyProduct("TogglAPI")]
13+
[assembly: AssemblyCopyright("Copyright © Thermo Fisher Scientific 2013")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("e9081bfd-84ac-45b2-9c7f-4be8361187bb")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Toggl.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Net.Http;
6+
using System.Net.Http.Headers;
7+
using System.Runtime.Serialization;
8+
using System.Runtime.Serialization.Json;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
namespace TogglAPI
13+
{
14+
[DataContract]
15+
abstract public class Toggl
16+
: IToggl, IExtensibleDataObject
17+
{
18+
[DataMember(Name = "id", EmitDefaultValue = false)]
19+
public int? Id { get; set; }
20+
21+
[DataMember(Name = "name", EmitDefaultValue = false)]
22+
public string Name { get; set; }
23+
24+
public ExtensionDataObject ExtensionData { get; set; }
25+
}
26+
27+
}

TogglAPI.csproj

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{720A46C2-97CE-4474-A318-46F609C226B9}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>TogglAPI</RootNamespace>
11+
<AssemblyName>TogglAPI</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="System" />
34+
<Reference Include="System.Core" />
35+
<Reference Include="System.Net.Http" />
36+
<Reference Include="System.Runtime.Serialization" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="System.Data.DataSetExtensions" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Data" />
41+
<Reference Include="System.Xml" />
42+
</ItemGroup>
43+
<ItemGroup>
44+
<Compile Include="IToggl.cs" />
45+
<Compile Include="Properties\AssemblyInfo.cs" />
46+
<Compile Include="Toggl.cs" />
47+
<Compile Include="TogglData.cs" />
48+
<Compile Include="TogglProject.cs" />
49+
<Compile Include="TogglProjectUser.cs" />
50+
<Compile Include="TogglSession.cs" />
51+
<Compile Include="TogglSessionData.cs" />
52+
<Compile Include="TogglSessionInfo.cs" />
53+
<Compile Include="TogglTag.cs" />
54+
<Compile Include="TogglTask.cs" />
55+
<Compile Include="TogglTimeEntry.cs" />
56+
<Compile Include="TogglUser.cs" />
57+
<Compile Include="TogglWithDetails.cs" />
58+
<Compile Include="TogglWorkspace.cs" />
59+
</ItemGroup>
60+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
61+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
62+
Other similar extension points exist, see Microsoft.Common.targets.
63+
<Target Name="BeforeBuild">
64+
</Target>
65+
<Target Name="AfterBuild">
66+
</Target>
67+
-->
68+
</Project>

TogglData.cs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Net.Http;
7+
using System.Net.Http.Headers;
8+
using System.Reflection;
9+
using System.Runtime.Serialization;
10+
using System.Runtime.Serialization.Json;
11+
using System.Text;
12+
using System.Threading.Tasks;
13+
14+
namespace TogglAPI
15+
{
16+
[DataContract]
17+
public class TogglDataObj
18+
{
19+
[DataMember(Name = "data")]
20+
public TogglData Data { get; set; }
21+
}
22+
23+
[DataContract]
24+
public class TogglData
25+
: IExtensibleDataObject
26+
{
27+
public ExtensionDataObject ExtensionData { get; set; }
28+
29+
private Dictionary<string, object> _ExtensionData;
30+
31+
public Dictionary<string, object> ExtensionDataMembers
32+
{
33+
get
34+
{
35+
if (_ExtensionData == null)
36+
GetExtensionDataMembers();
37+
return _ExtensionData;
38+
}
39+
}
40+
41+
public object GetExtensionDataMembers()
42+
{
43+
_ExtensionData = new Dictionary<string, object>();
44+
object innerValue = null;
45+
PropertyInfo membersProperty = typeof(ExtensionDataObject).GetProperty("Members", BindingFlags.NonPublic | BindingFlags.Instance);
46+
IList members = (IList)membersProperty.GetValue(ExtensionData, null);
47+
foreach (object member in members)
48+
{
49+
PropertyInfo[] Properties = member.GetType().GetProperties();//y("Name", BindingFlags.IgnoreCase);
50+
PropertyInfo nameProperty = Properties.Where(x => x.Name.Equals("Name", StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
51+
if (nameProperty == null) continue;
52+
string name = (string)nameProperty.GetValue(member, null);
53+
PropertyInfo valueProperty = Properties.Where(x => x.Name.Equals("Value", StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
54+
if (valueProperty != null)
55+
{
56+
object value = valueProperty.GetValue(member, null);
57+
PropertyInfo innerValueProperty = value.GetType().GetProperty("Value", BindingFlags.Public | BindingFlags.Instance);
58+
innerValue = innerValueProperty.GetValue(value, null);
59+
}
60+
_ExtensionData.Add(name, innerValue);
61+
}
62+
return innerValue;
63+
}
64+
65+
}
66+
67+
}

TogglProject.cs

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Net.Http;
7+
using System.Net.Http.Headers;
8+
using System.Runtime.Serialization;
9+
using System.Runtime.Serialization.Json;
10+
using System.Text;
11+
using System.Threading.Tasks;
12+
13+
namespace TogglAPI
14+
{
15+
[DataContract]
16+
public class TogglProject
17+
: TogglWithDetails
18+
{
19+
protected override string DetailsUrl { get { return "https://www.toggl.com/api/v8/projects/{0}/tasks"; } }
20+
21+
[DataMember(Name = "wid")]
22+
public int WorkspaceId { get; set; }
23+
24+
[DataMember(Name = "billable")]
25+
public bool Billable { get; set; }
26+
27+
[DataMember(Name = "is_private")]
28+
public bool IsPrivate { get; set; }
29+
30+
[DataMember(Name = "active")]
31+
public bool Active { get; set; }
32+
33+
[DataMember(Name = "at")]
34+
public string AtStr { get; set; }
35+
36+
public DateTime At
37+
{
38+
get
39+
{
40+
return Convert.ToDateTime(AtStr);
41+
}
42+
set
43+
{
44+
string append = value.ToString("%K");
45+
if (string.IsNullOrEmpty(append)) append = DateTime.Now.ToString("%K");
46+
AtStr = value.ToString("s", DateTimeFormatInfo.InvariantInfo) + append;
47+
}
48+
}
49+
50+
private const string TogglTaskUrl = "https://www.toggl.com/api/v8/tasks";
51+
52+
public TogglTask AddTask(string name, string owner, string uApiToken, string ApiToken,
53+
IEnumerable<TogglUser> users, IEnumerable<TogglProjectUser> projectUsers)
54+
{
55+
TogglTask task = default(TogglTask);
56+
// create a new task
57+
TogglProjectUser projectUser = default(TogglProjectUser);
58+
TogglUser user = users.Where(x => x.Email.Equals(owner, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
59+
if (user != null)
60+
projectUser = projectUsers.Where(x => x.UserId == user.Id).FirstOrDefault();
61+
if (projectUser == null) uApiToken = ApiToken;
62+
TogglTaskObj taskObj = new TogglTaskObj()
63+
{
64+
Task = new TogglTask()
65+
{
66+
Name = name,
67+
ProjectId = this.Id.Value,
68+
Active = true
69+
}
70+
};
71+
task = taskObj.Task;
72+
try
73+
{
74+
TogglDataObj result = TogglSession.PostDetails<TogglTaskObj>(uApiToken, taskObj, RequestUrl: TogglTaskUrl);
75+
task.Id = (int)result.Data.ExtensionDataMembers["id"];
76+
}
77+
catch (Exception)
78+
{
79+
task = null;
80+
}
81+
return task;
82+
}
83+
84+
public TogglTask UpdateTask(TogglTask task, string owner, string uApiToken, string ApiToken,
85+
IEnumerable<TogglUser> users, IEnumerable<TogglProjectUser> projectUsers)
86+
{
87+
TogglProjectUser projectUser = default(TogglProjectUser);
88+
TogglUser user = users.Where(x => x.Email.Equals(owner, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
89+
if (user != null)
90+
projectUser = projectUsers.Where(x => x.UserId == user.Id).FirstOrDefault();
91+
if (projectUser == null) uApiToken = ApiToken;
92+
TogglTaskObj taskObj = new TogglTaskObj()
93+
{
94+
Task = task
95+
};
96+
try
97+
{
98+
TogglDataObj result = TogglSession.PutDetails<TogglTaskObj>(uApiToken, taskObj,
99+
RequestUrl: String.Format("{0}/{1}", TogglTaskUrl, task.Id));
100+
}
101+
catch (Exception)
102+
{
103+
task = null;
104+
}
105+
return task;
106+
}
107+
108+
}
109+
110+
}

TogglProjectUser.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.Serialization;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace TogglAPI
9+
{
10+
[DataContract]
11+
public class TogglProjectUser
12+
: Toggl, IExtensibleDataObject
13+
{
14+
[DataMember(Name = "uid")]
15+
public int UserId { get; set; }
16+
17+
}
18+
}

0 commit comments

Comments
 (0)