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-06 15:29:29 -04:00
|
|
|
public async Task<EventResponse<NotificationInfo>?> CreateEventSubscription(string type, string version, string sessionId, string userId, string? broadcasterId = null)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-08-06 15:29:29 -04:00
|
|
|
var conditions = new Dictionary<string, string>() { { "user_id", userId }, { "broadcaster_user_id", broadcasterId ?? userId }, { "moderator_user_id", broadcasterId ?? userId } };
|
|
|
|
var subscriptionData = new EventSubscriptionMessage(type, version, sessionId, conditions);
|
2024-08-04 19:46:10 -04:00
|
|
|
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());
|
2024-08-06 15:29:29 -04:00
|
|
|
return await response.Content.ReadFromJsonAsync(typeof(EventResponse<NotificationInfo>)) as EventResponse<NotificationInfo>;
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
2024-08-06 15:29:29 -04:00
|
|
|
_logger.Error("Twitch api failed to create event subscription for websocket: " + await response.Content.ReadAsStringAsync());
|
2024-08-04 19:46:10 -04:00
|
|
|
return null;
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
|
2024-08-06 15:29:29 -04:00
|
|
|
public async Task DeleteEventSubscription(string subscriptionId)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-08-06 15:29:29 -04:00
|
|
|
await _web.Delete("https://api.twitch.tv/helix/eventsub/subscriptions?id=" + subscriptionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<EventResponse<NotificationInfo>?> GetSubscriptions(string? status = null, string? broadcasterId = null, string? after = null)
|
|
|
|
{
|
|
|
|
List<string> queryParams = new List<string>();
|
|
|
|
if (!string.IsNullOrWhiteSpace(status))
|
|
|
|
queryParams.Add("status=" + status);
|
|
|
|
if (!string.IsNullOrWhiteSpace(broadcasterId))
|
|
|
|
queryParams.Add("user_id=" + broadcasterId);
|
|
|
|
if (!string.IsNullOrWhiteSpace(after))
|
|
|
|
queryParams.Add("after=" + after);
|
|
|
|
var query = queryParams.Any() ? '?' + string.Join('&', queryParams) : string.Empty;
|
|
|
|
return await _web.GetJson<EventResponse<NotificationInfo>>("https://api.twitch.tv/helix/eventsub/subscriptions" + query);
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
|
2024-08-06 15:29:29 -04:00
|
|
|
public void Initialize(TwitchBotToken token)
|
|
|
|
{
|
2024-08-04 19:46:10 -04:00
|
|
|
_web.AddHeader("Authorization", "Bearer " + token.AccessToken);
|
|
|
|
_web.AddHeader("Client-Id", token.ClientId);
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
}
|