Added support to test against Twitch's mocked websocket & webserver. Fixed some connection problems.
This commit is contained in:
@ -4,17 +4,21 @@ using Serilog;
|
||||
using TwitchChatTTS.Twitch.Socket.Messages;
|
||||
using System.Net.Http.Json;
|
||||
using System.Net;
|
||||
using TwitchChatTTS;
|
||||
|
||||
public class TwitchApiClient
|
||||
{
|
||||
private readonly Configuration _configuration;
|
||||
private readonly ILogger _logger;
|
||||
private readonly WebClientWrap _web;
|
||||
|
||||
|
||||
public TwitchApiClient(
|
||||
Configuration configuration,
|
||||
ILogger logger
|
||||
)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
|
||||
_web = new WebClientWrap(new JsonSerializerOptions()
|
||||
@ -28,19 +32,23 @@ public class TwitchApiClient
|
||||
{
|
||||
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);
|
||||
var response = await _web.Post("https://api.twitch.tv/helix/eventsub/subscriptions", subscriptionData);
|
||||
var base_url = _configuration.Environment == "PROD" || string.IsNullOrWhiteSpace(_configuration.Twitch?.ApiUrl)
|
||||
? "https://api.twitch.tv/helix" : _configuration.Twitch.ApiUrl;
|
||||
var response = await _web.Post($"{base_url}/eventsub/subscriptions", subscriptionData);
|
||||
if (response.StatusCode == HttpStatusCode.Accepted)
|
||||
{
|
||||
_logger.Debug("Twitch API call [type: create event subscription]: " + await response.Content.ReadAsStringAsync());
|
||||
_logger.Debug($"Twitch API call [type: create event subscription][subscription type: {type}][response: {await response.Content.ReadAsStringAsync()}]");
|
||||
return await response.Content.ReadFromJsonAsync(typeof(EventResponse<NotificationInfo>)) as EventResponse<NotificationInfo>;
|
||||
}
|
||||
_logger.Error("Twitch api failed to create event subscription for websocket: " + await response.Content.ReadAsStringAsync());
|
||||
_logger.Error($"Twitch API call failed [type: create event subscription][subscription type: {type}][response: {await response.Content.ReadAsStringAsync()}]");
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task DeleteEventSubscription(string subscriptionId)
|
||||
{
|
||||
await _web.Delete("https://api.twitch.tv/helix/eventsub/subscriptions?id=" + subscriptionId);
|
||||
var base_url = _configuration.Environment == "PROD" || string.IsNullOrWhiteSpace(_configuration.Twitch?.ApiUrl)
|
||||
? "https://api.twitch.tv/helix" : _configuration.Twitch.ApiUrl;
|
||||
await _web.Delete($"{base_url}/eventsub/subscriptions?id=" + subscriptionId);
|
||||
}
|
||||
|
||||
public async Task<EventResponse<NotificationInfo>?> GetSubscriptions(string? status = null, string? broadcasterId = null, string? after = null)
|
||||
|
Reference in New Issue
Block a user