Basic API client
This commit is contained in:
parent
04e1c7ff40
commit
317eef562b
|
@ -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"));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue