lucidiot/HabitSharp
lucidiot
/
HabitSharp
Archived
1
0
Fork 0

Basic API client

This commit is contained in:
Lucidiot 2018-08-31 00:43:02 +02:00
parent 04e1c7ff40
commit 317eef562b
No known key found for this signature in database
GPG Key ID: AE3F7205692FA205
1 changed files with 32 additions and 0 deletions

32
HabiticaApi.cs Normal file
View File

@ -0,0 +1,32 @@
using System;
using RestSharp;
namespace HabitSharp
{
public class HabiticaApi
{
const string BaseURL = "https://habitica.com/api/v3";
private readonly RestClient Client;
public HabiticaApi(string UserID, string ApiKey) : this(Guid.Parse(UserID), Guid.Parse(ApiKey)) { }
public HabiticaApi(Guid UserID, Guid ApiKey)
{
this.Client = new RestClient(BaseURL);
this.Client.Authenticator = new HabiticaAuthenticator(UserID, ApiKey);
}
public T Execute<T>(RestRequest request) where T : new() {
var response = Client.Execute<T>(request);
if (response.ErrorException != null) throw new ApplicationException(
"An error occured while running a Habitica API request. See inner exception for more info",
response.ErrorException);
return response.Data;
}
public ListResult<Tag> ListTags() {
return this.Execute<ListResult<Tag>>(new RestRequest("tags"));
}
}
}