2024-03-12 14:05:27 -04:00
|
|
|
using TwitchChatTTS.Helpers;
|
|
|
|
using TwitchChatTTS;
|
|
|
|
using System.Text.Json;
|
2024-06-16 20:19:31 -04:00
|
|
|
using TwitchChatTTS.Hermes;
|
2024-08-04 19:46:10 -04:00
|
|
|
using Serilog;
|
2023-12-30 04:27:31 -05:00
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public class HermesApiClient
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly WebClientWrap _web;
|
2024-08-04 19:46:10 -04:00
|
|
|
private readonly ILogger _logger;
|
2024-08-06 15:29:29 -04:00
|
|
|
|
2024-07-12 13:36:09 -04:00
|
|
|
public const string BASE_URL = "tomtospeech.com";
|
2023-12-30 04:27:31 -05:00
|
|
|
|
2024-08-14 14:19:18 -04:00
|
|
|
public HermesApiClient(Configuration configuration, ILogger logger)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(configuration.Hermes?.Token))
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
throw new Exception("Ensure you have written your API key in \".token\" file, in the same folder as this application.");
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
_web = new WebClientWrap(new JsonSerializerOptions()
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
PropertyNameCaseInsensitive = false,
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
|
|
|
});
|
2024-03-15 08:27:35 -04:00
|
|
|
_web.AddHeader("x-api-key", configuration.Hermes.Token);
|
2024-08-04 19:46:10 -04:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2024-07-12 13:36:09 -04:00
|
|
|
public async Task<TTSVersion?> GetLatestTTSVersion()
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-07-12 13:36:09 -04:00
|
|
|
return await _web.GetJson<TTSVersion>($"https://{BASE_URL}/api/info/version");
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<Account> FetchHermesAccountDetails()
|
|
|
|
{
|
2024-08-06 15:29:29 -04:00
|
|
|
var account = await _web.GetJson<Account>($"https://{BASE_URL}/api/account", new JsonSerializerOptions()
|
|
|
|
{
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
|
|
});
|
2024-03-15 08:27:35 -04:00
|
|
|
if (account == null || account.Id == null || account.Username == null)
|
|
|
|
throw new NullReferenceException("Invalid value found while fetching for hermes account data.");
|
|
|
|
return account;
|
2023-12-30 04:27:31 -05:00
|
|
|
}
|
|
|
|
}
|