2024-03-12 14:05:27 -04:00
|
|
|
using System.Text.Json;
|
|
|
|
using TwitchChatTTS.Helpers;
|
2024-06-16 20:19:31 -04:00
|
|
|
using Serilog;
|
2024-08-04 19:46:10 -04:00
|
|
|
using TwitchChatTTS.Twitch.Socket.Messages;
|
|
|
|
using System.Net.Http.Json;
|
|
|
|
using System.Net;
|
2024-03-12 14:05:27 -04:00
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public class TwitchApiClient
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly ILogger _logger;
|
2024-07-16 00:48:55 -04:00
|
|
|
private readonly WebClientWrap _web;
|
2024-03-15 08:27:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
public TwitchApiClient(
|
2024-06-16 20:19:31 -04:00
|
|
|
ILogger logger
|
|
|
|
)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
2024-03-12 14:05:27 -04:00
|
|
|
|
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-08-04 19:46:10 -04:00
|
|
|
public async Task<EventResponse<EventSubscriptionMessage>?> CreateEventSubscription(string type, string version, string userId)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-08-04 19:46:10 -04:00
|
|
|
var conditions = new Dictionary<string, string>() { { "user_id", userId }, { "broadcaster_user_id", userId }, { "moderator_user_id", userId } };
|
|
|
|
var subscriptionData = new EventSubscriptionMessage(type, version, "https://hermes.goblincaves.com/api/account/authorize", "isdnmjfopsdfmsf4390", conditions);
|
|
|
|
var response = await _web.Post("https://api.twitch.tv/helix/eventsub/subscriptions", subscriptionData);
|
|
|
|
if (response.StatusCode == HttpStatusCode.Accepted)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-08-04 19:46:10 -04:00
|
|
|
_logger.Debug("Twitch API call [type: create event subscription]: " + await response.Content.ReadAsStringAsync());
|
|
|
|
return await response.Content.ReadFromJsonAsync(typeof(EventResponse<EventSubscriptionMessage>)) as EventResponse<EventSubscriptionMessage>;
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
2024-08-04 19:46:10 -04:00
|
|
|
_logger.Warning("Twitch api failed to create event subscription: " + await response.Content.ReadAsStringAsync());
|
|
|
|
return null;
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
|
2024-08-04 19:46:10 -04:00
|
|
|
public async Task<EventResponse<EventSubscriptionMessage>?> CreateEventSubscription(string type, string version, string sessionId, string userId)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-08-04 19:46:10 -04:00
|
|
|
var conditions = new Dictionary<string, string>() { { "user_id", userId }, { "broadcaster_user_id", userId }, { "moderator_user_id", userId } };
|
|
|
|
var subscriptionData = new EventSubscriptionMessage(type, version, sessionId, conditions);
|
|
|
|
var response = await _web.Post("https://api.twitch.tv/helix/eventsub/subscriptions", subscriptionData);
|
|
|
|
if (response.StatusCode == HttpStatusCode.Accepted)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-08-04 19:46:10 -04:00
|
|
|
_logger.Debug("Twitch API call [type: create event subscription]: " + await response.Content.ReadAsStringAsync());
|
|
|
|
return await response.Content.ReadFromJsonAsync(typeof(EventResponse<EventSubscriptionMessage>)) as EventResponse<EventSubscriptionMessage>;
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
2024-08-04 19:46:10 -04:00
|
|
|
_logger.Error("Twitch api failed to create event subscription: " + await response.Content.ReadAsStringAsync());
|
|
|
|
return null;
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
|
2024-08-04 19:46:10 -04:00
|
|
|
public void Initialize(TwitchBotToken token) {
|
|
|
|
_web.AddHeader("Authorization", "Bearer " + token.AccessToken);
|
|
|
|
_web.AddHeader("Client-Id", token.ClientId);
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
}
|