lucidiot/HabitSharp
lucidiot
/
HabitSharp
Archived
1
0
Fork 0
This repository has been archived on 2022-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
HabitSharp/HabiticaApi.cs

40 lines
1.4 KiB
C#
Raw Normal View History

2018-08-30 22:43:02 +00:00
using System;
2018-09-01 14:18:53 +00:00
using System.Collections.Generic;
2018-08-30 22:43:02 +00:00
using RestSharp;
using HabitSharp.Managers;
2018-08-30 22:43:02 +00:00
namespace HabitSharp
{
public class HabiticaApi
{
const string BaseURL = "https://habitica.com/api/v3";
private readonly RestClient Client;
public readonly TagManager Tags;
2018-08-30 22:43:02 +00:00
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);
this.Tags = new TagManager(this);
2018-08-30 22:43:02 +00:00
}
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;
}
2018-09-01 14:18:53 +00:00
public Result<Dictionary<string, string>> GetModelPaths(string ModelName) {
var req = new RestRequest("models/{model}/paths");
req.AddUrlSegment("model", ModelName);
return this.Execute<Result<Dictionary<string, string>>>(req);
}
2018-08-30 22:43:02 +00:00
}
}