hermes-client/TwitchChatTTS/Hermes/HermesClient.cs

89 lines
2.8 KiB
C#
Raw Normal View History

2023-12-30 04:27:31 -05:00
using System;
using TwitchChatTTS.Hermes;
2023-12-30 04:27:31 -05:00
public class HermesClient {
private Account account;
private string key;
private WebHelper _web;
2023-12-30 04:27:31 -05:00
public string Id { get => account?.id; }
public string Username { get => account?.username; }
public HermesClient() {
// Read API Key from file.
if (!File.Exists(".token")) {
throw new Exception("Ensure you have written your API key in \".token\" file, in the same folder as this application.");
}
key = File.ReadAllText(".token")?.Trim();
_web = new WebHelper();
_web.AddHeader("x-api-key", key);
2023-12-30 04:27:31 -05:00
}
public async Task UpdateHermesAccount() {
ValidateKey();
account = await _web.GetJson<Account>("https://hermes.goblincaves.com/api/account");
2023-12-30 04:27:31 -05:00
}
public async Task<TwitchBotToken> FetchTwitchBotToken() {
ValidateKey();
2023-12-30 04:27:31 -05:00
var token = await _web.GetJson<TwitchBotToken>("https://hermes.goblincaves.com/api/token/bot");
2023-12-30 04:27:31 -05:00
if (token == null) {
throw new Exception("Failed to fetch Twitch API token from Hermes.");
}
return token;
}
public async Task<IEnumerable<TTSUsernameFilter>> FetchTTSUsernameFilters() {
ValidateKey();
var filters = await _web.GetJson<IEnumerable<TTSUsernameFilter>>("https://hermes.goblincaves.com/api/settings/tts/filter/users");
if (filters == null) {
throw new Exception("Failed to fetch TTS username filters from Hermes.");
}
return filters;
}
public async Task<string> FetchTTSDefaultVoice() {
ValidateKey();
var data = await _web.GetJson<TTSVoice>("https://hermes.goblincaves.com/api/settings/tts/default");
if (data == null) {
throw new Exception("Failed to fetch TTS default voice from Hermes.");
}
return data.label;
}
public async Task<IEnumerable<TTSVoice>> FetchTTSEnabledVoices() {
ValidateKey();
var voices = await _web.GetJson<IEnumerable<TTSVoice>>("https://hermes.goblincaves.com/api/settings/tts");
if (voices == null) {
throw new Exception("Failed to fetch TTS enabled voices from Hermes.");
}
return voices;
}
public async Task<IEnumerable<TTSWordFilter>> FetchTTSWordFilters() {
ValidateKey();
var filters = await _web.GetJson<IEnumerable<TTSWordFilter>>("https://hermes.goblincaves.com/api/settings/tts/filter/words");
if (filters == null) {
throw new Exception("Failed to fetch TTS word filters from Hermes.");
}
return filters;
}
private void ValidateKey() {
if (string.IsNullOrWhiteSpace(key)) {
throw new InvalidOperationException("Hermes API key not provided.");
}
}
2023-12-30 04:27:31 -05:00
}